From 8973fa9d3e59bc793539afae448c9bb45d10104b Mon Sep 17 00:00:00 2001 From: chenyong <1521761801@qq.com> Date: Wed, 29 Aug 2018 11:11:10 +0800 Subject: [PATCH] [bsp][stm32f4xx-HAL] Add uart3 driver --- bsp/stm32f4xx-HAL/Kconfig | 8 +++- bsp/stm32f4xx-HAL/drivers/drv_usart.c | 66 +++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/bsp/stm32f4xx-HAL/Kconfig b/bsp/stm32f4xx-HAL/Kconfig index 62eec30473..ee749b6f44 100644 --- a/bsp/stm32f4xx-HAL/Kconfig +++ b/bsp/stm32f4xx-HAL/Kconfig @@ -303,8 +303,12 @@ config RT_USING_UART1 config RT_USING_UART2 bool "Using UART2" - default n - + default n + +config RT_USING_UART3 + bool "Using UART3" + default n + config RT_USING_UART6 bool "Using UART6" default n diff --git a/bsp/stm32f4xx-HAL/drivers/drv_usart.c b/bsp/stm32f4xx-HAL/drivers/drv_usart.c index 7a58042a45..d065d853fc 100644 --- a/bsp/stm32f4xx-HAL/drivers/drv_usart.c +++ b/bsp/stm32f4xx-HAL/drivers/drv_usart.c @@ -193,8 +193,31 @@ void USART2_IRQHandler(void) } #endif /* RT_USING_UART2 */ +#if defined(RT_USING_UART3) +/* UART3 device driver structure */ +static struct drv_uart uart3; +struct rt_serial_device serial3; +void USART3_IRQHandler(void) +{ + struct drv_uart *uart; + uart = &uart3; + /* enter interrupt */ + rt_interrupt_enter(); + /* UART in mode Receiver -------------------------------------------------*/ + if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) && + (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET)) + { + rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND); + /* Clear RXNE interrupt flag */ + __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE); + } + /* leave interrupt */ + rt_interrupt_leave(); +} +#endif /* RT_USING_UART3 */ + #if defined(RT_USING_UART6) -/* UART2 device driver structure */ +/* UART6 device driver structure */ static struct drv_uart uart6; struct rt_serial_device serial6; void USART6_IRQHandler(void) @@ -214,7 +237,7 @@ void USART6_IRQHandler(void) /* leave interrupt */ rt_interrupt_leave(); } -#endif /* RT_USING_UART3 */ +#endif /* RT_USING_UART6 */ /** * @brief UART MSP Initialization @@ -260,6 +283,22 @@ void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle) GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } + else if (uartHandle->Instance == USART3) + { + /* USART3 clock enable */ + __HAL_RCC_USART3_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + /**USART3 GPIO Configuration + PB10 ------> USART3_TX + PB11 ------> USART3_RX + */ + GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; + GPIO_InitStruct.Alternate = GPIO_AF7_USART3; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + } else if (uartHandle->Instance == USART6) { /* USART6 clock enable */ @@ -300,6 +339,16 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle) */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3); } + else if (uartHandle->Instance == USART3) + { + /* Peripheral clock disable */ + __HAL_RCC_USART3_CLK_DISABLE(); + /**USART3 GPIO Configuration + PB10 ------> USART3_TX + PB11 ------> USART3_RX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11); + } else if (uartHandle->Instance == USART6) { /* Peripheral clock disable */ @@ -338,6 +387,17 @@ int hw_usart_init(void) RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); #endif /* RT_USING_UART2 */ +#ifdef RT_USING_UART3 + uart = &uart3; + uart->UartHandle.Instance = USART3; + uart->irq = USART3_IRQn; + serial3.ops = &drv_uart_ops; + serial3.config = config; + /* register UART3 device */ + rt_hw_serial_register(&serial3, "uart3", + RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + uart); +#endif /* RT_USING_UART3 */ #ifdef RT_USING_UART6 uart = &uart6; uart->UartHandle.Instance = USART6; @@ -348,7 +408,7 @@ int hw_usart_init(void) rt_hw_serial_register(&serial6, "uart6", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); -#endif /* RT_USING_UART2 */ +#endif /* RT_USING_UART6 */ return 0; } INIT_BOARD_EXPORT(hw_usart_init);