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