1212 * 2009-01-05 Bernard the first version
1313 * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
1414 * 2013-05-13 aozima update for kehong-lingtai.
15+ * 2015-01-31 armink make sure the serial transmit complete in putc()
1516 */
1617
1718#include "stm32f10x.h"
1819#include "usart.h"
1920#include "board.h"
20-
2121#include <rtdevice.h>
2222
2323/* USART1 */
24- #define UART1_GPIO_TX GPIO_Pin_9
25- #define UART1_GPIO_RX GPIO_Pin_10
26- #define UART1_GPIO GPIOA
24+ #define UART1_GPIO_TX GPIO_Pin_9
25+ #define UART1_GPIO_RX GPIO_Pin_10
26+ #define UART1_GPIO GPIOA
2727
2828/* USART2 */
29- #define UART2_GPIO_TX GPIO_Pin_2
30- #define UART2_GPIO_RX GPIO_Pin_3
31- #define UART2_GPIO GPIOA
29+ #define UART2_GPIO_TX GPIO_Pin_2
30+ #define UART2_GPIO_RX GPIO_Pin_3
31+ #define UART2_GPIO GPIOA
3232
3333/* 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
34+ #define UART3_GPIO_TX GPIO_Pin_10
35+ #define UART3_GPIO_RX GPIO_Pin_11
36+ #define UART3_GPIO GPIOB
3737
3838/* STM32 uart driver */
3939struct stm32_uart
@@ -54,23 +54,32 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
5454
5555 USART_InitStructure .USART_BaudRate = cfg -> baud_rate ;
5656
57- if (cfg -> data_bits == DATA_BITS_8 )
57+ if (cfg -> data_bits == DATA_BITS_8 ){
5858 USART_InitStructure .USART_WordLength = USART_WordLength_8b ;
59+ } else if (cfg -> data_bits == DATA_BITS_9 ) {
60+ USART_InitStructure .USART_WordLength = USART_WordLength_9b ;
61+ }
5962
60- if (cfg -> stop_bits == STOP_BITS_1 )
63+ if (cfg -> stop_bits == STOP_BITS_1 ){
6164 USART_InitStructure .USART_StopBits = USART_StopBits_1 ;
62- else if (cfg -> stop_bits == STOP_BITS_2 )
65+ } else if (cfg -> stop_bits == STOP_BITS_2 ){
6366 USART_InitStructure .USART_StopBits = USART_StopBits_2 ;
67+ }
68+
69+ if (cfg -> parity == PARITY_NONE ){
70+ USART_InitStructure .USART_Parity = USART_Parity_No ;
71+ } else if (cfg -> parity == PARITY_ODD ) {
72+ USART_InitStructure .USART_Parity = USART_Parity_Odd ;
73+ } else if (cfg -> parity == PARITY_EVEN ) {
74+ USART_InitStructure .USART_Parity = USART_Parity_Even ;
75+ }
6476
65- USART_InitStructure .USART_Parity = USART_Parity_No ;
6677 USART_InitStructure .USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
6778 USART_InitStructure .USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;
6879 USART_Init (uart -> uart_device , & USART_InitStructure );
6980
7081 /* Enable USART */
7182 USART_Cmd (uart -> uart_device , ENABLE );
72- /* enable interrupt */
73- USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
7483
7584 return RT_EOK ;
7685}
@@ -84,13 +93,19 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
8493
8594 switch (cmd )
8695 {
96+ /* disable interrupt */
8797 case RT_DEVICE_CTRL_CLR_INT :
8898 /* disable rx irq */
8999 UART_DISABLE_IRQ (uart -> irq );
100+ /* disable interrupt */
101+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , DISABLE );
90102 break ;
103+ /* enable interrupt */
91104 case RT_DEVICE_CTRL_SET_INT :
92105 /* enable rx irq */
93106 UART_ENABLE_IRQ (uart -> irq );
107+ /* enable interrupt */
108+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
94109 break ;
95110 }
96111
@@ -104,8 +119,8 @@ static int stm32_putc(struct rt_serial_device *serial, char c)
104119 RT_ASSERT (serial != RT_NULL );
105120 uart = (struct stm32_uart * )serial -> parent .user_data ;
106121
107- while (!(uart -> uart_device -> SR & USART_FLAG_TXE ));
108122 uart -> uart_device -> DR = c ;
123+ while (!(uart -> uart_device -> SR & USART_FLAG_TC ));
109124
110125 return 1 ;
111126}
@@ -158,6 +173,7 @@ void USART1_IRQHandler(void)
158173 /* clear interrupt */
159174 USART_ClearITPendingBit (uart -> uart_device , USART_IT_RXNE );
160175 }
176+
161177 if (USART_GetITStatus (uart -> uart_device , USART_IT_TC ) != RESET )
162178 {
163179 /* clear interrupt */
@@ -250,21 +266,21 @@ void USART3_IRQHandler(void)
250266
251267static void RCC_Configuration (void )
252268{
253- #ifdef RT_USING_UART1
269+ #if defined( RT_USING_UART1 )
254270 /* Enable UART GPIO clocks */
255271 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
256272 /* Enable UART clock */
257273 RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 , ENABLE );
258274#endif /* RT_USING_UART1 */
259275
260- #ifdef RT_USING_UART2
276+ #if defined( RT_USING_UART2 )
261277 /* Enable UART GPIO clocks */
262278 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
263279 /* Enable UART clock */
264280 RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2 , ENABLE );
265281#endif /* RT_USING_UART2 */
266282
267- #ifdef RT_USING_UART3
283+ #if defined( RT_USING_UART3 )
268284 /* Enable UART GPIO clocks */
269285 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB , ENABLE );
270286 /* Enable UART clock */
@@ -278,7 +294,7 @@ static void GPIO_Configuration(void)
278294
279295 GPIO_InitStructure .GPIO_Speed = GPIO_Speed_2MHz ;
280296
281- #ifdef RT_USING_UART1
297+ #if defined( RT_USING_UART1 )
282298 /* Configure USART Rx/tx PIN */
283299 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
284300 GPIO_InitStructure .GPIO_Pin = UART1_GPIO_RX ;
@@ -289,7 +305,7 @@ static void GPIO_Configuration(void)
289305 GPIO_Init (UART1_GPIO , & GPIO_InitStructure );
290306#endif /* RT_USING_UART1 */
291307
292- #ifdef RT_USING_UART2
308+ #if defined( RT_USING_UART2 )
293309 /* Configure USART Rx/tx PIN */
294310 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
295311 GPIO_InitStructure .GPIO_Pin = UART2_GPIO_RX ;
@@ -300,7 +316,7 @@ static void GPIO_Configuration(void)
300316 GPIO_Init (UART2_GPIO , & GPIO_InitStructure );
301317#endif /* RT_USING_UART2 */
302318
303- #ifdef RT_USING_UART3
319+ #if defined( RT_USING_UART3 )
304320 /* Configure USART Rx/tx PIN */
305321 GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
306322 GPIO_InitStructure .GPIO_Pin = UART3_GPIO_RX ;
@@ -332,7 +348,7 @@ void rt_hw_usart_init(void)
332348 RCC_Configuration ();
333349 GPIO_Configuration ();
334350
335- #ifdef RT_USING_UART1
351+ #if defined( RT_USING_UART1 )
336352 uart = & uart1 ;
337353 config .baud_rate = BAUD_RATE_115200 ;
338354
@@ -343,11 +359,11 @@ void rt_hw_usart_init(void)
343359
344360 /* register UART1 device */
345361 rt_hw_serial_register (& serial1 , "uart1" ,
346- RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
362+ RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX ,
347363 uart );
348364#endif /* RT_USING_UART1 */
349365
350- #ifdef RT_USING_UART2
366+ #if defined( RT_USING_UART2 )
351367 uart = & uart2 ;
352368
353369 config .baud_rate = BAUD_RATE_115200 ;
@@ -362,7 +378,7 @@ void rt_hw_usart_init(void)
362378 uart );
363379#endif /* RT_USING_UART2 */
364380
365- #ifdef RT_USING_UART3
381+ #if defined( RT_USING_UART3 )
366382 uart = & uart3 ;
367383
368384 config .baud_rate = BAUD_RATE_115200 ;
0 commit comments