2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\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
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\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
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
80 /******************************************************************************
\r
81 *** NOTE: COM0 == USART1, COM1 == USART2
\r
82 ******************************************************************************/
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
91 /* Library includes. */
\r
92 #include "stm32f10x_lib.h"
\r
94 /* Driver includes. */
\r
95 #include "STM32_USART.h"
\r
96 /*-----------------------------------------------------------*/
\r
98 /* The number of COM ports that can be controlled at the same time. */
\r
99 #define serNUM_COM_PORTS ( 2 )
\r
101 /* Queues are used to hold characters that are waiting to be transmitted. This
\r
102 constant sets the maximum number of characters that can be contained in such a
\r
103 queue at any one time. */
\r
104 #define serTX_QUEUE_LEN ( 100 )
\r
106 /* Queues are used to hold characters that have been received but not yet
\r
107 processed. This constant sets the maximum number of characters that can be
\r
108 contained in such a queue. */
\r
109 #define serRX_QUEUE_LEN ( 100 )
\r
111 /* The maximum amount of time that calls to lSerialPutString() should wait for
\r
112 there to be space to post each character to the queue of characters waiting
\r
113 transmission. NOTE! This is the time to wait per character - not the time to
\r
114 wait for the entire string. */
\r
115 #define serPUT_STRING_CHAR_DELAY ( 5 / portTICK_RATE_MS )
\r
117 /*-----------------------------------------------------------*/
\r
119 /* References to the USART peripheral addresses themselves. */
\r
120 static USART_TypeDef * const xUARTS[ serNUM_COM_PORTS ] = { ( ( USART_TypeDef * ) USART1_BASE ), ( ( USART_TypeDef * ) USART2_BASE ) };
\r
122 /* Queues used to hold characters waiting to be transmitted - one queue per port. */
\r
123 static xQueueHandle xCharsForTx[ serNUM_COM_PORTS ] = { 0 };
\r
125 /* Queues holding received characters - one queue per port. */
\r
126 static xQueueHandle xRxedChars[ serNUM_COM_PORTS ] = { 0 };
\r
128 /*-----------------------------------------------------------*/
\r
130 /* UART interrupt handlers, as named in the vector table. */
\r
131 void USART1_IRQHandler( void );
\r
132 void USART2_IRQHandler( void );
\r
134 /*-----------------------------------------------------------*/
\r
137 * See header file for parameter descriptions.
\r
139 long lCOMPortInit( unsigned long ulPort, unsigned long ulWantedBaud )
\r
141 long lReturn = pdFAIL;
\r
142 USART_InitTypeDef USART_InitStructure;
\r
143 NVIC_InitTypeDef NVIC_InitStructure;
\r
144 GPIO_InitTypeDef GPIO_InitStructure;
\r
146 if( ulPort < serNUM_COM_PORTS )
\r
148 /* The common (not port dependent) part of the initialisation. */
\r
149 USART_InitStructure.USART_BaudRate = ulWantedBaud;
\r
150 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
\r
151 USART_InitStructure.USART_StopBits = USART_StopBits_1;
\r
152 USART_InitStructure.USART_Parity = USART_Parity_No;
\r
153 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
154 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
155 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
\r
156 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
157 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
160 /* Init the buffer structures with the buffer for the COM port being
\r
161 initialised, and perform any non-common initialisation necessary. This
\r
162 does not check to see if the COM port has already been initialised. */
\r
165 /* Create the queue of chars that are waiting to be sent to COM0. */
\r
166 xCharsForTx[ 0 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
\r
168 /* Create the queue used to hold characters received from COM0. */
\r
169 xRxedChars[ 0 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
\r
171 /* Enable COM0 clock - the ST libraries start numbering from UART1. */
\r
172 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
\r
174 /* Configure USART1 Rx (PA10) as input floating */
\r
175 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
\r
176 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
177 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
179 /* Configure USART1 Tx (PA9) as alternate function push-pull */
\r
180 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
\r
181 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
182 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
183 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
185 USART_Init( USART1, &USART_InitStructure );
\r
186 USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
\r
188 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
\r
189 NVIC_Init( &NVIC_InitStructure );
\r
191 USART_DMACmd( USART1, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
\r
192 USART_Cmd( USART1, ENABLE );
\r
194 /* Everything is ok. */
\r
197 else if( ulPort == 1 )
\r
199 /* Create the queue of chars that are waiting to be sent to COM1. */
\r
200 xCharsForTx[ 1 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
\r
202 /* Create the queue used to hold characters received from COM0. */
\r
203 xRxedChars[ 1 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
\r
205 /* Enable COM0 clock - the ST libraries start numbering from 1. */
\r
206 RCC_APB2PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE );
\r
208 /* Configure USART2 Rx (PA3) as input floating */
\r
209 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
\r
210 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
211 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
213 /* Configure USART2 Tx (PA2) as alternate function push-pull */
\r
214 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
\r
215 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
216 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
217 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
219 USART_Init( USART2, &USART_InitStructure );
\r
220 USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );
\r
222 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
\r
223 NVIC_Init( &NVIC_InitStructure );
\r
225 USART_DMACmd( USART2, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
\r
226 USART_Cmd( USART2, ENABLE );
\r
228 /* Everything is ok. */
\r
233 /* Nothing to do unless more than two ports are supported. */
\r
239 /*-----------------------------------------------------------*/
\r
241 signed long xSerialGetChar( long lPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
243 long lReturn = pdFAIL;
\r
245 if( lPort < serNUM_COM_PORTS )
\r
247 if( xQueueReceive( xRxedChars[ lPort ], pcRxedChar, xBlockTime ) == pdPASS )
\r
255 /*-----------------------------------------------------------*/
\r
257 long lSerialPutString( long lPort, const char * const pcString, unsigned long ulStringLength )
\r
262 if( lPort < serNUM_COM_PORTS )
\r
266 for( ul = 0; ul < ulStringLength; ul++ )
\r
268 if( xQueueSend( xCharsForTx[ lPort ], &( pcString[ ul ] ), serPUT_STRING_CHAR_DELAY ) != pdPASS )
\r
270 /* Cannot fit any more in the queue. Try turning the Tx on to
\r
271 clear some space. */
\r
272 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
273 vTaskDelay( serPUT_STRING_CHAR_DELAY );
\r
275 /* Go back and try again. */
\r
280 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
289 /*-----------------------------------------------------------*/
\r
291 signed long xSerialPutChar( long lPort, signed char cOutChar, portTickType xBlockTime )
\r
295 if( xQueueSend( xCharsForTx[ lPort ], &cOutChar, xBlockTime ) == pdPASS )
\r
298 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
307 /*-----------------------------------------------------------*/
\r
309 void USART1_IRQHandler( void )
\r
311 long xHigherPriorityTaskWoken = pdFALSE;
\r
314 if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )
\r
316 /* The interrupt was caused by the THR becoming empty. Are there any
\r
317 more characters to transmit? */
\r
318 if( xQueueReceiveFromISR( xCharsForTx[ 0 ], &cChar, &xHigherPriorityTaskWoken ) )
\r
320 /* A character was retrieved from the buffer so can be sent to the
\r
322 USART_SendData( USART1, cChar );
\r
326 USART_ITConfig( USART1, USART_IT_TXE, DISABLE );
\r
330 if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )
\r
332 cChar = USART_ReceiveData( USART1 );
\r
333 xQueueSendFromISR( xRxedChars[ 0 ], &cChar, &xHigherPriorityTaskWoken );
\r
336 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
338 /*-----------------------------------------------------------*/
\r
340 void USART2_IRQHandler( void )
\r