1、【优化】RTT串口框架中断读取及设置接口。
Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
parent
8a913c86b5
commit
334f97f211
|
@ -23,21 +23,21 @@
|
||||||
/* USART1 */
|
/* USART1 */
|
||||||
#define UART1_GPIO_TX GPIO_Pin_9
|
#define UART1_GPIO_TX GPIO_Pin_9
|
||||||
#define UART1_GPIO_RX GPIO_Pin_10
|
#define UART1_GPIO_RX GPIO_Pin_10
|
||||||
#define UART1_GPIO GPIOA
|
#define UART1_GPIO GPIOA
|
||||||
/* USART1_REMAP */
|
/* USART1_REMAP */
|
||||||
#define UART1_GPIO_REMAP_TX GPIO_Pin_6
|
#define UART1_GPIO_REMAP_TX GPIO_Pin_6
|
||||||
#define UART1_GPIO_REMAP_RX GPIO_Pin_7
|
#define UART1_GPIO_REMAP_RX GPIO_Pin_7
|
||||||
#define UART1_GPIO_REMAP GPIOB
|
#define UART1_GPIO_REMAP GPIOB
|
||||||
|
|
||||||
/* USART2 */
|
/* USART2 */
|
||||||
#define UART2_GPIO_TX GPIO_Pin_2
|
#define UART2_GPIO_TX GPIO_Pin_2
|
||||||
#define UART2_GPIO_RX GPIO_Pin_3
|
#define UART2_GPIO_RX GPIO_Pin_3
|
||||||
#define UART2_GPIO GPIOA
|
#define UART2_GPIO GPIOA
|
||||||
|
|
||||||
/* USART3_REMAP[1:0] = 00 */
|
/* USART3_REMAP[1:0] = 00 */
|
||||||
#define UART3_GPIO_TX GPIO_Pin_10
|
#define UART3_GPIO_TX GPIO_Pin_10
|
||||||
#define UART3_GPIO_RX GPIO_Pin_11
|
#define UART3_GPIO_RX GPIO_Pin_11
|
||||||
#define UART3_GPIO GPIOB
|
#define UART3_GPIO GPIOB
|
||||||
|
|
||||||
/* STM32 uart driver */
|
/* STM32 uart driver */
|
||||||
struct stm32_uart
|
struct stm32_uart
|
||||||
|
@ -95,7 +95,7 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
|
||||||
static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
|
static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
|
||||||
{
|
{
|
||||||
struct stm32_uart* uart;
|
struct stm32_uart* uart;
|
||||||
rt_uint32_t *irq_type = (rt_uint32_t *)(arg);
|
rt_uint32_t irq_type = (rt_uint32_t)(arg);
|
||||||
|
|
||||||
RT_ASSERT(serial != RT_NULL);
|
RT_ASSERT(serial != RT_NULL);
|
||||||
uart = (struct stm32_uart *)serial->parent.user_data;
|
uart = (struct stm32_uart *)serial->parent.user_data;
|
||||||
|
@ -105,12 +105,12 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
|
||||||
/* disable interrupt */
|
/* disable interrupt */
|
||||||
case RT_DEVICE_CTRL_CLR_INT:
|
case RT_DEVICE_CTRL_CLR_INT:
|
||||||
|
|
||||||
if ((*irq_type) == RT_DEVICE_FLAG_INT_RX)
|
if (irq_type == RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
/* disable rx irq */
|
/* disable rx irq */
|
||||||
USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
|
USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
|
||||||
}
|
}
|
||||||
else if ((*irq_type) == RT_DEVICE_FLAG_INT_TX)
|
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
|
||||||
{
|
{
|
||||||
/* disable tx irq */
|
/* disable tx irq */
|
||||||
USART_ITConfig(uart->uart_device, uart->tx_irq_type, DISABLE);
|
USART_ITConfig(uart->uart_device, uart->tx_irq_type, DISABLE);
|
||||||
|
@ -123,12 +123,12 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
|
||||||
break;
|
break;
|
||||||
/* enable interrupt */
|
/* enable interrupt */
|
||||||
case RT_DEVICE_CTRL_SET_INT:
|
case RT_DEVICE_CTRL_SET_INT:
|
||||||
if ((*irq_type) == RT_DEVICE_FLAG_INT_RX)
|
if (irq_type == RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
/* enable rx irq */
|
/* enable rx irq */
|
||||||
USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
|
USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
|
||||||
}
|
}
|
||||||
else if ((*irq_type) == RT_DEVICE_FLAG_INT_TX)
|
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
|
||||||
{
|
{
|
||||||
/* enable tx irq */
|
/* enable tx irq */
|
||||||
USART_ITConfig(uart->uart_device, uart->tx_irq_type, ENABLE);
|
USART_ITConfig(uart->uart_device, uart->tx_irq_type, ENABLE);
|
||||||
|
@ -141,37 +141,34 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
|
||||||
break;
|
break;
|
||||||
/* get interrupt flag */
|
/* get interrupt flag */
|
||||||
case RT_DEVICE_CTRL_GET_INT:
|
case RT_DEVICE_CTRL_GET_INT:
|
||||||
if ((*irq_type) == RT_DEVICE_FLAG_INT_RX)
|
if (irq_type == RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
/* return rx irq flag */
|
/* return rx irq flag */
|
||||||
(*irq_type) = USART_GetITStatus(uart->uart_device, USART_IT_RXNE);
|
return USART_GetITStatus(uart->uart_device, USART_IT_RXNE);
|
||||||
}
|
}
|
||||||
else if ((*irq_type) == RT_DEVICE_FLAG_INT_TX)
|
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
|
||||||
{
|
{
|
||||||
/* return tx irq flag */
|
/* return tx irq flag */
|
||||||
(*irq_type) = USART_GetITStatus(uart->uart_device, uart->tx_irq_type);
|
return USART_GetITStatus(uart->uart_device, uart->tx_irq_type);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/* get USART flag */
|
/* get USART flag */
|
||||||
case RT_DEVICE_CTRL_GET_FLAG:
|
case RT_DEVICE_CTRL_GET_FLAG:
|
||||||
if ((*irq_type) == RT_DEVICE_FLAG_INT_RX)
|
if (irq_type == RT_DEVICE_FLAG_INT_RX)
|
||||||
{
|
{
|
||||||
/* return rx irq flag */
|
/* return rx irq flag */
|
||||||
(*irq_type) = USART_GetFlagStatus(uart->uart_device,
|
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_RXNE);
|
||||||
USART_FLAG_RXNE);
|
|
||||||
}
|
}
|
||||||
else if ((*irq_type) == RT_DEVICE_FLAG_INT_TX)
|
else if (irq_type == RT_DEVICE_FLAG_INT_TX)
|
||||||
{
|
{
|
||||||
/* return tx flag */
|
/* return tx flag */
|
||||||
if (uart->tx_irq_type == USART_IT_TC)
|
if (uart->tx_irq_type == USART_IT_TC)
|
||||||
{
|
{
|
||||||
(*irq_type) = USART_GetFlagStatus(uart->uart_device,
|
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_TC);
|
||||||
USART_FLAG_TC);
|
|
||||||
}
|
}
|
||||||
else if (uart->tx_irq_type == USART_IT_TXE)
|
else if (uart->tx_irq_type == USART_IT_TXE)
|
||||||
{
|
{
|
||||||
(*irq_type) = USART_GetFlagStatus(uart->uart_device,
|
return USART_GetFlagStatus(uart->uart_device, USART_FLAG_TXE);
|
||||||
USART_FLAG_TXE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -445,7 +442,7 @@ void rt_hw_usart_init(void)
|
||||||
|
|
||||||
#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1)
|
#if defined(RT_USING_UART1) || defined(RT_USING_REMAP_UART1)
|
||||||
uart = &uart1;
|
uart = &uart1;
|
||||||
config.baud_rate = BAUD_RATE_9600;
|
config.baud_rate = BAUD_RATE_115200;
|
||||||
|
|
||||||
serial1.ops = &stm32_uart_ops;
|
serial1.ops = &stm32_uart_ops;
|
||||||
serial1.config = config;
|
serial1.config = config;
|
||||||
|
|
|
@ -131,22 +131,18 @@ BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
|
||||||
void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
||||||
{
|
{
|
||||||
rt_uint32_t recved_event;
|
rt_uint32_t recved_event;
|
||||||
uint32_t irq_type;
|
|
||||||
if (xRxEnable)
|
if (xRxEnable)
|
||||||
{
|
{
|
||||||
/* waiting for last transmit complete */
|
/* waiting for last transmit complete */
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
irq_type = RT_DEVICE_FLAG_INT_TX;
|
if (serial->ops->control(serial, RT_DEVICE_CTRL_GET_FLAG, (void *)RT_DEVICE_FLAG_INT_TX))
|
||||||
serial->ops->control(serial, RT_DEVICE_CTRL_GET_FLAG, &irq_type);
|
|
||||||
if (irq_type)
|
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* enable RX interrupt */
|
/* enable RX interrupt */
|
||||||
irq_type = RT_DEVICE_FLAG_INT_RX;
|
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||||
serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, &irq_type);
|
|
||||||
/* switch 485 to receive mode */
|
/* switch 485 to receive mode */
|
||||||
SLAVE_RS485_RECEIVE_MODE;
|
SLAVE_RS485_RECEIVE_MODE;
|
||||||
}
|
}
|
||||||
|
@ -155,14 +151,12 @@ void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
||||||
/* switch 485 to transmit mode */
|
/* switch 485 to transmit mode */
|
||||||
SLAVE_RS485_TRANS_MODE;
|
SLAVE_RS485_TRANS_MODE;
|
||||||
/* disable RX interrupt */
|
/* disable RX interrupt */
|
||||||
irq_type = RT_DEVICE_FLAG_INT_RX;
|
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)RT_DEVICE_FLAG_INT_RX);
|
||||||
serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, &irq_type);
|
|
||||||
}
|
}
|
||||||
if (xTxEnable)
|
if (xTxEnable)
|
||||||
{
|
{
|
||||||
/* start serial transmit */
|
/* start serial transmit */
|
||||||
rt_event_send(&event_serial, EVENT_SERIAL_TRANS_START);
|
rt_event_send(&event_serial, EVENT_SERIAL_TRANS_START);
|
||||||
irq_type = RT_DEVICE_FLAG_INT_TX;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -170,7 +164,6 @@ void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
|
||||||
rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START,
|
rt_event_recv(&event_serial, EVENT_SERIAL_TRANS_START,
|
||||||
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 0,
|
RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 0,
|
||||||
&recved_event);
|
&recved_event);
|
||||||
irq_type = RT_DEVICE_FLAG_INT_TX;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue