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