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