2 FreeRTOS V6.0.5 - 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 * This project includes a lot of tasks and tests and is therefore complex.
\r
56 * If you would prefer a much simpler project to get started with then select
\r
57 * the 'Blinky' build configuration within the HEW IDE.
\r
59 * Creates all the demo application tasks, then starts the scheduler. The web
\r
60 * documentation provides more details of the standard demo application tasks,
\r
61 * which provide no particular functionality but do provide a good example of
\r
62 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
63 * set of standard demo tasks to ensure the floating point unit gets some
\r
66 * In addition to the standard demo tasks, the following tasks and tests are
\r
67 * defined and/or created within this file:
\r
69 * "Reg test" tasks - These fill the registers with known values, then check
\r
70 * that each register still contains its expected value. Each task uses
\r
71 * different values. The tasks run with very low priority so get preempted
\r
72 * very frequently. A check variable is incremented on each iteration of the
\r
73 * test loop. A register containing an unexpected value is indicative of an
\r
74 * error in the context switching mechanism and will result in a branch to a
\r
75 * null loop - which in turn will prevent the check variable from incrementing
\r
76 * any further and allow the check task (described below) to determine that an
\r
77 * error has occurred. The nature of the reg test tasks necessitates that they
\r
78 * are written in assembly code.
\r
80 * "Check" task - This only executes every five seconds but has a high priority
\r
81 * to ensure it gets processor time. Its main function is to check that all the
\r
82 * standard demo tasks are still operational. While no errors have been
\r
83 * discovered the check task will toggle LED 5 every 5 seconds - the toggle
\r
84 * rate increasing to 200ms being a visual indication that at least one task has
\r
85 * reported unexpected behaviour.
\r
87 * "High frequency timer test" - A high frequency periodic interrupt is
\r
88 * generated using a timer - the interrupt is assigned a priority above
\r
89 * configMAX_SYSCALL_INTERRUPT_PRIORITY so should not be effected by anything
\r
90 * the kernel is doing. The interrupt service routine measures the number of
\r
91 * counts a separate timer performs between each interrupt to determine the
\r
92 * jitter in the interrupt timing.
\r
94 * *NOTE 1* If LED5 is toggling every 5 seconds then all the demo application
\r
95 * tasks are executing as expected and no errors have been reported in any
\r
96 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
97 * has reported unexpected behaviour.
\r
99 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
100 * the application set up a timer to generate the tick interrupt. In this
\r
101 * example a compare match timer is used for this purpose.
\r
103 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
104 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
105 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
107 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
108 * of all the 8bit timers (as two cascaded 16bit units).
\r
111 /* Standard includes. */
\r
112 #include "string.h"
\r
114 /* Hardware specific includes. */
\r
115 #include "iodefine.h"
\r
117 /* Kernel includes. */
\r
118 #include "FreeRTOS.h"
\r
121 /* Standard demo includes. */
\r
122 #include "partest.h"
\r
124 #include "IntQueue.h"
\r
125 #include "BlockQ.h"
\r
127 #include "integer.h"
\r
128 #include "blocktim.h"
\r
129 #include "semtest.h"
\r
131 #include "GenQTest.h"
\r
133 #include "recmutex.h"
\r
136 /* Values that are passed into the reg test tasks using the task parameter. The
\r
137 tasks check that the values are passed in correctly. */
\r
138 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
139 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
141 /* Priorities at which the tasks are created. */
\r
142 #define mainCHECK_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
143 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
144 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
145 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
146 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
147 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
148 #define mainuIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
149 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
150 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
151 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
153 /* The LED toggled by the check task. */
\r
154 #define mainCHECK_LED ( 5 )
\r
156 /* The rate at which mainCHECK_LED will toggle when all the tasks are running
\r
157 without error. Controlled by the check task as described at the top of this
\r
159 #define mainNO_ERROR_CYCLE_TIME ( 5000 / portTICK_RATE_MS )
\r
161 /* The rate at which mainCHECK_LED will toggle when an error has been reported
\r
162 by at least one task. Controlled by the check task as described at the top of
\r
164 #define mainERROR_CYCLE_TIME ( 200 / portTICK_RATE_MS )
\r
166 /* The period of the peripheral clock in nano seconds. This is used to calculate
\r
167 the jitter time in nano seconds as part of the high frequency timer test. The
\r
168 clock driving the timer is divided by 8. */
\r
169 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( ( double ) configPERIPHERAL_CLOCK_HZ ) / 8.0 ) * 1000000000.0 ) )
\r
172 * vApplicationMallocFailedHook() will only be called if
\r
173 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
174 * function that will execute if a call to pvPortMalloc() fails.
\r
175 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
176 * semaphore is created. It is also called by various parts of the demo
\r
179 void vApplicationMallocFailedHook( void );
\r
182 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
183 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
184 * of the idle task. It is essential that code added to this hook function
\r
185 * never attempts to block in any way (for example, call xQueueReceive() with
\r
186 * a block time specified). If the application makes use of the vTaskDelete()
\r
187 * API function (as this demo application does) then it is also important that
\r
188 * vApplicationIdleHook() is permitted to return to its calling function because
\r
189 * it is the responsibility of the idle task to clean up memory allocated by the
\r
190 * kernel to any task that has since been deleted.
\r
192 void vApplicationIdleHook( void );
\r
195 * vApplicationStackOverflowHook() will only be called if
\r
196 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
197 * name of the offending task should be passed in the function parameters, but
\r
198 * it is possible that the stack overflow will have corrupted these - in which
\r
199 * case pxCurrentTCB can be inspected to find the same information.
\r
201 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );
\r
204 * The reg test tasks as described at the top of this file.
\r
206 static void prvRegTest1Task( void *pvParameters );
\r
207 static void prvRegTest2Task( void *pvParameters );
\r
210 * The actual implementation of the reg test functionality, which, because of
\r
211 * the direct register access, have to be in assembly.
\r
213 extern void prvRegTest1Implementation( void );
\r
214 extern void prvRegTest2Implementation( void );
\r
218 * The check task as described at the top of this file.
\r
220 static void prvCheckTask( void *pvParameters );
\r
222 /* Variables that are incremented on each iteration of the reg test tasks -
\r
223 provided the tasks have not reported any errors. The check task inspects these
\r
224 variables to ensure they are still incrementing as expected. If a variable
\r
225 stops incrementing then it is likely that its associate task has stalled. */
\r
226 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
228 /*-----------------------------------------------------------*/
\r
232 extern void HardwareSetup( void );
\r
234 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
238 /* Turn all LEDs off. */
\r
239 vParTestInitialise();
\r
241 /* Start the reg test tasks which test the context switching mechanism. */
\r
242 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
243 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
245 /* Start the check task as described at the top of this file. */
\r
246 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 3, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
248 /* Create the standard demo tasks. */
\r
249 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
250 vCreateBlockTimeTasks();
\r
251 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
252 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
253 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
254 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
255 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
256 vStartQueuePeekTasks();
\r
257 vStartRecursiveMutexTasks();
\r
258 vStartInterruptQueueTasks();
\r
259 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
261 /* The suicide tasks must be created last as they need to know how many
\r
262 tasks were running prior to their creation in order to ascertain whether
\r
263 or not the correct/expected number of tasks are running at any given time. */
\r
264 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
266 /* Start the tasks running. */
\r
267 vTaskStartScheduler();
\r
269 /* If all is well we will never reach here as the scheduler will now be
\r
270 running. If we do reach here then it is likely that there was insufficient
\r
271 heap available for the idle task to be created. */
\r
274 /*-----------------------------------------------------------*/
\r
276 static void prvCheckTask( void *pvParameters )
\r
278 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
279 portTickType xNextWakeTime, xCycleFrequency = mainNO_ERROR_CYCLE_TIME;
\r
280 extern void vSetupHighFrequencyTimer( void );
\r
281 extern volatile unsigned short usMaxJitter;
\r
282 volatile unsigned long ulActualJitter = 0;
\r
283 static char cErrorText[ 100 ];
\r
285 /* If this is being executed then the kernel has been started. Start the high
\r
286 frequency timer test as described at the top of this file. This is only
\r
287 included in the optimised build configuration - otherwise it takes up too much
\r
289 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
290 vSetupHighFrequencyTimer();
\r
293 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
294 xNextWakeTime = xTaskGetTickCount();
\r
298 /* Place this task in the blocked state until it is time to run again. */
\r
299 vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );
\r
301 /* Check the standard demo tasks are running without error. */
\r
302 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
304 /* Increase the rate at which this task cycles, which will increase the
\r
305 rate at which mainCHECK_LED flashes to give visual feedback that an error
\r
307 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
308 strcpy( cErrorText, "Error: GenQueue" );
\r
310 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
312 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
313 strcpy( cErrorText, "Error: QueuePeek" );
\r
315 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
317 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
318 strcpy( cErrorText, "Error: BlockQueue" );
\r
320 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
322 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
323 strcpy( cErrorText, "Error: BlockTime" );
\r
325 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
327 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
328 strcpy( cErrorText, "Error: SemTest" );
\r
330 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
332 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
333 strcpy( cErrorText, "Error: PollQueue" );
\r
335 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
337 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
338 strcpy( cErrorText, "Error: Death" );
\r
340 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
342 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
343 strcpy( cErrorText, "Error: IntMath" );
\r
345 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
347 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
348 strcpy( cErrorText, "Error: RecMutex" );
\r
350 else if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
352 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
353 strcpy( cErrorText, "Error: IntQueue" );
\r
355 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
357 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
358 strcpy( cErrorText, "Error: Flop" );
\r
361 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
362 their loop counters if they encounter an error. */
\r
363 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
365 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
366 strcpy( cErrorText, "Error: RegTest1" );
\r
369 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
371 xCycleFrequency = mainERROR_CYCLE_TIME;
\r
372 strcpy( cErrorText, "Error: RegTest2" );
\r
375 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
376 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
378 /* Toggle the check LED to give an indication of the system status. If
\r
379 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
380 indicates an error. */
\r
381 vParTestToggleLED( mainCHECK_LED );
\r
383 /* Calculate the maximum jitter experienced by the high frequency timer
\r
384 test and print it out. It is ok to use printf without worrying about
\r
385 mutual exclusion as it is not used anywhere else in this demo. */
\r
386 //sprintf( cTempBuf, "%s [%fns]\n", "Max Jitter = ", ( ( float ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
387 ulActualJitter = ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK;
\r
389 if( xCycleFrequency == mainERROR_CYCLE_TIME )
\r
391 /* Just for break point. */
\r
396 /*-----------------------------------------------------------*/
\r
398 /* The RX port uses this callback function to configure its tick interrupt.
\r
399 This allows the application to choose the tick interrupt source. */
\r
400 void vApplicationSetupTimerInterrupt( void )
\r
402 /* Enable compare match timer 0. */
\r
405 /* Interrupt on compare match. */
\r
406 CMT0.CMCR.BIT.CMIE = 1;
\r
408 /* Set the compare match value. */
\r
409 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
411 /* Divide the PCLK by 8. */
\r
412 CMT0.CMCR.BIT.CKS = 0;
\r
414 /* Enable the interrupt... */
\r
415 _IEN( _CMT0_CMI0 ) = 1;
\r
417 /* ...and set its priority to the application defined kernel priority. */
\r
418 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
420 /* Start the timer. */
\r
421 CMT.CMSTR0.BIT.STR0 = 1;
\r
423 /*-----------------------------------------------------------*/
\r
425 /* This function is explained by the comments above its prototype at the top
\r
427 void vApplicationMallocFailedHook( void )
\r
431 /*-----------------------------------------------------------*/
\r
433 /* This function is explained by the comments above its prototype at the top
\r
435 void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
\r
439 /*-----------------------------------------------------------*/
\r
441 /* This function is explained by the comments above its prototype at the top
\r
443 void vApplicationIdleHook( void )
\r
445 static volatile unsigned long ulIdleLoopCount = 0UL;
\r
449 /*-----------------------------------------------------------*/
\r
451 /* This function is explained in the comments at the top of this file. */
\r
452 static void prvRegTest1Task( void *pvParameters )
\r
454 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
456 /* The parameter did not contain the expected value. */
\r
459 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
460 taskDISABLE_INTERRUPTS();
\r
464 /* This is an asm function that never returns. */
\r
465 prvRegTest1Implementation();
\r
467 /*-----------------------------------------------------------*/
\r
469 /* This function is explained in the comments at the top of this file. */
\r
470 static void prvRegTest2Task( void *pvParameters )
\r
472 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
474 /* The parameter did not contain the expected value. */
\r
477 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
478 taskDISABLE_INTERRUPTS();
\r
482 /* This is an asm function that never returns. */
\r
483 prvRegTest2Implementation();
\r
485 /*-----------------------------------------------------------*/
\r