1 |
614 |
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 |
|
|
* Creates all the demo application tasks, then starts the scheduler. The WEB
|
56 |
|
|
* documentation provides more details of the standard demo application tasks.
|
57 |
|
|
* In addition to the standard demo tasks, the following tasks and tests are
|
58 |
|
|
* defined and/or created within this file:
|
59 |
|
|
*
|
60 |
|
|
* "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
|
61 |
|
|
* is permitted to access the display directly. Other tasks wishing to write a
|
62 |
|
|
* message to the LCD send the message on a queue to the LCD task instead of
|
63 |
|
|
* accessing the LCD themselves. The LCD task just blocks on the queue waiting
|
64 |
|
|
* for messages - waking and displaying the messages as they arrive.
|
65 |
|
|
*
|
66 |
|
|
* "Check" task - This only executes every three seconds but has the highest
|
67 |
|
|
* priority so is guaranteed to get processor time. Its main function is to
|
68 |
|
|
* check that all the standard demo tasks are still operational. Should any
|
69 |
|
|
* unexpected behaviour within a demo task be discovered the check task will
|
70 |
|
|
* write an error to the LCD (via the LCD task). If all the demo tasks are
|
71 |
|
|
* executing with their expected behaviour then the check task instead writes
|
72 |
|
|
* a count of the number of times the high frequency interrupt has incremented
|
73 |
|
|
* ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.
|
74 |
|
|
*
|
75 |
|
|
* "Register test" tasks - These tasks are used in part to test the kernel port.
|
76 |
|
|
* They set each processor register to a known value, then check that the
|
77 |
|
|
* register still contains that value. Each of the tasks sets the registers
|
78 |
|
|
* to different values, and will get swapping in and out between setting and
|
79 |
|
|
* then subsequently checking the register values. Discovery of an incorrect
|
80 |
|
|
* value would be indicative of an error in the task switching mechanism.
|
81 |
|
|
*
|
82 |
|
|
* By way of demonstration, the demo application defines
|
83 |
|
|
* configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY
|
84 |
|
|
* to be 1, and all other interrupts as follows:
|
85 |
|
|
*
|
86 |
|
|
* + The UART is allocated a priority of 2. This means it can interrupt the
|
87 |
|
|
* RTOS tick, and can also safely use queues.
|
88 |
|
|
* + Two timers are configured to generate interrupts just to test the nesting
|
89 |
|
|
* and queue access mechanisms. These timers are allocated priorities 2 and 3
|
90 |
|
|
* respectively. Even though they both access the same two queues, the
|
91 |
|
|
* priority 3 interrupt can safely interrupt the priority 2 interrupt. Both
|
92 |
|
|
* can interrupt the RTOS tick.
|
93 |
|
|
* + Finally a high frequency timer interrupt is configured to use priority 4 -
|
94 |
|
|
* therefore kernel activity will never prevent the high frequency timer from
|
95 |
|
|
* executing immediately that the interrupt is raised (within the limitations
|
96 |
|
|
* of the hardware itself). It would not be safe to access a queue from this
|
97 |
|
|
* interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.
|
98 |
|
|
*
|
99 |
|
|
* See the online documentation for this demo for more information on interrupt
|
100 |
|
|
* usage.
|
101 |
|
|
*/
|
102 |
|
|
|
103 |
|
|
/* Standard includes. */
|
104 |
|
|
#include <stdio.h>
|
105 |
|
|
|
106 |
|
|
/* Scheduler includes. */
|
107 |
|
|
#include "FreeRTOS.h"
|
108 |
|
|
#include "task.h"
|
109 |
|
|
#include "queue.h"
|
110 |
|
|
|
111 |
|
|
/* Demo application includes. */
|
112 |
|
|
#include "partest.h"
|
113 |
|
|
#include "blocktim.h"
|
114 |
|
|
#include "flash.h"
|
115 |
|
|
#include "semtest.h"
|
116 |
|
|
#include "GenQTest.h"
|
117 |
|
|
#include "QPeek.h"
|
118 |
|
|
#include "lcd.h"
|
119 |
|
|
#include "comtest2.h"
|
120 |
|
|
#include "timertest.h"
|
121 |
|
|
#include "IntQueue.h"
|
122 |
|
|
|
123 |
|
|
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
|
124 |
|
|
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
|
125 |
|
|
|
126 |
|
|
/*-----------------------------------------------------------*/
|
127 |
|
|
|
128 |
|
|
/* The rate at which the LED controlled by the 'check' task will flash when no
|
129 |
|
|
errors have been detected. */
|
130 |
|
|
#define mainNO_ERROR_PERIOD ( 3000 / portTICK_RATE_MS )
|
131 |
|
|
|
132 |
|
|
/* The rate at which the LED controlled by the 'check' task will flash when an
|
133 |
|
|
error has been detected. */
|
134 |
|
|
#define mainERROR_PERIOD ( 500 / portTICK_RATE_MS )
|
135 |
|
|
|
136 |
|
|
/* The priorities of the various demo application tasks. */
|
137 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
|
138 |
|
|
#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
139 |
|
|
#define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
140 |
|
|
#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
141 |
|
|
#define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
|
142 |
|
|
#define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
|
143 |
|
|
|
144 |
|
|
/* The LED controlled by the 'check' task. */
|
145 |
|
|
#define mainCHECK_LED ( 7 )
|
146 |
|
|
|
147 |
|
|
/* The LED used by the comtest tasks. mainCOM_TEST_LED + 1 is also used.
|
148 |
|
|
See the comtest.c file for more information. */
|
149 |
|
|
#define mainCOM_TEST_LED ( 4 )
|
150 |
|
|
|
151 |
|
|
/* Baud rate used by the comtest tasks. */
|
152 |
|
|
#define mainCOM_TEST_BAUD_RATE ( 115200 )
|
153 |
|
|
|
154 |
|
|
/* Misc. */
|
155 |
|
|
#define mainDONT_WAIT ( 0 )
|
156 |
|
|
|
157 |
|
|
/* Dimension the buffer used to hold the value of the high frequency timer
|
158 |
|
|
count when it is converted to a string. */
|
159 |
|
|
#define mainMAX_STRING_LENGTH ( 20 )
|
160 |
|
|
|
161 |
|
|
/* The frequency at which the "fast interrupt test" interrupt will occur. */
|
162 |
|
|
#define mainTEST_INTERRUPT_FREQUENCY ( 20000 )
|
163 |
|
|
|
164 |
|
|
/* The number of timer clocks we expect to occur between each "fast
|
165 |
|
|
interrupt test" interrupt. */
|
166 |
|
|
#define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )
|
167 |
|
|
|
168 |
|
|
/* The number of nano seconds between each core clock. */
|
169 |
|
|
#define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )
|
170 |
|
|
|
171 |
|
|
/*-----------------------------------------------------------*/
|
172 |
|
|
|
173 |
|
|
/*
|
174 |
|
|
* Setup the processor ready for the demo.
|
175 |
|
|
*/
|
176 |
|
|
static void prvSetupHardware( void );
|
177 |
|
|
|
178 |
|
|
/*
|
179 |
|
|
* Implements the 'check' task functionality as described at the top of this
|
180 |
|
|
* file.
|
181 |
|
|
*/
|
182 |
|
|
static void prvCheckTask( void *pvParameters ) __attribute__((noreturn));
|
183 |
|
|
|
184 |
|
|
/*
|
185 |
|
|
* Tasks that test the context switch mechanism by filling the processor
|
186 |
|
|
* registers with known values, then checking that the values contained
|
187 |
|
|
* within the registers is as expected. The tasks are likely to get swapped
|
188 |
|
|
* in and out between setting the register values and checking the register
|
189 |
|
|
* values. */
|
190 |
|
|
static void prvTestTask1( void *pvParameters );
|
191 |
|
|
static void prvTestTask2( void *pvParameters );
|
192 |
|
|
|
193 |
|
|
/*-----------------------------------------------------------*/
|
194 |
|
|
|
195 |
|
|
/* The queue used to send messages to the LCD task. */
|
196 |
|
|
static xQueueHandle xLCDQueue;
|
197 |
|
|
|
198 |
|
|
/* Flag used by prvTestTask1() and prvTestTask2() to indicate their status
|
199 |
|
|
(pass/fail). */
|
200 |
|
|
unsigned portLONG ulStatus1 = pdPASS;
|
201 |
|
|
|
202 |
|
|
/* Variables incremented by prvTestTask1() and prvTestTask2() respectively on
|
203 |
|
|
each iteration of their function. This is used to detect either task stopping
|
204 |
|
|
their execution.. */
|
205 |
|
|
unsigned portLONG ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;
|
206 |
|
|
|
207 |
|
|
/*-----------------------------------------------------------*/
|
208 |
|
|
|
209 |
|
|
/*
|
210 |
|
|
* Create the demo tasks then start the scheduler.
|
211 |
|
|
*/
|
212 |
|
|
int main( void )
|
213 |
|
|
{
|
214 |
|
|
/* Configure any hardware required for this demo. */
|
215 |
|
|
prvSetupHardware();
|
216 |
|
|
|
217 |
|
|
/* Create the LCD task - this returns the queue to use when writing
|
218 |
|
|
messages to the LCD. */
|
219 |
|
|
xLCDQueue = xStartLCDTask();
|
220 |
|
|
|
221 |
|
|
/* Create all the other standard demo tasks. */
|
222 |
|
|
vStartLEDFlashTasks( tskIDLE_PRIORITY );
|
223 |
|
|
vCreateBlockTimeTasks();
|
224 |
|
|
vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
|
225 |
|
|
vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
|
226 |
|
|
vStartQueuePeekTasks();
|
227 |
|
|
vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
|
228 |
|
|
vStartInterruptQueueTasks();
|
229 |
|
|
|
230 |
|
|
/* Create the tasks defined within this file. */
|
231 |
|
|
xTaskCreate( prvTestTask1, "Tst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
|
232 |
|
|
xTaskCreate( prvTestTask2, "Tst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
|
233 |
|
|
|
234 |
|
|
/* prvCheckTask uses sprintf so requires more stack. */
|
235 |
|
|
xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
236 |
|
|
|
237 |
|
|
/* Finally start the scheduler. */
|
238 |
|
|
vTaskStartScheduler();
|
239 |
|
|
|
240 |
|
|
/* Will only reach here if there is insufficient heap available to start
|
241 |
|
|
the scheduler. */
|
242 |
|
|
return 0;
|
243 |
|
|
}
|
244 |
|
|
/*-----------------------------------------------------------*/
|
245 |
|
|
|
246 |
|
|
static void prvTestTask1( void *pvParameters )
|
247 |
|
|
{
|
248 |
|
|
extern void vRegTest1( unsigned long * );
|
249 |
|
|
|
250 |
|
|
for( ;; )
|
251 |
|
|
{
|
252 |
|
|
/* Perform the register test function. */
|
253 |
|
|
vRegTest1( &ulStatus1 );
|
254 |
|
|
|
255 |
|
|
/* Increment the counter so the check task knows we are still
|
256 |
|
|
running. */
|
257 |
|
|
ulRegTest1Cycles++;
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
/*-----------------------------------------------------------*/
|
261 |
|
|
|
262 |
|
|
static void prvTestTask2( void *pvParameters )
|
263 |
|
|
{
|
264 |
|
|
extern void vRegTest2( unsigned long * );
|
265 |
|
|
|
266 |
|
|
for( ;; )
|
267 |
|
|
{
|
268 |
|
|
/* Perform the register test function. */
|
269 |
|
|
vRegTest2( &ulStatus1 );
|
270 |
|
|
|
271 |
|
|
/* Increment the counter so the check task knows we are still
|
272 |
|
|
running. */
|
273 |
|
|
ulRegTest2Cycles++;
|
274 |
|
|
}
|
275 |
|
|
}
|
276 |
|
|
/*-----------------------------------------------------------*/
|
277 |
|
|
|
278 |
|
|
static void prvSetupHardware( void )
|
279 |
|
|
{
|
280 |
|
|
/* Set the system and peripheral bus speeds and enable the program cache*/
|
281 |
|
|
SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 );
|
282 |
|
|
mOSCSetPBDIV( OSC_PB_DIV_2 );
|
283 |
|
|
|
284 |
|
|
/* Setup to use the external interrupt controller. */
|
285 |
|
|
INTEnableSystemMultiVectoredInt();
|
286 |
|
|
|
287 |
|
|
portDISABLE_INTERRUPTS();
|
288 |
|
|
|
289 |
|
|
/* Setup the digital IO for the LED's. */
|
290 |
|
|
vParTestInitialise();
|
291 |
|
|
}
|
292 |
|
|
/*-----------------------------------------------------------*/
|
293 |
|
|
|
294 |
|
|
static void prvCheckTask( void *pvParameters )
|
295 |
|
|
{
|
296 |
|
|
unsigned portLONG ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulTicksToWait = mainNO_ERROR_PERIOD;
|
297 |
|
|
portTickType xLastExecutionTime;
|
298 |
|
|
|
299 |
|
|
/* Buffer into which the high frequency timer count is written as a string. */
|
300 |
|
|
static portCHAR cStringBuffer[ mainMAX_STRING_LENGTH ];
|
301 |
|
|
|
302 |
|
|
/* The count of the high frequency timer interrupts. */
|
303 |
|
|
extern unsigned portLONG ulHighFrequencyTimerInterrupts;
|
304 |
|
|
xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };
|
305 |
|
|
|
306 |
|
|
/* Setup the high frequency, high priority, timer test. It is setup here
|
307 |
|
|
to ensure it does not fire before the scheduler is started. */
|
308 |
|
|
vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
|
309 |
|
|
|
310 |
|
|
/* Initialise the variable used to control our iteration rate prior to
|
311 |
|
|
its first use. */
|
312 |
|
|
xLastExecutionTime = xTaskGetTickCount();
|
313 |
|
|
|
314 |
|
|
for( ;; )
|
315 |
|
|
{
|
316 |
|
|
/* Wait until it is time to run the tests again. */
|
317 |
|
|
vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );
|
318 |
|
|
|
319 |
|
|
/* Has either register check 1 or 2 task discovered an error? */
|
320 |
|
|
if( ulStatus1 != pdPASS )
|
321 |
|
|
{
|
322 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
323 |
|
|
xMessage.pcMessage = "Error: Reg test1";
|
324 |
|
|
}
|
325 |
|
|
|
326 |
|
|
/* Check that the register test 1 task is still running. */
|
327 |
|
|
if( ulLastRegTest1Value == ulRegTest1Cycles )
|
328 |
|
|
{
|
329 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
330 |
|
|
xMessage.pcMessage = "Error: Reg test2";
|
331 |
|
|
}
|
332 |
|
|
ulLastRegTest1Value = ulRegTest1Cycles;
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
/* Check that the register test 2 task is still running. */
|
336 |
|
|
if( ulLastRegTest2Value == ulRegTest2Cycles )
|
337 |
|
|
{
|
338 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
339 |
|
|
xMessage.pcMessage = "Error: Reg test3";
|
340 |
|
|
}
|
341 |
|
|
ulLastRegTest2Value = ulRegTest2Cycles;
|
342 |
|
|
|
343 |
|
|
|
344 |
|
|
/* Have any of the standard demo tasks detected an error in their
|
345 |
|
|
operation? */
|
346 |
|
|
if( xAreGenericQueueTasksStillRunning() != pdTRUE )
|
347 |
|
|
{
|
348 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
349 |
|
|
xMessage.pcMessage = "Error: Gen Q";
|
350 |
|
|
}
|
351 |
|
|
else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
|
352 |
|
|
{
|
353 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
354 |
|
|
xMessage.pcMessage = "Error: Q Peek";
|
355 |
|
|
}
|
356 |
|
|
else if( xAreComTestTasksStillRunning() != pdTRUE )
|
357 |
|
|
{
|
358 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
359 |
|
|
xMessage.pcMessage = "Error: COM test";
|
360 |
|
|
}
|
361 |
|
|
else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
|
362 |
|
|
{
|
363 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
364 |
|
|
xMessage.pcMessage = "Error: Blck time";
|
365 |
|
|
}
|
366 |
|
|
else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
|
367 |
|
|
{
|
368 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
369 |
|
|
xMessage.pcMessage = "Error: Sem test";
|
370 |
|
|
}
|
371 |
|
|
else if( xAreIntQueueTasksStillRunning() != pdTRUE )
|
372 |
|
|
{
|
373 |
|
|
ulTicksToWait = mainERROR_PERIOD;
|
374 |
|
|
xMessage.pcMessage = "Error: Int queue";
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
/* Write the ulHighFrequencyTimerInterrupts value to the string
|
378 |
|
|
buffer. It will only be displayed if no errors have been detected. */
|
379 |
|
|
sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );
|
380 |
|
|
|
381 |
|
|
xQueueSend( xLCDQueue, &xMessage, mainDONT_WAIT );
|
382 |
|
|
vParTestToggleLED( mainCHECK_LED );
|
383 |
|
|
}
|
384 |
|
|
}
|
385 |
|
|
/*-----------------------------------------------------------*/
|
386 |
|
|
|
387 |
|
|
void vApplicationStackOverflowHook( void )
|
388 |
|
|
{
|
389 |
|
|
/* Look at pxCurrentTCB to see which task overflowed its stack. */
|
390 |
|
|
for( ;; );
|
391 |
|
|
}
|
392 |
|
|
/*-----------------------------------------------------------*/
|
393 |
|
|
|
394 |
|
|
void _general_exception_handler( unsigned portLONG ulCause, unsigned portLONG ulStatus )
|
395 |
|
|
{
|
396 |
|
|
/* This overrides the definition provided by the kernel. Other exceptions
|
397 |
|
|
should be handled here. */
|
398 |
|
|
for( ;; );
|
399 |
|
|
}
|
400 |
|
|
/*-----------------------------------------------------------*/
|
401 |
|
|
|