diff --git a/bsp/stm32f10x/drivers/usart.c b/bsp/stm32f10x/drivers/usart.c index b10b971538c12c240ee5d581f897445b0d73f71a..cf7c2a1d51887a9be6991abb0bbb0c21ac624165 100644 --- a/bsp/stm32f10x/drivers/usart.c +++ b/bsp/stm32f10x/drivers/usart.c @@ -35,6 +35,12 @@ #define UART3_GPIO_RX GPIO_Pin_11 #define UART3_GPIO GPIOB +/* USART4 */ +#define UART4_GPIO_TX GPIO_Pin_10 +#define UART4_GPIO_RX GPIO_Pin_11 +#define UART4_GPIO GPIOC + + /* STM32 uart driver */ struct stm32_uart { @@ -264,6 +270,45 @@ void USART3_IRQHandler(void) } #endif /* RT_USING_UART3 */ +#if defined(RT_USING_UART4) +/* UART4 device driver structure */ +struct stm32_uart uart4 = +{ + UART4, + UART4_IRQn, +}; +struct rt_serial_device serial4; + +void UART4_IRQHandler(void) +{ + struct stm32_uart* uart; + + uart = &uart4; + + /* enter interrupt */ + rt_interrupt_enter(); + if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET) + { + rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND); + /* clear interrupt */ + USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE); + } + if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET) + { + /* clear interrupt */ + USART_ClearITPendingBit(uart->uart_device, USART_IT_TC); + } + if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET) + { + stm32_getc(&serial4); + } + + /* leave interrupt */ + rt_interrupt_leave(); +} +#endif /* RT_USING_UART3 */ + + static void RCC_Configuration(void) { #if defined(RT_USING_UART1) @@ -286,6 +331,14 @@ static void RCC_Configuration(void) /* Enable UART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); #endif /* RT_USING_UART3 */ + +#if defined(RT_USING_UART4) + /* Enable UART GPIO clocks */ + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + /* Enable UART clock */ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); +#endif /* RT_USING_UART4 */ + } static void GPIO_Configuration(void) @@ -326,6 +379,18 @@ static void GPIO_Configuration(void) GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX; GPIO_Init(UART3_GPIO, &GPIO_InitStructure); #endif /* RT_USING_UART3 */ + +#if defined(RT_USING_UART4) + /* Configure USART Rx/tx PIN */ + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; + GPIO_InitStructure.GPIO_Pin = UART4_GPIO_RX; + GPIO_Init(UART4_GPIO, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Pin = UART4_GPIO_TX; + GPIO_Init(UART4_GPIO, &GPIO_InitStructure); +#endif /* RT_USING_UART4 */ + } static void NVIC_Configuration(struct stm32_uart* uart) @@ -393,4 +458,21 @@ void rt_hw_usart_init(void) RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); #endif /* RT_USING_UART3 */ + +#if defined(RT_USING_UART4) + uart = &uart4; + + config.baud_rate = BAUD_RATE_115200; + + serial4.ops = &stm32_uart_ops; + serial4.config = config; + + NVIC_Configuration(&uart4); + + /* register UART4 device */ + rt_hw_serial_register(&serial4, "uart4", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + uart); +#endif /* RT_USING_UART4 */ + }