2 FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* ****************************************************************************
\r
67 * This project includes a lot of tasks and tests and is therefore complex.
\r
68 * If you would prefer a much simpler project to get started with then select
\r
69 * the 'Blinky' build configuration within the HEW IDE. The Blinky build
\r
70 * configuration uses main-blinky.c instead of main-full.c.
\r
71 * ****************************************************************************
\r
73 * Creates all the demo application tasks, then starts the scheduler. The web
\r
74 * documentation provides more details of the standard demo application tasks,
\r
75 * which provide no particular functionality but do provide a good example of
\r
76 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
77 * set of standard demo tasks to ensure the floating point unit gets some
\r
80 * In addition to the standard demo tasks, the following tasks and tests are
\r
81 * defined and/or created within this file:
\r
83 * "Reg test" tasks - These fill the registers with known values, then
\r
84 * repeatedly check that each register still contains its expected value for
\r
85 * the lifetime of the tasks. Each task uses different values. The tasks run
\r
86 * with very low priority so get preempted very frequently. A check variable
\r
87 * is incremented on each iteration of the test loop. A register containing an
\r
88 * unexpected value is indicative of an error in the context switching
\r
89 * mechanism and will result in a branch to a null loop - which in turn will
\r
90 * prevent the check variable from incrementing any further and allow the check
\r
91 * timer (described below) to determine that an error has occurred. The nature
\r
92 * of the reg test tasks necessitates that they are written in assembly code.
\r
94 * "Check Timer" and Callback Function - The check timer period is initially
\r
95 * set to five seconds. The check timer callback function checks that all the
\r
96 * standard demo tasks are not only still executing, but are executing without
\r
97 * reporting any errors. If the check timer discovers that a task has either
\r
98 * stalled, or reported an error, then it changes its own period from the
\r
99 * initial five seconds, to just 200ms. The check timer callback function
\r
100 * also toggles LED 3 each time it is called. This provides a visual
\r
101 * indication of the system status: If the LED toggles every five seconds,
\r
102 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
103 * an issue has been discovered with at least one task.
\r
105 * "High frequency timer test" - A high frequency periodic interrupt is
\r
106 * generated using a timer - the interrupt is assigned a priority above
\r
107 * configMAX_SYSCALL_INTERRUPT_PRIORITY, so will not be effected by anything
\r
108 * the kernel is doing. The frequency and priority of the interrupt, in
\r
109 * combination with other standard tests executed in this demo, will result
\r
110 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
111 * included in build configurations that have the optimiser switched on.
\r
113 * *NOTE 1* If LED3 is toggling every 5 seconds then all the demo application
\r
114 * tasks are executing as expected and no errors have been reported in any
\r
115 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
116 * has reported unexpected behaviour.
\r
118 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
119 * the application set up a timer to generate the tick interrupt. In this
\r
120 * example a compare match timer is used for this purpose.
\r
122 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
123 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
124 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
126 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
127 * of all the 8bit timers (as two cascaded 16bit units).
\r
130 /* Hardware specific includes. */
\r
131 #include "iodefine.h"
\r
133 /* Kernel includes. */
\r
134 #include "FreeRTOS.h"
\r
136 #include "timers.h"
\r
137 #include "semphr.h"
\r
139 /* Standard demo includes. */
\r
140 #include "partest.h"
\r
142 #include "IntQueue.h"
\r
143 #include "BlockQ.h"
\r
145 #include "integer.h"
\r
146 #include "blocktim.h"
\r
147 #include "semtest.h"
\r
149 #include "GenQTest.h"
\r
151 #include "recmutex.h"
\r
154 /* Values that are passed into the reg test tasks using the task parameter. The
\r
155 tasks check that the values are passed in correctly. */
\r
156 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
157 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
159 /* Priorities at which the tasks are created. */
\r
160 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
161 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
162 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
163 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
164 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
165 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
166 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
167 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
169 /* The LED toggled by the check timer. */
\r
170 #define mainCHECK_LED ( 3 )
\r
172 /* The period at which the check timer will expire, in ms, provided no errors
\r
173 have been reported by any of the standard demo tasks. ms are converted to the
\r
174 equivalent in ticks using the portTICK_RATE_MS constant. */
\r
175 #define mainCHECK_TIMER_PERIOD_MS ( 5000UL / portTICK_RATE_MS )
\r
177 /* The period at which the check timer will expire, in ms, if an error has been
\r
178 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
179 in ticks using the portTICK_RATE_MS constant. */
\r
180 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
\r
182 /* A block time of zero simple means "Don't Block". */
\r
183 #define mainDONT_BLOCK ( 0UL )
\r
186 * vApplicationMallocFailedHook() will only be called if
\r
187 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
188 * function that will execute if a call to pvPortMalloc() fails.
\r
189 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
190 * semaphore is created. It is also called by various parts of the demo
\r
193 void vApplicationMallocFailedHook( void );
\r
196 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
197 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
198 * of the idle task. It is essential that code added to this hook function
\r
199 * never attempts to block in any way (for example, call xQueueReceive() with
\r
200 * a block time specified). If the application makes use of the vTaskDelete()
\r
201 * API function (as this demo application does) then it is also important that
\r
202 * vApplicationIdleHook() is permitted to return to its calling function because
\r
203 * it is the responsibility of the idle task to clean up memory allocated by the
\r
204 * kernel to any task that has since been deleted.
\r
206 void vApplicationIdleHook( void );
\r
209 * vApplicationStackOverflowHook() will only be called if
\r
210 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
211 * name of the offending task should be passed in the function parameters, but
\r
212 * it is possible that the stack overflow will have corrupted these - in which
\r
213 * case pxCurrentTCB can be inspected to find the same information.
\r
215 void vApplicationStackOverflowHook( xTaskHandle pxTask, char *pcTaskName );
\r
218 * The reg test tasks as described at the top of this file.
\r
220 static void prvRegTest1Task( void *pvParameters );
\r
221 static void prvRegTest2Task( void *pvParameters );
\r
224 * The actual implementation of the reg test functionality, which, because of
\r
225 * the direct register access, have to be in assembly.
\r
227 static void prvRegTest1Implementation( void );
\r
228 static void prvRegTest2Implementation( void );
\r
231 * The check timer callback function, as described at the top of this file.
\r
233 static void prvCheckTimerCallback( xTimerHandle xTimer );
\r
236 /*-----------------------------------------------------------*/
\r
238 /* Variables that are incremented on each iteration of the reg test tasks -
\r
239 provided the tasks have not reported any errors. The check timer inspects these
\r
240 variables to ensure they are still incrementing as expected. If a variable
\r
241 stops incrementing then it is likely that its associate task has stalled. */
\r
242 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
244 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
246 static xTimerHandle xCheckTimer = NULL;
\r
248 /*-----------------------------------------------------------*/
\r
252 extern void HardwareSetup( void );
\r
254 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
258 /* Turn all LEDs off. */
\r
259 vParTestInitialise();
\r
261 /* Start the reg test tasks which test the context switching mechanism. */
\r
262 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
263 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
265 /* Create the standard demo tasks. */
\r
266 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
267 vCreateBlockTimeTasks();
\r
268 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
269 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
270 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
271 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
272 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
273 vStartQueuePeekTasks();
\r
274 vStartRecursiveMutexTasks();
\r
275 vStartInterruptQueueTasks();
\r
276 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
278 /* The suicide tasks must be created last as they need to know how many
\r
279 tasks were running prior to their creation in order to ascertain whether
\r
280 or not the correct/expected number of tasks are running at any given time. */
\r
281 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
283 /* Create the software timer that performs the 'check' functionality,
\r
284 as described at the top of this file. */
\r
285 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
286 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
287 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
288 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
289 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
292 /* Sanity check that the check timer was indeed created. */
\r
293 configASSERT( xCheckTimer );
\r
295 /* Start the check timer. It will actually start when the scheduler is
\r
297 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
299 /* Start the tasks running. */
\r
300 vTaskStartScheduler();
\r
302 /* If all is well, the following line will never be reached as the scheduler
\r
303 will be running. If the following line is reached, there was insufficient
\r
304 FreeRTOS heap available for the idle task to be created. See
\r
305 http://www.freertos.org/a00111.html and the malloc failed hook function for
\r
306 more information. */
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvCheckTimerCallback( xTimerHandle xTimer )
\r
313 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
314 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
316 /* Check the standard demo tasks are running without error. */
\r
317 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
319 lErrorStatus = pdFAIL;
\r
321 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
323 lErrorStatus = pdFAIL;
\r
325 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
327 lErrorStatus = pdFAIL;
\r
329 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
331 lErrorStatus = pdFAIL;
\r
333 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
335 lErrorStatus = pdFAIL;
\r
337 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
339 lErrorStatus = pdFAIL;
\r
341 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
343 lErrorStatus = pdFAIL;
\r
345 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
347 lErrorStatus = pdFAIL;
\r
349 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
351 lErrorStatus = pdFAIL;
\r
353 else if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
355 lErrorStatus = pdFAIL;
\r
357 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
359 lErrorStatus = pdFAIL;
\r
362 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
363 their loop counters if they encounter an error. */
\r
364 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
366 lErrorStatus = pdFAIL;
\r
369 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
371 lErrorStatus = pdFAIL;
\r
374 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
375 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
377 /* Toggle the check LED to give an indication of the system status. If
\r
378 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
379 indicates an error. */
\r
380 vParTestToggleLED( mainCHECK_LED );
\r
382 /* Was an error detected this time through the callback execution? */
\r
383 if( lErrorStatus != pdPASS )
\r
385 if( lChangedTimerPeriodAlready == pdFALSE )
\r
387 lChangedTimerPeriodAlready = pdTRUE;
\r
389 /* This call to xTimerChangePeriod() uses a zero block time.
\r
390 Functions called from inside of a timer callback function must
\r
391 *never* attempt to block. */
\r
392 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\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, 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 /* If this is being executed then the kernel has been started. Start the high
\r
446 frequency timer test as described at the top of this file. This is only
\r
447 included in the optimised build configuration - otherwise it takes up too much
\r
448 CPU time and can disrupt other tests. */
\r
449 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
450 static portBASE_TYPE xTimerTestStarted = pdFALSE;
\r
451 extern void vSetupHighFrequencyTimer( void );
\r
452 if( xTimerTestStarted == pdFALSE )
\r
454 vSetupHighFrequencyTimer();
\r
455 xTimerTestStarted = pdTRUE;
\r
459 /*-----------------------------------------------------------*/
\r
461 /* This function is explained in the comments at the top of this file. */
\r
462 static void prvRegTest1Task( void *pvParameters )
\r
464 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
466 /* The parameter did not contain the expected value. */
\r
469 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
470 taskDISABLE_INTERRUPTS();
\r
474 /* This is an inline asm function that never returns. */
\r
475 prvRegTest1Implementation();
\r
477 /*-----------------------------------------------------------*/
\r
479 /* This function is explained in the comments at the top of this file. */
\r
480 static void prvRegTest2Task( void *pvParameters )
\r
482 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
484 /* The parameter did not contain the expected value. */
\r
487 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
488 taskDISABLE_INTERRUPTS();
\r
492 /* This is an inline asm function that never returns. */
\r
493 prvRegTest2Implementation();
\r
495 /*-----------------------------------------------------------*/
\r
497 /* This function is explained in the comments at the top of this file. */
\r
498 #pragma inline_asm prvRegTest1Implementation
\r
499 static void prvRegTest1Implementation( void )
\r
501 ; Put a known value in each register.
\r
518 ; Loop, checking each itteration that each register still contains the
\r
522 ; Push the registers that are going to get clobbered.
\r
525 ; Increment the loop counter to show this task is still getting CPU time.
\r
526 MOV.L #_ulRegTest1CycleCount, R14
\r
531 ; Yield to extend the text coverage. Set the bit in the ITU SWINTR register.
\r
533 MOV.L #0872E0H, R15
\r
538 ; Restore the clobbered registers.
\r
541 ; Now compare each register to ensure it still contains the value that was
\r
542 ; set before this loop was entered.
\r
574 ; All comparisons passed, start a new itteratio of this loop.
\r
578 ; A compare failed, just loop here so the loop counter stops incrementing
\r
579 ; causing the check timer to indicate the error.
\r
582 /*-----------------------------------------------------------*/
\r
584 /* This function is explained in the comments at the top of this file. */
\r
585 #pragma inline_asm prvRegTest2Implementation
\r
586 static void prvRegTest2Implementation( void )
\r
588 ; Put a known value in each register.
\r
605 ; Loop, checking on each itteration that each register still contains the
\r
609 ; Push the registers that are going to get clobbered.
\r
612 ; Increment the loop counter to show this task is still getting CPU time.
\r
613 MOV.L #_ulRegTest2CycleCount, R14
\r
618 ; Restore the clobbered registers.
\r
652 ; All comparisons passed, start a new itteratio of this loop.
\r
656 ; A compare failed, just loop here so the loop counter stops incrementing
\r
657 ; - causing the check timer to indicate the error.
\r
660 /*-----------------------------------------------------------*/
\r