2 FreeRTOS V6.0.2 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the standard demo application tasks.
\r
57 * In addition to the standard demo tasks, the following tasks and tests are
\r
58 * defined and/or created within this file:
\r
60 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
61 * is permitted to access the display directly. Other tasks wishing to write a
\r
62 * message to the LCD send the message on a queue to the LCD task instead of
\r
63 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
64 * for messages - waking and displaying the messages as they arrive.
\r
66 * "Check" task - This only executes every three seconds but has the highest
\r
67 * priority so is guaranteed to get processor time. Its main function is to
\r
68 * check that all the standard demo tasks are still operational. Should any
\r
69 * unexpected behaviour within a demo task be discovered the check task will
\r
70 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
71 * executing with their expected behaviour then the check task instead writes
\r
72 * a count of the number of times the high frequency interrupt has incremented
\r
73 * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.
\r
75 * "Register test" tasks - These tasks are used in part to test the kernel port.
\r
76 * They set each processor register to a known value, then check that the
\r
77 * register still contains that value. Each of the tasks sets the registers
\r
78 * to different values, and will get swapping in and out between setting and
\r
79 * then subsequently checking the register values. Discovery of an incorrect
\r
80 * value would be indicative of an error in the task switching mechanism.
\r
82 * By way of demonstration, the demo application defines
\r
83 * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY
\r
84 * to be 1, and all other interrupts as follows:
\r
86 * + The UART is allocated a priority of 2. This means it can interrupt the
\r
87 * RTOS tick, and can also safely use queues.
\r
88 * + Two timers are configured to generate interrupts just to test the nesting
\r
89 * and queue access mechanisms. These timers are allocated priorities 2 and 3
\r
90 * respectively. Even though they both access the same two queues, the
\r
91 * priority 3 interrupt can safely interrupt the priority 2 interrupt. Both
\r
92 * can interrupt the RTOS tick.
\r
93 * + Finally a high frequency timer interrupt is configured to use priority 4 -
\r
94 * therefore kernel activity will never prevent the high frequency timer from
\r
95 * executing immediately that the interrupt is raised (within the limitations
\r
96 * of the hardware itself). It would not be safe to access a queue from this
\r
97 * interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.
\r
99 * See the online documentation for this demo for more information on interrupt
\r
103 /* Standard includes. */
\r
106 /* Scheduler includes. */
\r
107 #include "FreeRTOS.h"
\r
111 /* Demo application includes. */
\r
112 #include "partest.h"
\r
113 #include "blocktim.h"
\r
115 #include "semtest.h"
\r
116 #include "GenQTest.h"
\r
119 #include "comtest2.h"
\r
120 #include "timertest.h"
\r
121 #include "IntQueue.h"
\r
123 #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
\r
124 #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2
\r
126 /*-----------------------------------------------------------*/
\r
128 /* The rate at which the LED controlled by the 'check' task will flash when no
\r
129 errors have been detected. */
\r
130 #define mainNO_ERROR_PERIOD ( 3000 / portTICK_RATE_MS )
\r
132 /* The rate at which the LED controlled by the 'check' task will flash when an
\r
133 error has been detected. */
\r
134 #define mainERROR_PERIOD ( 500 / portTICK_RATE_MS )
\r
136 /* The priorities of the various demo application tasks. */
\r
137 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
138 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
139 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
140 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
141 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
142 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
144 /* The LED controlled by the 'check' task. */
\r
145 #define mainCHECK_LED ( 7 )
\r
147 /* The LED used by the comtest tasks. mainCOM_TEST_LED + 1 is also used.
\r
148 See the comtest.c file for more information. */
\r
149 #define mainCOM_TEST_LED ( 4 )
\r
151 /* Baud rate used by the comtest tasks. */
\r
152 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
155 #define mainDONT_WAIT ( 0 )
\r
157 /* Dimension the buffer used to hold the value of the high frequency timer
\r
158 count when it is converted to a string. */
\r
159 #define mainMAX_STRING_LENGTH ( 20 )
\r
161 /* The frequency at which the "fast interrupt test" interrupt will occur. */
\r
162 #define mainTEST_INTERRUPT_FREQUENCY ( 20000 )
\r
164 /* The number of timer clocks we expect to occur between each "fast
\r
165 interrupt test" interrupt. */
\r
166 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )
\r
168 /* The number of nano seconds between each core clock. */
\r
169 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )
\r
171 /*-----------------------------------------------------------*/
\r
174 * Setup the processor ready for the demo.
\r
176 static void prvSetupHardware( void );
\r
179 * Implements the 'check' task functionality as described at the top of this
\r
182 static void prvCheckTask( void *pvParameters ) __attribute__((noreturn));
\r
185 * Tasks that test the context switch mechanism by filling the processor
\r
186 * registers with known values, then checking that the values contained
\r
187 * within the registers is as expected. The tasks are likely to get swapped
\r
188 * in and out between setting the register values and checking the register
\r
190 static void prvTestTask1( void *pvParameters );
\r
191 static void prvTestTask2( void *pvParameters );
\r
193 /*-----------------------------------------------------------*/
\r
195 /* The queue used to send messages to the LCD task. */
\r
196 static xQueueHandle xLCDQueue;
\r
198 /* Flag used by prvTestTask1() and prvTestTask2() to indicate their status
\r
200 unsigned portLONG ulStatus1 = pdPASS;
\r
202 /* Variables incremented by prvTestTask1() and prvTestTask2() respectively on
\r
203 each iteration of their function. This is used to detect either task stopping
\r
204 their execution.. */
\r
205 unsigned portLONG ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;
\r
207 /*-----------------------------------------------------------*/
\r
210 * Create the demo tasks then start the scheduler.
\r
214 /* Configure any hardware required for this demo. */
\r
215 prvSetupHardware();
\r
217 /* Create the LCD task - this returns the queue to use when writing
\r
218 messages to the LCD. */
\r
219 xLCDQueue = xStartLCDTask();
\r
221 /* Create all the other standard demo tasks. */
\r
222 vStartLEDFlashTasks( tskIDLE_PRIORITY );
\r
223 vCreateBlockTimeTasks();
\r
224 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
225 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
226 vStartQueuePeekTasks();
\r
227 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
228 vStartInterruptQueueTasks();
\r
230 /* Create the tasks defined within this file. */
\r
231 xTaskCreate( prvTestTask1, "Tst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
232 xTaskCreate( prvTestTask2, "Tst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
234 /* prvCheckTask uses sprintf so requires more stack. */
\r
235 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
237 /* Finally start the scheduler. */
\r
238 vTaskStartScheduler();
\r
240 /* Will only reach here if there is insufficient heap available to start
\r
244 /*-----------------------------------------------------------*/
\r
246 static void prvTestTask1( void *pvParameters )
\r
248 extern void vRegTest1( unsigned long * );
\r
252 /* Perform the register test function. */
\r
253 vRegTest1( &ulStatus1 );
\r
255 /* Increment the counter so the check task knows we are still
\r
257 ulRegTest1Cycles++;
\r
260 /*-----------------------------------------------------------*/
\r
262 static void prvTestTask2( void *pvParameters )
\r
264 extern void vRegTest2( unsigned long * );
\r
268 /* Perform the register test function. */
\r
269 vRegTest2( &ulStatus1 );
\r
271 /* Increment the counter so the check task knows we are still
\r
273 ulRegTest2Cycles++;
\r
276 /*-----------------------------------------------------------*/
\r
278 static void prvSetupHardware( void )
\r
280 /* Set the system and peripheral bus speeds and enable the program cache*/
\r
281 SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 );
\r
282 mOSCSetPBDIV( OSC_PB_DIV_2 );
\r
284 /* Setup to use the external interrupt controller. */
\r
285 INTEnableSystemMultiVectoredInt();
\r
287 portDISABLE_INTERRUPTS();
\r
289 /* Setup the digital IO for the LED's. */
\r
290 vParTestInitialise();
\r
292 /*-----------------------------------------------------------*/
\r
294 static void prvCheckTask( void *pvParameters )
\r
296 unsigned portLONG ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulTicksToWait = mainNO_ERROR_PERIOD;
\r
297 portTickType xLastExecutionTime;
\r
299 /* Buffer into which the high frequency timer count is written as a string. */
\r
300 static portCHAR cStringBuffer[ mainMAX_STRING_LENGTH ];
\r
302 /* The count of the high frequency timer interrupts. */
\r
303 extern unsigned portLONG ulHighFrequencyTimerInterrupts;
\r
304 xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };
\r
306 /* Setup the high frequency, high priority, timer test. It is setup here
\r
307 to ensure it does not fire before the scheduler is started. */
\r
308 vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );
\r
310 /* Initialise the variable used to control our iteration rate prior to
\r
312 xLastExecutionTime = xTaskGetTickCount();
\r
316 /* Wait until it is time to run the tests again. */
\r
317 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );
\r
319 /* Has either register check 1 or 2 task discovered an error? */
\r
320 if( ulStatus1 != pdPASS )
\r
322 ulTicksToWait = mainERROR_PERIOD;
\r
323 xMessage.pcMessage = "Error: Reg test1";
\r
326 /* Check that the register test 1 task is still running. */
\r
327 if( ulLastRegTest1Value == ulRegTest1Cycles )
\r
329 ulTicksToWait = mainERROR_PERIOD;
\r
330 xMessage.pcMessage = "Error: Reg test2";
\r
332 ulLastRegTest1Value = ulRegTest1Cycles;
\r
335 /* Check that the register test 2 task is still running. */
\r
336 if( ulLastRegTest2Value == ulRegTest2Cycles )
\r
338 ulTicksToWait = mainERROR_PERIOD;
\r
339 xMessage.pcMessage = "Error: Reg test3";
\r
341 ulLastRegTest2Value = ulRegTest2Cycles;
\r
344 /* Have any of the standard demo tasks detected an error in their
\r
346 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
348 ulTicksToWait = mainERROR_PERIOD;
\r
349 xMessage.pcMessage = "Error: Gen Q";
\r
351 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
353 ulTicksToWait = mainERROR_PERIOD;
\r
354 xMessage.pcMessage = "Error: Q Peek";
\r
356 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
358 ulTicksToWait = mainERROR_PERIOD;
\r
359 xMessage.pcMessage = "Error: COM test";
\r
361 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
363 ulTicksToWait = mainERROR_PERIOD;
\r
364 xMessage.pcMessage = "Error: Blck time";
\r
366 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
368 ulTicksToWait = mainERROR_PERIOD;
\r
369 xMessage.pcMessage = "Error: Sem test";
\r
371 else if( xAreIntQueueTasksStillRunning() != pdTRUE )
\r
373 ulTicksToWait = mainERROR_PERIOD;
\r
374 xMessage.pcMessage = "Error: Int queue";
\r
377 /* Write the ulHighFrequencyTimerInterrupts value to the string
\r
378 buffer. It will only be displayed if no errors have been detected. */
\r
379 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );
\r
381 xQueueSend( xLCDQueue, &xMessage, mainDONT_WAIT );
\r
382 vParTestToggleLED( mainCHECK_LED );
\r
385 /*-----------------------------------------------------------*/
\r
387 void vApplicationStackOverflowHook( void )
\r
389 /* Look at pxCurrentTCB to see which task overflowed its stack. */
\r
392 /*-----------------------------------------------------------*/
\r
394 void _general_exception_handler( unsigned portLONG ulCause, unsigned portLONG ulStatus )
\r
396 /* This overrides the definition provided by the kernel. Other exceptions
\r
397 should be handled here. */
\r
400 /*-----------------------------------------------------------*/
\r