12
12
* 2009-01-05 Bernard the first version
13
13
* 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
14
14
* 2013-05-13 aozima update for kehong-lingtai.
15
+ * 2015-01-31 armink make sure the serial transmit complete in putc()
15
16
*/
16
17
17
18
#include "stm32f10x.h"
18
19
#include "usart.h"
19
20
#include "board.h"
20
-
21
21
#include <rtdevice.h>
22
22
23
23
/* 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
27
27
28
28
/* 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
32
32
33
33
/* 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
37
37
38
38
/* STM32 uart driver */
39
39
struct stm32_uart
@@ -54,23 +54,32 @@ static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_c
54
54
55
55
USART_InitStructure .USART_BaudRate = cfg -> baud_rate ;
56
56
57
- if (cfg -> data_bits == DATA_BITS_8 )
57
+ if (cfg -> data_bits == DATA_BITS_8 ){
58
58
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
+ }
59
62
60
- if (cfg -> stop_bits == STOP_BITS_1 )
63
+ if (cfg -> stop_bits == STOP_BITS_1 ){
61
64
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 ){
63
66
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
+ }
64
76
65
- USART_InitStructure .USART_Parity = USART_Parity_No ;
66
77
USART_InitStructure .USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
67
78
USART_InitStructure .USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;
68
79
USART_Init (uart -> uart_device , & USART_InitStructure );
69
80
70
81
/* Enable USART */
71
82
USART_Cmd (uart -> uart_device , ENABLE );
72
- /* enable interrupt */
73
- USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
74
83
75
84
return RT_EOK ;
76
85
}
@@ -84,13 +93,19 @@ static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *ar
84
93
85
94
switch (cmd )
86
95
{
96
+ /* disable interrupt */
87
97
case RT_DEVICE_CTRL_CLR_INT :
88
98
/* disable rx irq */
89
99
UART_DISABLE_IRQ (uart -> irq );
100
+ /* disable interrupt */
101
+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , DISABLE );
90
102
break ;
103
+ /* enable interrupt */
91
104
case RT_DEVICE_CTRL_SET_INT :
92
105
/* enable rx irq */
93
106
UART_ENABLE_IRQ (uart -> irq );
107
+ /* enable interrupt */
108
+ USART_ITConfig (uart -> uart_device , USART_IT_RXNE , ENABLE );
94
109
break ;
95
110
}
96
111
@@ -104,8 +119,8 @@ static int stm32_putc(struct rt_serial_device *serial, char c)
104
119
RT_ASSERT (serial != RT_NULL );
105
120
uart = (struct stm32_uart * )serial -> parent .user_data ;
106
121
107
- while (!(uart -> uart_device -> SR & USART_FLAG_TXE ));
108
122
uart -> uart_device -> DR = c ;
123
+ while (!(uart -> uart_device -> SR & USART_FLAG_TC ));
109
124
110
125
return 1 ;
111
126
}
@@ -158,6 +173,7 @@ void USART1_IRQHandler(void)
158
173
/* clear interrupt */
159
174
USART_ClearITPendingBit (uart -> uart_device , USART_IT_RXNE );
160
175
}
176
+
161
177
if (USART_GetITStatus (uart -> uart_device , USART_IT_TC ) != RESET )
162
178
{
163
179
/* clear interrupt */
@@ -250,21 +266,21 @@ void USART3_IRQHandler(void)
250
266
251
267
static void RCC_Configuration (void )
252
268
{
253
- #ifdef RT_USING_UART1
269
+ #if defined( RT_USING_UART1 )
254
270
/* Enable UART GPIO clocks */
255
271
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
256
272
/* Enable UART clock */
257
273
RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 , ENABLE );
258
274
#endif /* RT_USING_UART1 */
259
275
260
- #ifdef RT_USING_UART2
276
+ #if defined( RT_USING_UART2 )
261
277
/* Enable UART GPIO clocks */
262
278
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA , ENABLE );
263
279
/* Enable UART clock */
264
280
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART2 , ENABLE );
265
281
#endif /* RT_USING_UART2 */
266
282
267
- #ifdef RT_USING_UART3
283
+ #if defined( RT_USING_UART3 )
268
284
/* Enable UART GPIO clocks */
269
285
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB , ENABLE );
270
286
/* Enable UART clock */
@@ -278,7 +294,7 @@ static void GPIO_Configuration(void)
278
294
279
295
GPIO_InitStructure .GPIO_Speed = GPIO_Speed_2MHz ;
280
296
281
- #ifdef RT_USING_UART1
297
+ #if defined( RT_USING_UART1 )
282
298
/* Configure USART Rx/tx PIN */
283
299
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
284
300
GPIO_InitStructure .GPIO_Pin = UART1_GPIO_RX ;
@@ -289,7 +305,7 @@ static void GPIO_Configuration(void)
289
305
GPIO_Init (UART1_GPIO , & GPIO_InitStructure );
290
306
#endif /* RT_USING_UART1 */
291
307
292
- #ifdef RT_USING_UART2
308
+ #if defined( RT_USING_UART2 )
293
309
/* Configure USART Rx/tx PIN */
294
310
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
295
311
GPIO_InitStructure .GPIO_Pin = UART2_GPIO_RX ;
@@ -300,7 +316,7 @@ static void GPIO_Configuration(void)
300
316
GPIO_Init (UART2_GPIO , & GPIO_InitStructure );
301
317
#endif /* RT_USING_UART2 */
302
318
303
- #ifdef RT_USING_UART3
319
+ #if defined( RT_USING_UART3 )
304
320
/* Configure USART Rx/tx PIN */
305
321
GPIO_InitStructure .GPIO_Mode = GPIO_Mode_IN_FLOATING ;
306
322
GPIO_InitStructure .GPIO_Pin = UART3_GPIO_RX ;
@@ -332,7 +348,7 @@ void rt_hw_usart_init(void)
332
348
RCC_Configuration ();
333
349
GPIO_Configuration ();
334
350
335
- #ifdef RT_USING_UART1
351
+ #if defined( RT_USING_UART1 )
336
352
uart = & uart1 ;
337
353
config .baud_rate = BAUD_RATE_115200 ;
338
354
@@ -343,11 +359,11 @@ void rt_hw_usart_init(void)
343
359
344
360
/* register UART1 device */
345
361
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 ,
347
363
uart );
348
364
#endif /* RT_USING_UART1 */
349
365
350
- #ifdef RT_USING_UART2
366
+ #if defined( RT_USING_UART2 )
351
367
uart = & uart2 ;
352
368
353
369
config .baud_rate = BAUD_RATE_115200 ;
@@ -362,7 +378,7 @@ void rt_hw_usart_init(void)
362
378
uart );
363
379
#endif /* RT_USING_UART2 */
364
380
365
- #ifdef RT_USING_UART3
381
+ #if defined( RT_USING_UART3 )
366
382
uart = & uart3 ;
367
383
368
384
config .baud_rate = BAUD_RATE_115200 ;
0 commit comments