1717#include "stm32f10x.h"
1818#include "usart.h"
1919#include "board.h"
20-
2120#include <rtdevice.h>
2221
2322/* USART1 */
24- #define UART1_GPIO_TX GPIO_Pin_9
25- #define UART1_GPIO_RX GPIO_Pin_10
26- #define UART1_GPIO GPIOA
23+ #define UART1_GPIO_TX GPIO_Pin_9
24+ #define UART1_GPIO_RX GPIO_Pin_10
25+ #define UART1_GPIO GPIOA
2726
2827/* USART2 */
29- #define UART2_GPIO_TX GPIO_Pin_2
30- #define UART2_GPIO_RX GPIO_Pin_3
31- #define UART2_GPIO GPIOA
28+ #define UART2_GPIO_TX GPIO_Pin_2
29+ #define UART2_GPIO_RX GPIO_Pin_3
30+ #define UART2_GPIO GPIOA
3231
3332/* USART3_REMAP[1:0] = 00 */
34- #define UART3_GPIO_TX GPIO_Pin_10
35- #define UART3_GPIO_RX GPIO_Pin_11
36- #define UART3_GPIO GPIOB
33+ #define UART3_GPIO_TX GPIO_Pin_10
34+ #define UART3_GPIO_RX GPIO_Pin_11
35+ #define UART3_GPIO GPIOB
3736
3837/* STM32 uart driver */
3938struct stm32_uart
@@ -54,15 +53,26 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
5453
5554 USART_InitStructure .USART_BaudRate = cfg -> baud_rate ;
5655
57- if (cfg -> data_bits == DATA_BITS_8 )
56+ if (cfg -> data_bits == DATA_BITS_8 ){
5857 USART_InitStructure .USART_WordLength = USART_WordLength_8b ;
58+ } else if (cfg -> data_bits == DATA_BITS_9 ) {
59+ USART_InitStructure .USART_WordLength = USART_WordLength_9b ;
60+ }
5961
60- if (cfg -> stop_bits == STOP_BITS_1 )
62+ if (cfg -> stop_bits == STOP_BITS_1 ){
6163 USART_InitStructure .USART_StopBits = USART_StopBits_1 ;
62- else if (cfg -> stop_bits == STOP_BITS_2 )
64+ } else if (cfg -> stop_bits == STOP_BITS_2 ){
6365 USART_InitStructure .USART_StopBits = USART_StopBits_2 ;
66+ }
67+
68+ if (cfg -> parity == PARITY_NONE ){
69+ USART_InitStructure .USART_Parity = USART_Parity_No ;
70+ } else if (cfg -> parity == PARITY_ODD ) {
71+ USART_InitStructure .USART_Parity = USART_Parity_Odd ;
72+ } else if (cfg -> parity == PARITY_EVEN ) {
73+ USART_InitStructure .USART_Parity = USART_Parity_Even ;
74+ }
6475
65- USART_InitStructure .USART_Parity = USART_Parity_No ;
6676 USART_InitStructure .USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
6777 USART_InitStructure .USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;
6878 USART_Init (uart -> uart_device , & USART_InitStructure );
@@ -162,6 +172,7 @@ void USART1_IRQHandler(void)
162172 /* clear interrupt */
163173 USART_ClearITPendingBit (uart -> uart_device , USART_IT_RXNE );
164174 }
175+
165176 if (USART_GetITStatus (uart -> uart_device , USART_IT_TC ) != RESET )
166177 {
167178 /* clear interrupt */
@@ -254,21 +265,21 @@ void USART3_IRQHandler(void)
254265
255266static void RCC_Configuration (void )
256267{
257- #ifdef RT_USING_UART1
268+ #if defined( RT_USING_UART1 )
258269 /* Enable UART GPIO clocks */
259270 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
260271 /* Enable UART clock */
261272 RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 , ENABLE );
262273#endif /* RT_USING_UART1 */
263274
264- #ifdef RT_USING_UART2
275+ #if defined( RT_USING_UART2 )
265276 /* Enable UART GPIO clocks */
266277 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
267278 /* Enable UART clock */
268279 RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2 , ENABLE );
269280#endif /* RT_USING_UART2 */
270281
271- #ifdef RT_USING_UART3
282+ #if defined( RT_USING_UART3 )
272283 /* Enable UART GPIO clocks */
273284 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB , ENABLE );
274285 /* Enable UART clock */
@@ -282,7 +293,7 @@ static void GPIO_Configuration(void)
282293
283294 GPIO_InitStructure .GPIO_Speed = GPIO_Speed_2MHz ;
284295
285- #ifdef RT_USING_UART1
296+ #if defined( RT_USING_UART1 )
286297 /* Configure USART Rx/tx PIN */
287298 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
288299 GPIO_InitStructure .GPIO_Pin = UART1_GPIO_RX ;
@@ -293,7 +304,7 @@ static void GPIO_Configuration(void)
293304 GPIO_Init (UART1_GPIO , & GPIO_InitStructure );
294305#endif /* RT_USING_UART1 */
295306
296- #ifdef RT_USING_UART2
307+ #if defined( RT_USING_UART2 )
297308 /* Configure USART Rx/tx PIN */
298309 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
299310 GPIO_InitStructure .GPIO_Pin = UART2_GPIO_RX ;
@@ -304,7 +315,7 @@ static void GPIO_Configuration(void)
304315 GPIO_Init (UART2_GPIO , & GPIO_InitStructure );
305316#endif /* RT_USING_UART2 */
306317
307- #ifdef RT_USING_UART3
318+ #if defined( RT_USING_UART3 )
308319 /* Configure USART Rx/tx PIN */
309320 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
310321 GPIO_InitStructure .GPIO_Pin = UART3_GPIO_RX ;
@@ -336,7 +347,7 @@ void rt_hw_usart_init(void)
336347 RCC_Configuration ();
337348 GPIO_Configuration ();
338349
339- #ifdef RT_USING_UART1
350+ #if defined( RT_USING_UART1 )
340351 uart = & uart1 ;
341352 config .baud_rate = BAUD_RATE_115200 ;
342353
@@ -347,11 +358,11 @@ void rt_hw_usart_init(void)
347358
348359 /* register UART1 device */
349360 rt_hw_serial_register (& serial1 , "uart1" ,
350- RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
361+ RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
351362 uart );
352363#endif /* RT_USING_UART1 */
353364
354- #ifdef RT_USING_UART2
365+ #if defined( RT_USING_UART2 )
355366 uart = & uart2 ;
356367
357368 config .baud_rate = BAUD_RATE_115200 ;
@@ -366,7 +377,7 @@ void rt_hw_usart_init(void)
366377 uart );
367378#endif /* RT_USING_UART2 */
368379
369- #ifdef RT_USING_UART3
380+ #if defined( RT_USING_UART3 )
370381 uart = & uart3 ;
371382
372383 config .baud_rate = BAUD_RATE_115200 ;
0 commit comments