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