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