2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* ****************************************************************************
\r
29 * This project includes a lot of tasks and tests and is therefore complex.
\r
30 * If you would prefer a much simpler project to get started with then select
\r
31 * the 'Blinky' build configuration within the HEW IDE. The Blinky build
\r
32 * configuration uses main-blinky.c instead of main-full.c.
\r
33 * ****************************************************************************
\r
35 * Creates all the demo application tasks, then starts the scheduler. The web
\r
36 * documentation provides more details of the standard demo application tasks,
\r
37 * which provide no particular functionality but do provide a good example of
\r
38 * how to use the FreeRTOS API. The tasks defined in flop.c are included in the
\r
39 * set of standard demo tasks to ensure the floating point unit gets some
\r
42 * In addition to the standard demo tasks, the following tasks and tests are
\r
43 * defined and/or created within this file:
\r
45 * "Reg test" tasks - These fill the registers with known values, then
\r
46 * repeatedly check that each register still contains its expected value for
\r
47 * the lifetime of the tasks. Each task uses different values. The tasks run
\r
48 * with very low priority so get preempted very frequently. A check variable
\r
49 * is incremented on each iteration of the test loop. A register containing an
\r
50 * unexpected value is indicative of an error in the context switching
\r
51 * mechanism and will result in a branch to a null loop - which in turn will
\r
52 * prevent the check variable from incrementing any further and allow the check
\r
53 * timer (described below) to determine that an error has occurred. The nature
\r
54 * of the reg test tasks necessitates that they are written in assembly code.
\r
56 * "Check Timer" and Callback Function - The check timer period is initially
\r
57 * set to five seconds. The check timer callback function checks that all the
\r
58 * standard demo tasks are not only still executing, but are executing without
\r
59 * reporting any errors. If the check timer discovers that a task has either
\r
60 * stalled, or reported an error, then it changes its own period from the
\r
61 * initial five seconds, to just 200ms. The check timer callback function
\r
62 * also toggles LED 3 each time it is called. This provides a visual
\r
63 * indication of the system status: If the LED toggles every five seconds,
\r
64 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
65 * an issue has been discovered with at least one task.
\r
67 * "High frequency timer test" - A high frequency periodic interrupt is
\r
68 * generated using a timer - the interrupt is assigned a priority above
\r
69 * configMAX_SYSCALL_INTERRUPT_PRIORITY, so will not be effected by anything
\r
70 * the kernel is doing. The frequency and priority of the interrupt, in
\r
71 * combination with other standard tests executed in this demo, will result
\r
72 * in interrupts nesting at least 3 and probably 4 deep. This test is only
\r
73 * included in build configurations that have the optimiser switched on.
\r
75 * *NOTE 1* If LED3 is toggling every 5 seconds then all the demo application
\r
76 * tasks are executing as expected and no errors have been reported in any
\r
77 * tasks. The toggle rate increasing to 200ms indicates that at least one task
\r
78 * has reported unexpected behaviour.
\r
80 * *NOTE 2* vApplicationSetupTimerInterrupt() is called by the kernel to let
\r
81 * the application set up a timer to generate the tick interrupt. In this
\r
82 * example a compare match timer is used for this purpose.
\r
84 * *NOTE 3* The CPU must be in Supervisor mode when the scheduler is started.
\r
85 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
86 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
88 * *NOTE 4* The IntQueue common demo tasks test interrupt nesting and make use
\r
89 * of all the 8bit timers (as two cascaded 16bit units).
\r
92 /* Hardware specific includes. */
\r
93 #include "iodefine.h"
\r
95 /* Kernel includes. */
\r
96 #include "FreeRTOS.h"
\r
101 /* Standard demo includes. */
\r
102 #include "partest.h"
\r
104 #include "IntQueue.h"
\r
105 #include "BlockQ.h"
\r
107 #include "integer.h"
\r
108 #include "blocktim.h"
\r
109 #include "semtest.h"
\r
111 #include "GenQTest.h"
\r
113 #include "recmutex.h"
\r
116 /* Values that are passed into the reg test tasks using the task parameter. The
\r
117 tasks check that the values are passed in correctly. */
\r
118 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
119 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
121 /* Priorities at which the tasks are created. */
\r
122 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
123 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
124 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
125 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
126 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
127 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
128 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
129 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
131 /* The LED toggled by the check timer. */
\r
132 #define mainCHECK_LED ( 3 )
\r
134 /* The period at which the check timer will expire, in ms, provided no errors
\r
135 have been reported by any of the standard demo tasks. ms are converted to the
\r
136 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
137 #define mainCHECK_TIMER_PERIOD_MS ( 5000UL / portTICK_PERIOD_MS )
\r
139 /* The period at which the check timer will expire, in ms, if an error has been
\r
140 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
141 in ticks using the portTICK_PERIOD_MS constant. */
\r
142 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
144 /* A block time of zero simple means "Don't Block". */
\r
145 #define mainDONT_BLOCK ( 0UL )
\r
148 * vApplicationMallocFailedHook() will only be called if
\r
149 * configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
\r
150 * function that will execute if a call to pvPortMalloc() fails.
\r
151 * pvPortMalloc() is called internally by the kernel whenever a task, queue or
\r
152 * semaphore is created. It is also called by various parts of the demo
\r
155 void vApplicationMallocFailedHook( void );
\r
158 * vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set to 1
\r
159 * in FreeRTOSConfig.h. It is a hook function that is called on each iteration
\r
160 * of the idle task. It is essential that code added to this hook function
\r
161 * never attempts to block in any way (for example, call xQueueReceive() with
\r
162 * a block time specified). If the application makes use of the vTaskDelete()
\r
163 * API function (as this demo application does) then it is also important that
\r
164 * vApplicationIdleHook() is permitted to return to its calling function because
\r
165 * it is the responsibility of the idle task to clean up memory allocated by the
\r
166 * kernel to any task that has since been deleted.
\r
168 void vApplicationIdleHook( void );
\r
171 * vApplicationStackOverflowHook() will only be called if
\r
172 * configCHECK_FOR_STACK_OVERFLOW is set to a non-zero value. The handle and
\r
173 * name of the offending task should be passed in the function parameters, but
\r
174 * it is possible that the stack overflow will have corrupted these - in which
\r
175 * case pxCurrentTCB can be inspected to find the same information.
\r
177 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
180 * The reg test tasks as described at the top of this file.
\r
182 static void prvRegTest1Task( void *pvParameters );
\r
183 static void prvRegTest2Task( void *pvParameters );
\r
186 * The actual implementation of the reg test functionality, which, because of
\r
187 * the direct register access, have to be in assembly.
\r
189 static void prvRegTest1Implementation( void );
\r
190 static void prvRegTest2Implementation( void );
\r
193 * The check timer callback function, as described at the top of this file.
\r
195 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
198 /*-----------------------------------------------------------*/
\r
200 /* Variables that are incremented on each iteration of the reg test tasks -
\r
201 provided the tasks have not reported any errors. The check timer inspects these
\r
202 variables to ensure they are still incrementing as expected. If a variable
\r
203 stops incrementing then it is likely that its associate task has stalled. */
\r
204 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
206 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
208 static TimerHandle_t xCheckTimer = NULL;
\r
210 /*-----------------------------------------------------------*/
\r
214 extern void HardwareSetup( void );
\r
216 /* Renesas provided CPU configuration routine. The clocks are configured in
\r
220 /* Turn all LEDs off. */
\r
221 vParTestInitialise();
\r
223 /* Start the reg test tasks which test the context switching mechanism. */
\r
224 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
225 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
227 /* Create the standard demo tasks. */
\r
228 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
229 vCreateBlockTimeTasks();
\r
230 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
231 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
232 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
233 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
234 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
235 vStartQueuePeekTasks();
\r
236 vStartRecursiveMutexTasks();
\r
237 vStartInterruptQueueTasks();
\r
238 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
240 /* The suicide tasks must be created last as they need to know how many
\r
241 tasks were running prior to their creation in order to ascertain whether
\r
242 or not the correct/expected number of tasks are running at any given time. */
\r
243 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
245 /* Create the software timer that performs the 'check' functionality,
\r
246 as described at the top of this file. */
\r
247 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
248 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
249 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
250 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
251 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
254 /* Sanity check that the check timer was indeed created. */
\r
255 configASSERT( xCheckTimer );
\r
257 /* Start the check timer. It will actually start when the scheduler is
\r
259 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
261 /* Start the tasks running. */
\r
262 vTaskStartScheduler();
\r
264 /* If all is well, the following line will never be reached as the scheduler
\r
265 will be running. If the following line is reached, there was insufficient
\r
266 FreeRTOS heap available for the idle task to be created. See
\r
267 http://www.freertos.org/a00111.html and the malloc failed hook function for
\r
268 more information. */
\r
271 /*-----------------------------------------------------------*/
\r
273 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
275 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
276 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
278 /* Check the standard demo tasks are running without error. */
\r
279 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
281 lErrorStatus = pdFAIL;
\r
283 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
285 lErrorStatus = pdFAIL;
\r
287 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
289 lErrorStatus = pdFAIL;
\r
291 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
293 lErrorStatus = pdFAIL;
\r
295 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
297 lErrorStatus = pdFAIL;
\r
299 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
301 lErrorStatus = pdFAIL;
\r
303 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
305 lErrorStatus = pdFAIL;
\r
307 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
309 lErrorStatus = pdFAIL;
\r
311 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
313 lErrorStatus = pdFAIL;
\r
315 else if( xAreIntQueueTasksStillRunning() != pdPASS )
\r
317 lErrorStatus = pdFAIL;
\r
319 else if( xAreMathsTaskStillRunning() != pdPASS )
\r
321 lErrorStatus = pdFAIL;
\r
324 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
325 their loop counters if they encounter an error. */
\r
326 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
328 lErrorStatus = pdFAIL;
\r
331 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
333 lErrorStatus = pdFAIL;
\r
336 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
337 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
339 /* Toggle the check LED to give an indication of the system status. If
\r
340 the LED toggles every 5 seconds then everything is ok. A faster toggle
\r
341 indicates an error. */
\r
342 vParTestToggleLED( mainCHECK_LED );
\r
344 /* Was an error detected this time through the callback execution? */
\r
345 if( lErrorStatus != pdPASS )
\r
347 if( lChangedTimerPeriodAlready == pdFALSE )
\r
349 lChangedTimerPeriodAlready = pdTRUE;
\r
351 /* This call to xTimerChangePeriod() uses a zero block time.
\r
352 Functions called from inside of a timer callback function must
\r
353 *never* attempt to block. */
\r
354 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
358 /*-----------------------------------------------------------*/
\r
360 /* The RX port uses this callback function to configure its tick interrupt.
\r
361 This allows the application to choose the tick interrupt source. */
\r
362 void vApplicationSetupTimerInterrupt( void )
\r
364 /* Enable compare match timer 0. */
\r
367 /* Interrupt on compare match. */
\r
368 CMT0.CMCR.BIT.CMIE = 1;
\r
370 /* Set the compare match value. */
\r
371 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
373 /* Divide the PCLK by 8. */
\r
374 CMT0.CMCR.BIT.CKS = 0;
\r
376 /* Enable the interrupt... */
\r
377 _IEN( _CMT0_CMI0 ) = 1;
\r
379 /* ...and set its priority to the application defined kernel priority. */
\r
380 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
382 /* Start the timer. */
\r
383 CMT.CMSTR0.BIT.STR0 = 1;
\r
385 /*-----------------------------------------------------------*/
\r
387 /* This function is explained by the comments above its prototype at the top
\r
389 void vApplicationMallocFailedHook( void )
\r
393 /*-----------------------------------------------------------*/
\r
395 /* This function is explained by the comments above its prototype at the top
\r
397 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
401 /*-----------------------------------------------------------*/
\r
403 /* This function is explained by the comments above its prototype at the top
\r
405 void vApplicationIdleHook( void )
\r
407 /* If this is being executed then the kernel has been started. Start the high
\r
408 frequency timer test as described at the top of this file. This is only
\r
409 included in the optimised build configuration - otherwise it takes up too much
\r
410 CPU time and can disrupt other tests. */
\r
411 #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
\r
412 static portBASE_TYPE xTimerTestStarted = pdFALSE;
\r
413 extern void vSetupHighFrequencyTimer( void );
\r
414 if( xTimerTestStarted == pdFALSE )
\r
416 vSetupHighFrequencyTimer();
\r
417 xTimerTestStarted = pdTRUE;
\r
421 /*-----------------------------------------------------------*/
\r
423 /* This function is explained in the comments at the top of this file. */
\r
424 static void prvRegTest1Task( void *pvParameters )
\r
426 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
428 /* The parameter did not contain the expected value. */
\r
431 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
432 taskDISABLE_INTERRUPTS();
\r
436 /* This is an inline asm function that never returns. */
\r
437 prvRegTest1Implementation();
\r
439 /*-----------------------------------------------------------*/
\r
441 /* This function is explained in the comments at the top of this file. */
\r
442 static void prvRegTest2Task( void *pvParameters )
\r
444 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
446 /* The parameter did not contain the expected value. */
\r
449 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
450 taskDISABLE_INTERRUPTS();
\r
454 /* This is an inline asm function that never returns. */
\r
455 prvRegTest2Implementation();
\r
457 /*-----------------------------------------------------------*/
\r
459 /* This function is explained in the comments at the top of this file. */
\r
460 #pragma inline_asm prvRegTest1Implementation
\r
461 static void prvRegTest1Implementation( void )
\r
463 ; Put a known value in each register.
\r
480 ; Loop, checking each iteration that each register still contains the
\r
484 ; Push the registers that are going to get clobbered.
\r
487 ; Increment the loop counter to show this task is still getting CPU time.
\r
488 MOV.L #_ulRegTest1CycleCount, R14
\r
493 ; Yield to extend the text coverage. Set the bit in the ITU SWINTR register.
\r
495 MOV.L #0872E0H, R15
\r
500 ; Restore the clobbered registers.
\r
503 ; Now compare each register to ensure it still contains the value that was
\r
504 ; set before this loop was entered.
\r
536 ; All comparisons passed, start a new itteratio of this loop.
\r
540 ; A compare failed, just loop here so the loop counter stops incrementing
\r
541 ; causing the check timer to indicate the error.
\r
544 /*-----------------------------------------------------------*/
\r
546 /* This function is explained in the comments at the top of this file. */
\r
547 #pragma inline_asm prvRegTest2Implementation
\r
548 static void prvRegTest2Implementation( void )
\r
550 ; Put a known value in each register.
\r
567 ; Loop, checking on each iteration that each register still contains the
\r
571 ; Push the registers that are going to get clobbered.
\r
574 ; Increment the loop counter to show this task is still getting CPU time.
\r
575 MOV.L #_ulRegTest2CycleCount, R14
\r
580 ; Restore the clobbered registers.
\r
614 ; All comparisons passed, start a new itteratio of this loop.
\r
618 ; A compare failed, just loop here so the loop counter stops incrementing
\r
619 ; - causing the check timer to indicate the error.
\r
622 /*-----------------------------------------------------------*/
\r