1 |
582 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
|
56 |
|
|
|
57 |
|
|
***Note*** This example uses queues to send each character into an interrupt
|
58 |
|
|
service routine and out of an interrupt service routine individually. This
|
59 |
|
|
is done to demonstrate queues being used in an interrupt, and to deliberately
|
60 |
|
|
load the system to test the FreeRTOS port. It is *NOT* meant to be an
|
61 |
|
|
example of an efficient implementation. An efficient implementation should
|
62 |
|
|
use FIFO's or DMA if available, and only use FreeRTOS API functions when
|
63 |
|
|
enough has been received to warrant a task being unblocked to process the
|
64 |
|
|
data.
|
65 |
|
|
*/
|
66 |
|
|
|
67 |
|
|
/* Scheduler includes. */
|
68 |
|
|
#include "FreeRTOS.h"
|
69 |
|
|
#include "queue.h"
|
70 |
|
|
#include "semphr.h"
|
71 |
|
|
#include "comtest2.h"
|
72 |
|
|
|
73 |
|
|
/* Library includes. */
|
74 |
|
|
#include "stm32l152_eval.h"
|
75 |
|
|
|
76 |
|
|
/* Demo application includes. */
|
77 |
|
|
#include "serial.h"
|
78 |
|
|
/*-----------------------------------------------------------*/
|
79 |
|
|
|
80 |
|
|
/* Misc defines. */
|
81 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
82 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
83 |
|
|
|
84 |
|
|
/*-----------------------------------------------------------*/
|
85 |
|
|
|
86 |
|
|
/* The queue used to hold received characters. */
|
87 |
|
|
static xQueueHandle xRxedChars;
|
88 |
|
|
static xQueueHandle xCharsForTx;
|
89 |
|
|
|
90 |
|
|
/*-----------------------------------------------------------*/
|
91 |
|
|
|
92 |
|
|
/*
|
93 |
|
|
* See the serial2.h header file.
|
94 |
|
|
*/
|
95 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
96 |
|
|
{
|
97 |
|
|
USART_InitTypeDef USART_InitStructure;
|
98 |
|
|
xComPortHandle xReturn;
|
99 |
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
100 |
|
|
|
101 |
|
|
/* Create the queues used to hold Rx/Tx characters. */
|
102 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
103 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
104 |
|
|
|
105 |
|
|
/* If the queues were created correctly then setup the serial port
|
106 |
|
|
hardware. */
|
107 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
108 |
|
|
{
|
109 |
|
|
USART_InitStructure.USART_BaudRate = ulWantedBaud;
|
110 |
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
111 |
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
112 |
|
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
113 |
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
114 |
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
115 |
|
|
|
116 |
|
|
/* The Eval board COM2 is being used, which in reality is the STM32
|
117 |
|
|
USART3. */
|
118 |
|
|
STM_EVAL_COMInit( COM2, &USART_InitStructure );
|
119 |
|
|
|
120 |
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
|
121 |
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY;
|
122 |
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; /* Not used as 4 bits are used for the pre-emption priority. */;
|
123 |
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
124 |
|
|
NVIC_Init( &NVIC_InitStructure );
|
125 |
|
|
USART_ITConfig( USART3, USART_IT_RXNE, ENABLE );
|
126 |
|
|
}
|
127 |
|
|
else
|
128 |
|
|
{
|
129 |
|
|
xReturn = ( xComPortHandle ) 0;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/* This demo file only supports a single port but we have to return
|
133 |
|
|
something to comply with the standard demo header file. */
|
134 |
|
|
return xReturn;
|
135 |
|
|
}
|
136 |
|
|
/*-----------------------------------------------------------*/
|
137 |
|
|
|
138 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
139 |
|
|
{
|
140 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
141 |
|
|
( void ) pxPort;
|
142 |
|
|
|
143 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
144 |
|
|
are available, or arrive before xBlockTime expires. */
|
145 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
146 |
|
|
{
|
147 |
|
|
return pdTRUE;
|
148 |
|
|
}
|
149 |
|
|
else
|
150 |
|
|
{
|
151 |
|
|
return pdFALSE;
|
152 |
|
|
}
|
153 |
|
|
}
|
154 |
|
|
/*-----------------------------------------------------------*/
|
155 |
|
|
|
156 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
|
157 |
|
|
{
|
158 |
|
|
signed portCHAR *pxNext;
|
159 |
|
|
|
160 |
|
|
/* A couple of parameters that this port does not use. */
|
161 |
|
|
( void ) usStringLength;
|
162 |
|
|
( void ) pxPort;
|
163 |
|
|
|
164 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
165 |
|
|
block time is used! */
|
166 |
|
|
|
167 |
|
|
/* The port handle is not required as this driver only supports UART1. */
|
168 |
|
|
( void ) pxPort;
|
169 |
|
|
|
170 |
|
|
/* Send each character in the string, one at a time. */
|
171 |
|
|
pxNext = ( signed portCHAR * ) pcString;
|
172 |
|
|
while( *pxNext )
|
173 |
|
|
{
|
174 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
175 |
|
|
pxNext++;
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
/*-----------------------------------------------------------*/
|
179 |
|
|
|
180 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
181 |
|
|
{
|
182 |
|
|
signed portBASE_TYPE xReturn;
|
183 |
|
|
|
184 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
|
185 |
|
|
{
|
186 |
|
|
xReturn = pdPASS;
|
187 |
|
|
USART_ITConfig( USART3, USART_IT_TXE, ENABLE );
|
188 |
|
|
}
|
189 |
|
|
else
|
190 |
|
|
{
|
191 |
|
|
xReturn = pdFAIL;
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
return xReturn;
|
195 |
|
|
}
|
196 |
|
|
/*-----------------------------------------------------------*/
|
197 |
|
|
|
198 |
|
|
void vSerialClose( xComPortHandle xPort )
|
199 |
|
|
{
|
200 |
|
|
/* Not supported as not required by the demo application. */
|
201 |
|
|
}
|
202 |
|
|
/*-----------------------------------------------------------*/
|
203 |
|
|
|
204 |
|
|
void USART3_IRQHandler( void )
|
205 |
|
|
{
|
206 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
207 |
|
|
portCHAR cChar;
|
208 |
|
|
|
209 |
|
|
if( USART_GetITStatus( USART3, USART_IT_TXE ) == SET )
|
210 |
|
|
{
|
211 |
|
|
/* The interrupt was caused by the TX register becoming empty. Are
|
212 |
|
|
there any more characters to transmit? */
|
213 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
214 |
|
|
{
|
215 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
216 |
|
|
USART now. */
|
217 |
|
|
USART_SendData( USART3, cChar );
|
218 |
|
|
}
|
219 |
|
|
else
|
220 |
|
|
{
|
221 |
|
|
USART_ITConfig( USART3, USART_IT_TXE, DISABLE );
|
222 |
|
|
}
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
if( USART_GetITStatus( USART3, USART_IT_RXNE ) == SET )
|
226 |
|
|
{
|
227 |
|
|
/* A character has been received on the USART, send it to the Rx
|
228 |
|
|
handler task. */
|
229 |
|
|
cChar = USART_ReceiveData( USART3 );
|
230 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/* If sending or receiving from a queue has caused a task to unblock, and
|
234 |
|
|
the unblocked task has a priority equal to or higher than the currently
|
235 |
|
|
running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
|
236 |
|
|
will have automatically been set to pdTRUE within the queue send or receive
|
237 |
|
|
function. portEND_SWITCHING_ISR() will then ensure that this ISR returns
|
238 |
|
|
directly to the higher priority unblocked task. */
|
239 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|