1 |
581 |
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 |
|
|
* The comms test Rx and Tx task and co-routine. See the comments at the top
|
56 |
|
|
* of main.c for full information.
|
57 |
|
|
*/
|
58 |
|
|
|
59 |
|
|
|
60 |
|
|
/* Scheduler include files. */
|
61 |
|
|
#include "FreeRTOS.h"
|
62 |
|
|
#include "task.h"
|
63 |
|
|
#include "queue.h"
|
64 |
|
|
#include "croutine.h"
|
65 |
|
|
|
66 |
|
|
/* Demo application include files. */
|
67 |
|
|
#include "partest.h"
|
68 |
|
|
|
69 |
|
|
/* Library include files. */
|
70 |
|
|
#include "DriverLib.h"
|
71 |
|
|
|
72 |
|
|
/* The LED's toggled by the various tasks. */
|
73 |
|
|
#define commsFAIL_LED ( 7 )
|
74 |
|
|
#define commsRX_LED ( 6 )
|
75 |
|
|
#define commsTX_LED ( 5 )
|
76 |
|
|
|
77 |
|
|
/* The length of the queue used to pass received characters to the Comms Rx
|
78 |
|
|
task. */
|
79 |
|
|
#define commsRX_QUEUE_LEN ( 5 )
|
80 |
|
|
|
81 |
|
|
/* The baud rate used by the UART comms tasks/co-routine. */
|
82 |
|
|
#define commsBAUD_RATE ( 57600 )
|
83 |
|
|
|
84 |
|
|
/* FIFO setting for the UART. The FIFO is not used to create a better test. */
|
85 |
|
|
#define commsFIFO_SET ( 0x10 )
|
86 |
|
|
|
87 |
|
|
/* The string that is transmitted on the UART contains sequentially the
|
88 |
|
|
characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */
|
89 |
|
|
#define commsFIRST_TX_CHAR '0'
|
90 |
|
|
#define commsLAST_TX_CHAR 'z'
|
91 |
|
|
|
92 |
|
|
/* Just used to walk through the program memory in order that some random data
|
93 |
|
|
can be generated. */
|
94 |
|
|
#define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )
|
95 |
|
|
#define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )
|
96 |
|
|
|
97 |
|
|
/* The time between transmissions of the string on UART 0. This is pseudo
|
98 |
|
|
random in order to generate a bit or randomness to when the interrupts occur.*/
|
99 |
|
|
#define commsMIN_TX_DELAY ( 40 / portTICK_RATE_MS )
|
100 |
|
|
#define commsMAX_TX_DELAY ( ( portTickType ) 0x7f )
|
101 |
|
|
#define commsOFFSET_TIME ( ( portTickType ) 3 )
|
102 |
|
|
|
103 |
|
|
/* The time the Comms Rx task should wait to receive a character. This should
|
104 |
|
|
be slightly longer than the time between transmissions. If we do not receive
|
105 |
|
|
a character after this time then there must be an error in the transmission or
|
106 |
|
|
the timing of the transmission. */
|
107 |
|
|
#define commsRX_DELAY ( commsMAX_TX_DELAY + 20 )
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
|
111 |
|
|
|
112 |
|
|
/* The queue used to pass characters out of the ISR. */
|
113 |
|
|
static xQueueHandle xCommsQueue;
|
114 |
|
|
|
115 |
|
|
/* The next character to transmit. */
|
116 |
|
|
static char cNextChar;
|
117 |
|
|
|
118 |
|
|
/*-----------------------------------------------------------*/
|
119 |
|
|
|
120 |
|
|
void vSerialInit( void )
|
121 |
|
|
{
|
122 |
|
|
/* Create the queue used to communicate between the UART ISR and the Comms
|
123 |
|
|
Rx task. */
|
124 |
|
|
xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
|
125 |
|
|
|
126 |
|
|
/* Enable the UART. GPIOA has already been initialised. */
|
127 |
|
|
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
|
128 |
|
|
|
129 |
|
|
/* Set GPIO A0 and A1 as peripheral function. They are used to output the
|
130 |
|
|
UART signals. */
|
131 |
|
|
GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
|
132 |
|
|
|
133 |
|
|
/* Configure the UART for 8-N-1 operation. */
|
134 |
|
|
UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );
|
135 |
|
|
|
136 |
|
|
/* We dont want to use the fifo. This is for test purposes to generate
|
137 |
|
|
as many interrupts as possible. */
|
138 |
|
|
HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;
|
139 |
|
|
|
140 |
|
|
/* Enable both Rx and Tx interrupts. */
|
141 |
|
|
HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );
|
142 |
|
|
IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );
|
143 |
|
|
IntEnable( INT_UART0 );
|
144 |
|
|
}
|
145 |
|
|
/*-----------------------------------------------------------*/
|
146 |
|
|
|
147 |
|
|
void vSerialTxCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
|
148 |
|
|
{
|
149 |
|
|
portTickType xDelayPeriod;
|
150 |
|
|
static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
|
151 |
|
|
|
152 |
|
|
/* Co-routine MUST start with a call to crSTART. */
|
153 |
|
|
crSTART( xHandle );
|
154 |
|
|
|
155 |
|
|
for(;;)
|
156 |
|
|
{
|
157 |
|
|
/* Was the previously transmitted string received correctly? */
|
158 |
|
|
if( uxCommsErrorStatus != pdPASS )
|
159 |
|
|
{
|
160 |
|
|
/* An error was encountered so set the error LED. */
|
161 |
|
|
vParTestSetLED( commsFAIL_LED, pdTRUE );
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
/* The next character to Tx is the first in the string. */
|
165 |
|
|
cNextChar = commsFIRST_TX_CHAR;
|
166 |
|
|
|
167 |
|
|
UARTIntDisable( UART0_BASE, UART_INT_TX );
|
168 |
|
|
{
|
169 |
|
|
/* Send the first character. */
|
170 |
|
|
if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
|
171 |
|
|
{
|
172 |
|
|
HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
/* Move the variable to the char to Tx on so the ISR transmits
|
176 |
|
|
the next character in the string once this one has completed. */
|
177 |
|
|
cNextChar++;
|
178 |
|
|
}
|
179 |
|
|
UARTIntEnable(UART0_BASE, UART_INT_TX);
|
180 |
|
|
|
181 |
|
|
/* Toggle the LED to show a new string is being transmitted. */
|
182 |
|
|
vParTestToggleLED( commsTX_LED );
|
183 |
|
|
|
184 |
|
|
/* Delay before we start the string off again. A pseudo-random delay
|
185 |
|
|
is used as this will provide a better test. */
|
186 |
|
|
xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );
|
187 |
|
|
|
188 |
|
|
pulRandomBytes++;
|
189 |
|
|
if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
|
190 |
|
|
{
|
191 |
|
|
pulRandomBytes = commsFIRST_PROGRAM_BYTES;
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
/* Make sure we don't wait too long... */
|
195 |
|
|
xDelayPeriod &= commsMAX_TX_DELAY;
|
196 |
|
|
|
197 |
|
|
/* ...but we do want to wait. */
|
198 |
|
|
if( xDelayPeriod < commsMIN_TX_DELAY )
|
199 |
|
|
{
|
200 |
|
|
xDelayPeriod = commsMIN_TX_DELAY;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/* Block for the random(ish) time. */
|
204 |
|
|
crDELAY( xHandle, xDelayPeriod );
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
/* Co-routine MUST end with a call to crEND. */
|
208 |
|
|
crEND();
|
209 |
|
|
}
|
210 |
|
|
/*-----------------------------------------------------------*/
|
211 |
|
|
|
212 |
|
|
void vUART_ISR( void )
|
213 |
|
|
{
|
214 |
|
|
unsigned long ulStatus;
|
215 |
|
|
char cRxedChar;
|
216 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
217 |
|
|
|
218 |
|
|
/* What caused the interrupt. */
|
219 |
|
|
ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
|
220 |
|
|
|
221 |
|
|
/* Clear the interrupt. */
|
222 |
|
|
UARTIntClear( UART0_BASE, ulStatus );
|
223 |
|
|
|
224 |
|
|
/* Was an Rx interrpt pending? */
|
225 |
|
|
if( ulStatus & UART_INT_RX )
|
226 |
|
|
{
|
227 |
|
|
if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
|
228 |
|
|
{
|
229 |
|
|
/* Get the char from the buffer and post it onto the queue of
|
230 |
|
|
Rxed chars. Posting the character should wake the task that is
|
231 |
|
|
blocked on the queue waiting for characters. */
|
232 |
|
|
cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );
|
233 |
|
|
xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );
|
234 |
|
|
}
|
235 |
|
|
}
|
236 |
|
|
|
237 |
|
|
/* Was a Tx interrupt pending? */
|
238 |
|
|
if( ulStatus & UART_INT_TX )
|
239 |
|
|
{
|
240 |
|
|
/* Send the next character in the string. We are not using the FIFO. */
|
241 |
|
|
if( cNextChar <= commsLAST_TX_CHAR )
|
242 |
|
|
{
|
243 |
|
|
if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
|
244 |
|
|
{
|
245 |
|
|
HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
|
246 |
|
|
}
|
247 |
|
|
cNextChar++;
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
/* If a task was woken by the character being received then we force
|
252 |
|
|
a context switch to occur in case the task is of higher priority than
|
253 |
|
|
the currently executing task (i.e. the task that this interrupt
|
254 |
|
|
interrupted.) */
|
255 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
256 |
|
|
}
|
257 |
|
|
/*-----------------------------------------------------------*/
|
258 |
|
|
|
259 |
|
|
void vCommsRxTask( void * pvParameters )
|
260 |
|
|
{
|
261 |
|
|
static char cRxedChar, cExpectedChar;
|
262 |
|
|
|
263 |
|
|
/* Set the char we expect to receive to the start of the string. */
|
264 |
|
|
cExpectedChar = commsFIRST_TX_CHAR;
|
265 |
|
|
|
266 |
|
|
for( ;; )
|
267 |
|
|
{
|
268 |
|
|
/* Wait for a character to be received. */
|
269 |
|
|
xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
|
270 |
|
|
|
271 |
|
|
/* Was the character recived (if any) the expected character. */
|
272 |
|
|
if( cRxedChar != cExpectedChar )
|
273 |
|
|
{
|
274 |
|
|
/* Got an unexpected character. This can sometimes occur when
|
275 |
|
|
reseting the system using the debugger leaving characters already
|
276 |
|
|
in the UART regsters. */
|
277 |
|
|
uxCommsErrorStatus = pdFAIL;
|
278 |
|
|
|
279 |
|
|
/* Resync by waiting for the end of the current string. */
|
280 |
|
|
while( cRxedChar != commsLAST_TX_CHAR )
|
281 |
|
|
{
|
282 |
|
|
while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/* The next expected character is the start of the string again. */
|
286 |
|
|
cExpectedChar = commsFIRST_TX_CHAR;
|
287 |
|
|
}
|
288 |
|
|
else
|
289 |
|
|
{
|
290 |
|
|
if( cExpectedChar == commsLAST_TX_CHAR )
|
291 |
|
|
{
|
292 |
|
|
/* We have reached the end of the string - we now expect to
|
293 |
|
|
receive the first character in the string again. The LED is
|
294 |
|
|
toggled to indicate that the entire string was received without
|
295 |
|
|
error. */
|
296 |
|
|
vParTestToggleLED( commsRX_LED );
|
297 |
|
|
cExpectedChar = commsFIRST_TX_CHAR;
|
298 |
|
|
}
|
299 |
|
|
else
|
300 |
|
|
{
|
301 |
|
|
/* We got the expected character, we now expect to receive the
|
302 |
|
|
next character in the string. */
|
303 |
|
|
cExpectedChar++;
|
304 |
|
|
}
|
305 |
|
|
}
|
306 |
|
|
}
|
307 |
|
|
}
|
308 |
|
|
/*-----------------------------------------------------------*/
|
309 |
|
|
|
310 |
|
|
unsigned portBASE_TYPE uxGetCommsStatus( void )
|
311 |
|
|
{
|
312 |
|
|
return uxCommsErrorStatus;
|
313 |
|
|
}
|