2222#include <rtdevice.h>
2323
2424/* UART GPIO define. */
25- #define UART1_GPIO_TX GPIO_Pin_6
25+ #define UART1_GPIO_TX GPIO_Pin_6
2626#define UART1_TX_PIN_SOURCE GPIO_PinSource6
27- #define UART1_GPIO_RX GPIO_Pin_7
27+ #define UART1_GPIO_RX GPIO_Pin_7
2828#define UART1_RX_PIN_SOURCE GPIO_PinSource7
29- #define UART1_GPIO GPIOB
29+ #define UART1_GPIO GPIOB
3030#define UART1_GPIO_RCC RCC_AHB1Periph_GPIOB
31- #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
32- #define UART1_TX_DMA DMA1_Channel4
33- #define UART1_RX_DMA DMA1_Channel5
31+ #define RCC_APBPeriph_UART1 RCC_APB2Periph_USART1
32+ #define UART1_TX_DMA DMA1_Channel4
33+ #define UART1_RX_DMA DMA1_Channel5
3434
35- #define UART2_GPIO_TX GPIO_Pin_2
35+ #define UART2_GPIO_TX GPIO_Pin_2
3636#define UART2_TX_PIN_SOURCE GPIO_PinSource2
37- #define UART2_GPIO_RX GPIO_Pin_3
37+ #define UART2_GPIO_RX GPIO_Pin_3
3838#define UART2_RX_PIN_SOURCE GPIO_PinSource3
39- #define UART2_GPIO GPIOA
39+ #define UART2_GPIO GPIOA
4040#define UART2_GPIO_RCC RCC_AHB1Periph_GPIOA
41- #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
42- #define UART2_TX_DMA DMA1_Channel4
43- #define UART2_RX_DMA DMA1_Channel5
41+ #define RCC_APBPeriph_UART2 RCC_APB1Periph_USART2
42+ #define UART2_TX_DMA DMA1_Channel4
43+ #define UART2_RX_DMA DMA1_Channel5
4444
4545#define UART3_GPIO_TX GPIO_Pin_8
4646#define UART3_TX_PIN_SOURCE GPIO_PinSource8
5555/* STM32 uart driver */
5656struct stm32_uart
5757{
58- USART_TypeDef * uart_device ;
58+ USART_TypeDef * uart_device ;
5959 IRQn_Type irq ;
6060};
6161
6262static rt_err_t stm32_configure (struct rt_serial_device * serial , struct serial_configure * cfg )
6363{
64- struct stm32_uart * uart ;
64+ struct stm32_uart * uart ;
6565 USART_InitTypeDef USART_InitStructure ;
6666
6767 RT_ASSERT (serial != RT_NULL );
@@ -89,15 +89,13 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
8989
9090 /* Enable USART */
9191 USART_Cmd (uart -> uart_device , ENABLE );
92- /* enable interrupt */
93- USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
9492
9593 return RT_EOK ;
9694}
9795
9896static rt_err_t stm32_control (struct rt_serial_device * serial , int cmd , void * arg )
9997{
100- struct stm32_uart * uart ;
98+ struct stm32_uart * uart ;
10199
102100 RT_ASSERT (serial != RT_NULL );
103101 uart = (struct stm32_uart * )serial -> parent .user_data ;
@@ -107,10 +105,14 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
107105 case RT_DEVICE_CTRL_CLR_INT :
108106 /* disable rx irq */
109107 UART_DISABLE_IRQ (uart -> irq );
108+ /* disable interrupt */
109+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , DISABLE );
110110 break ;
111111 case RT_DEVICE_CTRL_SET_INT :
112112 /* enable rx irq */
113113 UART_ENABLE_IRQ (uart -> irq );
114+ /* enable interrupt */
115+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
114116 break ;
115117 }
116118
@@ -119,7 +121,7 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
119121
120122static int stm32_putc (struct rt_serial_device * serial , char c )
121123{
122- struct stm32_uart * uart ;
124+ struct stm32_uart * uart ;
123125
124126 RT_ASSERT (serial != RT_NULL );
125127 uart = (struct stm32_uart * )serial -> parent .user_data ;
@@ -133,7 +135,7 @@ static int stm32_putc(struct rt_serial_device *serial, char c)
133135static int stm32_getc (struct rt_serial_device * serial )
134136{
135137 int ch ;
136- struct stm32_uart * uart ;
138+ struct stm32_uart * uart ;
137139
138140 RT_ASSERT (serial != RT_NULL );
139141 uart = (struct stm32_uart * )serial -> parent .user_data ;
@@ -166,13 +168,13 @@ struct rt_serial_device serial1;
166168
167169void USART1_IRQHandler (void )
168170{
169- struct stm32_uart * uart ;
171+ struct stm32_uart * uart ;
170172
171173 uart = & uart1 ;
172174
173175 /* enter interrupt */
174176 rt_interrupt_enter ();
175- if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
177+ if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
176178 {
177179 rt_hw_serial_isr (& serial1 , RT_SERIAL_EVENT_RX_IND );
178180 /* clear interrupt */
@@ -200,13 +202,13 @@ struct rt_serial_device serial2;
200202
201203void USART2_IRQHandler (void )
202204{
203- struct stm32_uart * uart ;
205+ struct stm32_uart * uart ;
204206
205207 uart = & uart2 ;
206208
207209 /* enter interrupt */
208210 rt_interrupt_enter ();
209- if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
211+ if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
210212 {
211213 rt_hw_serial_isr (& serial2 , RT_SERIAL_EVENT_RX_IND );
212214 /* clear interrupt */
@@ -234,13 +236,13 @@ struct rt_serial_device serial3;
234236
235237void USART3_IRQHandler (void )
236238{
237- struct stm32_uart * uart ;
239+ struct stm32_uart * uart ;
238240
239241 uart = & uart3 ;
240242
241243 /* enter interrupt */
242244 rt_interrupt_enter ();
243- if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
245+ if (USART_GetITStatus (uart -> uart_device , USART_IT_RXNE ) != RESET )
244246 {
245247 rt_hw_serial_isr (& serial3 , RT_SERIAL_EVENT_RX_IND );
246248 /* clear interrupt */
@@ -321,7 +323,7 @@ static void GPIO_Configuration(void)
321323#endif /* RT_USING_UART3 */
322324}
323325
324- static void NVIC_Configuration (struct stm32_uart * uart )
326+ static void NVIC_Configuration (struct stm32_uart * uart )
325327{
326328 NVIC_InitTypeDef NVIC_InitStructure ;
327329
@@ -335,7 +337,7 @@ static void NVIC_Configuration(struct stm32_uart* uart)
335337
336338int stm32_hw_usart_init (void )
337339{
338- struct stm32_uart * uart ;
340+ struct stm32_uart * uart ;
339341 struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT ;
340342
341343 RCC_Configuration ();
0 commit comments