2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 /* ****************************************************************************
\r
68 * This project includes a lot of tasks and tests and is therefore complex.
\r
69 * If you would prefer a much simpler project to get started with then select
\r
70 * the 'Blinky' build configuration within the HEW IDE. The Blinky build
\r
71 * configuration uses main-blinky.c instead of main-full.c.
\r
72 * ****************************************************************************
\r
74 * Creates all the demo application tasks, then starts the scheduler. The web
\r
75 * documentation provides more details of the standard demo application tasks,
\r
76 * which provide no particular functionality but do provide a good example of
\r
77 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
78 * set of standard demo tasks to ensure the floating point unit gets some
\r
81 * In addition to the standard demo tasks, the following tasks and tests are
\r
82 * defined and/or created within this file:
\r
84 * "Reg test" tasks - These fill the registers with known values, then
\r
85 * repeatedly check that each register still contains its expected value for
\r
86 * the lifetime of the tasks. Each task uses different values. The tasks run
\r
87 * with very low priority so get preempted very frequently. A check variable
\r
88 * is incremented on each iteration of the test loop. A register containing an
\r
89 * unexpected value is indicative of an error in the context switching
\r
90 * mechanism and will result in a branch to a null loop - which in turn will
\r
91 * prevent the check variable from incrementing any further and allow the check
\r
92 * timer (described below) to determine that an error has occurred. The nature
\r
93 * of the reg test tasks necessitates that they are written in assembly code.
\r
95 * "Check Timer" and Callback Function - The check timer period is initially
\r
96 * set to five seconds. The check timer callback function checks that all the
\r
97 * standard demo tasks are not only still executing, but are executing without
\r
98 * reporting any errors. If the check timer discovers that a task has either
\r
99 * stalled, or reported an error, then it changes its own period from the
\r
100 * initial five seconds, to just 200ms. The check timer callback function
\r
101 * also toggles LED 3 each time it is called. This provides a visual
\r
102 * indication of the system status: If the LED toggles every five seconds,
\r
103 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
104 * an issue has been discovered with at least one task.
\r
106 * "High frequency timer test" - A high frequency periodic interrupt is
\r
107 * generated using a timer - the interrupt is assigned a priority above
\r
108 * configMAX_SYSCALL_INTERRUPT_PRIORITY, so will not be effected by anything
\r
109 * the kernel is doing. The frequency and priority of the interrupt, in
\r
110 * combination with other standard tests executed in this demo, will result
\r
111 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
112 * included in build configurations that have the optimiser switched on.
\r
114 * *NOTE 1* If LED3 is toggling every 5 seconds then all the demo application
\r
115 * tasks are executing as expected and no errors have been reported in any
\r
116 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
117 * has reported unexpected behaviour.
\r
119 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
120 * the application set up a timer to generate the tick interrupt. In this
\r
121 * example a compare match timer is used for this purpose.
\r
123 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
124 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
125 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
127 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
128 * of all the 8bit timers (as two cascaded 16bit units).
\r
131 /* Hardware specific includes. */
\r
132 #include "iodefine.h"
\r
134 /* Kernel includes. */
\r
135 #include "FreeRTOS.h"
\r
137 #include "timers.h"
\r
138 #include "semphr.h"
\r
140 /* Standard demo includes. */
\r
141 #include "partest.h"
\r
143 #include "IntQueue.h"
\r
144 #include "BlockQ.h"
\r
146 #include "integer.h"
\r
147 #include "blocktim.h"
\r
148 #include "semtest.h"
\r
150 #include "GenQTest.h"
\r
152 #include "recmutex.h"
\r
155 /* Values that are passed into the reg test tasks using the task parameter. The
\r
156 tasks check that the values are passed in correctly. */
\r
157 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
158 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
160 /* Priorities at which the tasks are created. */
\r
161 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
162 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
163 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
164 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
165 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
166 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
167 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
168 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
170 /* The LED toggled by the check timer. */
\r
171 #define mainCHECK_LED ( 3 )
\r
173 /* The period at which the check timer will expire, in ms, provided no errors
\r
174 have been reported by any of the standard demo tasks. ms are converted to the
\r
175 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
176 #define mainCHECK_TIMER_PERIOD_MS ( 5000UL / portTICK_RATE_MS )
\r
178 /* The period at which the check timer will expire, in ms, if an error has been
\r
179 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
180 in ticks using the portTICK_RATE_MS constant. */
\r
181 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
183 /* A block time of zero simple means "Don't Block". */
\r
184 #define mainDONT_BLOCK ( 0UL )
\r
187 * vApplicationMallocFailedHook() will only be called if
\r
188 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
189 * function that will execute if a call to pvPortMalloc() fails.
\r
190 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
191 * semaphore is created. It is also called by various parts of the demo
\r
194 void vApplicationMallocFailedHook( void );
\r
197 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
198 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
199 * of the idle task. It is essential that code added to this hook function
\r
200 * never attempts to block in any way (for example, call xQueueReceive() with
\r
201 * a block time specified). If the application makes use of the vTaskDelete()
\r
202 * API function (as this demo application does) then it is also important that
\r
203 * vApplicationIdleHook() is permitted to return to its calling function because
\r
204 * it is the responsibility of the idle task to clean up memory allocated by the
\r
205 * kernel to any task that has since been deleted.
\r
207 void vApplicationIdleHook( void );
\r
210 * vApplicationStackOverflowHook() will only be called if
\r
211 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
212 * name of the offending task should be passed in the function parameters, but
\r
213 * it is possible that the stack overflow will have corrupted these - in which
\r
214 * case pxCurrentTCB can be inspected to find the same information.
\r
216 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );
\r
219 * The reg test tasks as described at the top of this file.
\r
221 static void prvRegTest1Task( void *pvParameters );
\r
222 static void prvRegTest2Task( void *pvParameters );
\r
225 * The actual implementation of the reg test functionality, which, because of
\r
226 * the direct register access, have to be in assembly.
\r
228 static void prvRegTest1Implementation( void );
\r
229 static void prvRegTest2Implementation( void );
\r
232 * The check timer callback function, as described at the top of this file.
\r
234 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
237 /*-----------------------------------------------------------*/
\r
239 /* Variables that are incremented on each iteration of the reg test tasks -
\r
240 provided the tasks have not reported any errors. The check timer inspects these
\r
241 variables to ensure they are still incrementing as expected. If a variable
\r
242 stops incrementing then it is likely that its associate task has stalled. */
\r
243 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
245 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
247 static xTimerHandle xCheckTimer = NULL;
\r
249 /*-----------------------------------------------------------*/
\r
253 extern void HardwareSetup( void );
\r
255 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
259 /* Turn all LEDs off. */
\r
260 vParTestInitialise();
\r
262 /* Start the reg test tasks which test the context switching mechanism. */
\r
263 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
264 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
266 /* Create the standard demo tasks. */
\r
267 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
268 vCreateBlockTimeTasks();
\r
269 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
270 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
271 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
272 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
273 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
274 vStartQueuePeekTasks();
\r
275 vStartRecursiveMutexTasks();
\r
276 vStartInterruptQueueTasks();
\r
277 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
279 /* The suicide tasks must be created last as they need to know how many
\r
280 tasks were running prior to their creation in order to ascertain whether
\r
281 or not the correct/expected number of tasks are running at any given time. */
\r
282 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
284 /* Create the software timer that performs the 'check' functionality,
\r
285 as described at the top of this file. */
\r
286 xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
\r
287 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
288 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
289 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
290 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
293 /* Sanity check that the check timer was indeed created. */
\r
294 configASSERT( xCheckTimer );
\r
296 /* Start the check timer. It will actually start when the scheduler is
\r
298 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
300 /* Start the tasks running. */
\r
301 vTaskStartScheduler();
\r
303 /* If all is well, the following line will never be reached as the scheduler
\r
304 will be running. If the following line is reached, there was insufficient
\r
305 FreeRTOS heap available for the idle task to be created. See
\r
306 http://www.freertos.org/a00111.html and the malloc failed hook function for
\r
307 more information. */
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
314 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
315 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
317 /* Check the standard demo tasks are running without error. */
\r
318 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
320 lErrorStatus = pdFAIL;
\r
322 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
324 lErrorStatus = pdFAIL;
\r
326 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
328 lErrorStatus = pdFAIL;
\r
330 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
332 lErrorStatus = pdFAIL;
\r
334 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
336 lErrorStatus = pdFAIL;
\r
338 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
340 lErrorStatus = pdFAIL;
\r
342 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
344 lErrorStatus = pdFAIL;
\r
346 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
348 lErrorStatus = pdFAIL;
\r
350 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
352 lErrorStatus = pdFAIL;
\r
354 else if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
356 lErrorStatus = pdFAIL;
\r
358 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
360 lErrorStatus = pdFAIL;
\r
363 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
364 their loop counters if they encounter an error. */
\r
365 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
367 lErrorStatus = pdFAIL;
\r
370 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
372 lErrorStatus = pdFAIL;
\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 /* Was an error detected this time through the callback execution? */
\r
384 if( lErrorStatus != pdPASS )
\r
386 if( lChangedTimerPeriodAlready == pdFALSE )
\r
388 lChangedTimerPeriodAlready = pdTRUE;
\r
390 /* This call to xTimerChangePeriod() uses a zero block time.
\r
391 Functions called from inside of a timer callback function must
\r
392 *never* attempt to block. */
\r
393 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
397 /*-----------------------------------------------------------*/
\r
399 /* The RX port uses this callback function to configure its tick interrupt.
\r
400 This allows the application to choose the tick interrupt source. */
\r
401 void vApplicationSetupTimerInterrupt( void )
\r
403 /* Enable compare match timer 0. */
\r
406 /* Interrupt on compare match. */
\r
407 CMT0.CMCR.BIT.CMIE = 1;
\r
409 /* Set the compare match value. */
\r
410 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
412 /* Divide the PCLK by 8. */
\r
413 CMT0.CMCR.BIT.CKS = 0;
\r
415 /* Enable the interrupt... */
\r
416 _IEN( _CMT0_CMI0 ) = 1;
\r
418 /* ...and set its priority to the application defined kernel priority. */
\r
419 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
421 /* Start the timer. */
\r
422 CMT.CMSTR0.BIT.STR0 = 1;
\r
424 /*-----------------------------------------------------------*/
\r
426 /* This function is explained by the comments above its prototype at the top
\r
428 void vApplicationMallocFailedHook( void )
\r
432 /*-----------------------------------------------------------*/
\r
434 /* This function is explained by the comments above its prototype at the top
\r
436 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
440 /*-----------------------------------------------------------*/
\r
442 /* This function is explained by the comments above its prototype at the top
\r
444 void vApplicationIdleHook( void )
\r
446 /* If this is being executed then the kernel has been started. Start the high
\r
447 frequency timer test as described at the top of this file. This is only
\r
448 included in the optimised build configuration - otherwise it takes up too much
\r
449 CPU time and can disrupt other tests. */
\r
450 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
451 static portBASE_TYPE xTimerTestStarted = pdFALSE;
\r
452 extern void vSetupHighFrequencyTimer( void );
\r
453 if( xTimerTestStarted == pdFALSE )
\r
455 vSetupHighFrequencyTimer();
\r
456 xTimerTestStarted = pdTRUE;
\r
460 /*-----------------------------------------------------------*/
\r
462 /* This function is explained in the comments at the top of this file. */
\r
463 static void prvRegTest1Task( void *pvParameters )
\r
465 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
467 /* The parameter did not contain the expected value. */
\r
470 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
471 taskDISABLE_INTERRUPTS();
\r
475 /* This is an inline asm function that never returns. */
\r
476 prvRegTest1Implementation();
\r
478 /*-----------------------------------------------------------*/
\r
480 /* This function is explained in the comments at the top of this file. */
\r
481 static void prvRegTest2Task( void *pvParameters )
\r
483 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
485 /* The parameter did not contain the expected value. */
\r
488 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
489 taskDISABLE_INTERRUPTS();
\r
493 /* This is an inline asm function that never returns. */
\r
494 prvRegTest2Implementation();
\r
496 /*-----------------------------------------------------------*/
\r
498 /* This function is explained in the comments at the top of this file. */
\r
499 #pragma inline_asm prvRegTest1Implementation
\r
500 static void prvRegTest1Implementation( void )
\r
502 ; Put a known value in each register.
\r
519 ; Loop, checking each itteration that each register still contains the
\r
523 ; Push the registers that are going to get clobbered.
\r
526 ; Increment the loop counter to show this task is still getting CPU time.
\r
527 MOV.L #_ulRegTest1CycleCount, R14
\r
532 ; Yield to extend the text coverage. Set the bit in the ITU SWINTR register.
\r
534 MOV.L #0872E0H, R15
\r
539 ; Restore the clobbered registers.
\r
542 ; Now compare each register to ensure it still contains the value that was
\r
543 ; set before this loop was entered.
\r
575 ; All comparisons passed, start a new itteratio of this loop.
\r
579 ; A compare failed, just loop here so the loop counter stops incrementing
\r
580 ; causing the check timer to indicate the error.
\r
583 /*-----------------------------------------------------------*/
\r
585 /* This function is explained in the comments at the top of this file. */
\r
586 #pragma inline_asm prvRegTest2Implementation
\r
587 static void prvRegTest2Implementation( void )
\r
589 ; Put a known value in each register.
\r
606 ; Loop, checking on each itteration that each register still contains the
\r
610 ; Push the registers that are going to get clobbered.
\r
613 ; Increment the loop counter to show this task is still getting CPU time.
\r
614 MOV.L #_ulRegTest2CycleCount, R14
\r
619 ; Restore the clobbered registers.
\r
653 ; All comparisons passed, start a new itteratio of this loop.
\r
657 ; A compare failed, just loop here so the loop counter stops incrementing
\r
658 ; - causing the check timer to indicate the error.
\r
661 /*-----------------------------------------------------------*/
\r