]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F103_GCC_Rowley/Drivers/STM32_USART.c
2d6fd92a8819b3ca5544408aad6fe16b5231fec1
[freertos] / FreeRTOS / Demo / CORTEX_STM32F103_GCC_Rowley / Drivers / STM32_USART.c
1 /*\r
2     FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67         INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
68 */\r
69 \r
70 \r
71 /******************************************************************************\r
72 *** NOTE:  COM0 == USART1, COM1 == USART2\r
73 ******************************************************************************/\r
74 \r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "queue.h"\r
80 #include "semphr.h"\r
81 \r
82 /* Library includes. */\r
83 #include "stm32f10x_lib.h"\r
84 \r
85 /* Driver includes. */\r
86 #include "STM32_USART.h"\r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* The number of COM ports that can be controlled at the same time. */\r
90 #define serNUM_COM_PORTS                                ( 2 )\r
91 \r
92 /* Queues are used to hold characters that are waiting to be transmitted.  This\r
93 constant sets the maximum number of characters that can be contained in such a\r
94 queue at any one time. */\r
95 #define serTX_QUEUE_LEN                                 ( 100 )\r
96 \r
97 /* Queues are used to hold characters that have been received but not yet \r
98 processed.  This constant sets the maximum number of characters that can be \r
99 contained in such a queue. */\r
100 #define serRX_QUEUE_LEN                                 ( 100 )\r
101 \r
102 /* The maximum amount of time that calls to lSerialPutString() should wait for\r
103 there to be space to post each character to the queue of characters waiting\r
104 transmission.  NOTE!  This is the time to wait per character - not the time to\r
105 wait for the entire string. */\r
106 #define serPUT_STRING_CHAR_DELAY                ( 5 / portTICK_PERIOD_MS )\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /* References to the USART peripheral addresses themselves. */\r
111 static USART_TypeDef * const xUARTS[ serNUM_COM_PORTS ] = { ( ( USART_TypeDef * ) USART1_BASE ), ( ( USART_TypeDef * ) USART2_BASE ) };\r
112 \r
113 /* Queues used to hold characters waiting to be transmitted - one queue per port. */\r
114 static QueueHandle_t xCharsForTx[ serNUM_COM_PORTS ] = { 0 };\r
115 \r
116 /* Queues holding received characters - one queue per port. */\r
117 static QueueHandle_t xRxedChars[ serNUM_COM_PORTS ] = { 0 };\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 /* UART interrupt handlers, as named in the vector table. */\r
122 void USART1_IRQHandler( void );\r
123 void USART2_IRQHandler( void );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * See header file for parameter descriptions.\r
129  */\r
130 long lCOMPortInit( unsigned long ulPort, unsigned long ulWantedBaud )\r
131 {\r
132 long lReturn = pdFAIL;\r
133 USART_InitTypeDef USART_InitStructure;\r
134 NVIC_InitTypeDef NVIC_InitStructure;\r
135 GPIO_InitTypeDef GPIO_InitStructure;\r
136 \r
137         if( ulPort < serNUM_COM_PORTS )\r
138         {\r
139                 /* The common (not port dependent) part of the initialisation. */\r
140                 USART_InitStructure.USART_BaudRate = ulWantedBaud;\r
141                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;\r
142                 USART_InitStructure.USART_StopBits = USART_StopBits_1;\r
143                 USART_InitStructure.USART_Parity = USART_Parity_No;\r
144                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;\r
145                 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;\r
146                 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;\r
147                 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;\r
148                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;\r
149 \r
150 \r
151                 /* Init the buffer structures with the buffer for the COM port being\r
152                 initialised, and perform any non-common initialisation necessary.  This\r
153                 does not check to see if the COM port has already been initialised. */\r
154                 if( ulPort == 0 )\r
155                 {\r
156                         /* Create the queue of chars that are waiting to be sent to COM0. */\r
157                         xCharsForTx[ 0 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );\r
158 \r
159                         /* Create the queue used to hold characters received from COM0. */\r
160                         xRxedChars[ 0 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );\r
161 \r
162                         /* Enable COM0 clock - the ST libraries start numbering from UART1. */\r
163                         RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE ); \r
164 \r
165                         /* Configure USART1 Rx (PA10) as input floating */\r
166                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;\r
167                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
168                         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
169                         \r
170                         /* Configure USART1 Tx (PA9) as alternate function push-pull */\r
171                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;\r
172                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
173                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
174                         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
175 \r
176                         USART_Init( USART1, &USART_InitStructure );             \r
177                         USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );\r
178                         \r
179                         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;\r
180                         NVIC_Init( &NVIC_InitStructure );\r
181                         \r
182             USART_DMACmd( USART1, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );\r
183                         USART_Cmd( USART1, ENABLE );    \r
184 \r
185                         /* Everything is ok. */\r
186                         lReturn = pdPASS;\r
187                 }\r
188                 else if( ulPort == 1 )\r
189                 {\r
190                         /* Create the queue of chars that are waiting to be sent to COM1. */\r
191                         xCharsForTx[ 1 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );\r
192 \r
193                         /* Create the queue used to hold characters received from COM0. */\r
194                         xRxedChars[ 1 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );\r
195 \r
196                         /* Enable COM0 clock - the ST libraries start numbering from 1. */\r
197                         RCC_APB2PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE ); \r
198 \r
199                         /* Configure USART2 Rx (PA3) as input floating */\r
200                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;\r
201                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;\r
202                         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
203                         \r
204                         /* Configure USART2 Tx (PA2) as alternate function push-pull */\r
205                         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;\r
206                         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;\r
207                         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;\r
208                         GPIO_Init( GPIOA, &GPIO_InitStructure );\r
209 \r
210                         USART_Init( USART2, &USART_InitStructure );             \r
211                         USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );\r
212                         \r
213                         NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;\r
214                         NVIC_Init( &NVIC_InitStructure );\r
215                         \r
216             USART_DMACmd( USART2, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );\r
217                         USART_Cmd( USART2, ENABLE );    \r
218 \r
219                         /* Everything is ok. */\r
220                         lReturn = pdPASS;\r
221                 }       \r
222                 else\r
223                 {\r
224                         /* Nothing to do unless more than two ports are supported. */\r
225                 }\r
226         }\r
227         \r
228         return lReturn;\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 signed long xSerialGetChar( long lPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
233 {\r
234 long lReturn = pdFAIL;\r
235 \r
236         if( lPort < serNUM_COM_PORTS ) \r
237         {\r
238                 if( xQueueReceive( xRxedChars[ lPort ], pcRxedChar, xBlockTime ) == pdPASS )\r
239                 {\r
240                         lReturn = pdPASS;\r
241                 }\r
242         }\r
243 \r
244         return lReturn;\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 long lSerialPutString( long lPort, const char * const pcString, unsigned long ulStringLength )\r
249 {\r
250 long lReturn;\r
251 unsigned long ul;\r
252 \r
253         if( lPort < serNUM_COM_PORTS )\r
254         {\r
255                 lReturn = pdPASS;\r
256 \r
257                 for( ul = 0; ul < ulStringLength; ul++ )\r
258                 {\r
259                         if( xQueueSend( xCharsForTx[ lPort ], &( pcString[ ul ] ), serPUT_STRING_CHAR_DELAY ) != pdPASS )\r
260                         {\r
261                                 /* Cannot fit any more in the queue.  Try turning the Tx on to \r
262                                 clear some space. */\r
263                                 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );\r
264                                 vTaskDelay( serPUT_STRING_CHAR_DELAY );\r
265 \r
266                                 /* Go back and try again. */\r
267                                 continue;\r
268                         }\r
269                 }\r
270 \r
271         USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );\r
272         }\r
273         else\r
274         {\r
275                 lReturn = pdFAIL;\r
276         }\r
277 \r
278         return lReturn;\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 signed long xSerialPutChar( long lPort, signed char cOutChar, TickType_t xBlockTime )\r
283 {\r
284 long lReturn;\r
285 \r
286         if( xQueueSend( xCharsForTx[ lPort ], &cOutChar, xBlockTime ) == pdPASS )\r
287         {\r
288                 lReturn = pdPASS;\r
289                 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );\r
290         }\r
291         else\r
292         {\r
293                 lReturn = pdFAIL;\r
294         }\r
295 \r
296         return lReturn;\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void USART1_IRQHandler( void )\r
301 {\r
302 long xHigherPriorityTaskWoken = pdFALSE;\r
303 char cChar;\r
304 \r
305         if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )\r
306         {\r
307                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
308                 more characters to transmit? */\r
309                 if( xQueueReceiveFromISR( xCharsForTx[ 0 ], &cChar, &xHigherPriorityTaskWoken ) )\r
310                 {\r
311                         /* A character was retrieved from the buffer so can be sent to the\r
312                         THR now. */\r
313                         USART_SendData( USART1, cChar );\r
314                 }\r
315                 else\r
316                 {\r
317                         USART_ITConfig( USART1, USART_IT_TXE, DISABLE );                \r
318                 }               \r
319         }\r
320         \r
321         if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )\r
322         {\r
323                 cChar = USART_ReceiveData( USART1 );\r
324                 xQueueSendFromISR( xRxedChars[ 0 ], &cChar, &xHigherPriorityTaskWoken );\r
325         }       \r
326         \r
327         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 void USART2_IRQHandler( void )\r
332 {\r
333 }\r
334 \r
335 \r
336 \r
337 \r
338         \r