2 * FreeRTOS Kernel V10.2.0
\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 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in
\r
32 * FreeRTOSConfig.h. When configCREATE_LOW_POWER_DEMO is set to 1 main() will
\r
33 * call main_low_power() instead of main_full().
\r
34 * ****************************************************************************
\r
36 * Creates all the demo application tasks, then starts the scheduler. The web
\r
37 * documentation provides more details of the standard demo application tasks,
\r
38 * which provide no particular functionality but do provide a good example of
\r
39 * how to use the FreeRTOS API.
\r
41 * In addition to the standard demo tasks, the following tasks and tests are
\r
42 * defined and/or created within this file:
\r
44 * "Reg test" tasks - These fill the registers with known values, then
\r
45 * repeatedly check that each register still contains its expected value for
\r
46 * the lifetime of the tasks. Each task uses different values. The tasks run
\r
47 * with very low priority so get preempted very frequently. A check variable
\r
48 * is incremented on each iteration of the test loop. A register containing an
\r
49 * unexpected value is indicative of an error in the context switching
\r
50 * mechanism and will result in a branch to a null loop - which in turn will
\r
51 * prevent the check variable from incrementing any further and allow the check
\r
52 * timer (described below) to determine that an error has occurred. The nature
\r
53 * of the reg test tasks necessitates that they are written in assembly code.
\r
55 * "Check Timer" and Callback Function - The check timer period is initially
\r
56 * set to three seconds. The check timer callback function checks that all the
\r
57 * standard demo tasks are not only still executing, but are executing without
\r
58 * reporting any errors. If the check timer discovers that a task has either
\r
59 * stalled, or reported an error, then it changes its own period from the
\r
60 * initial three seconds, to just 200ms. The check timer callback function
\r
61 * also toggles LED 0 each time it is called. This provides a visual
\r
62 * indication of the system status: If the LED toggles every three seconds,
\r
63 * then no issues have been discovered. If the LED toggles every 200ms, then
\r
64 * an issue has been discovered with at least one task.
\r
66 * *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.
\r
67 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
68 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
71 /* Standard includes. */
\r
74 /* Hardware specific includes. */
\r
75 #include "iodefine.h"
\r
77 /* Kernel includes. */
\r
78 #include "FreeRTOS.h"
\r
83 /* Standard demo includes. */
\r
84 #include "partest.h"
\r
86 #include "blocktim.h"
\r
87 #include "GenQTest.h"
\r
88 #include "recmutex.h"
\r
90 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set
\r
91 to 0, otherwise the code in main_low_power.c is used. */
\r
92 #if configCREATE_LOW_POWER_DEMO == 0
\r
95 /* Values that are passed into the reg test tasks using the task parameter.
\r
96 The tasks check that the values are passed in correctly. */
\r
97 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
98 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
100 /* Priorities at which the standard demo tasks are created. */
\r
101 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
102 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
104 /* The LED toggled by the check timer. */
\r
105 #define mainCHECK_LED ( 0 )
\r
107 /* The period at which the check timer will expire, in ms, provided no errors
\r
108 have been reported by any of the standard demo tasks. ms are converted to the
\r
109 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
110 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
112 /* The period at which the check timer will expire, in ms, if an error has been
\r
113 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
114 in ticks using the portTICK_PERIOD_MS constant. */
\r
115 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
117 /* A block time of zero simple means "Don't Block". */
\r
118 #define mainDONT_BLOCK ( 0UL )
\r
121 * The reg test tasks as described at the top of this file.
\r
123 static void prvRegTest1Task( void *pvParameters );
\r
124 static void prvRegTest2Task( void *pvParameters );
\r
127 * The actual implementation of the reg test functionality, which, because of
\r
128 * the direct register access, have to be in assembly.
\r
130 static void prvRegTest1Implementation( void );
\r
131 static void prvRegTest2Implementation( void );
\r
134 * The check timer callback function, as described at the top of this file.
\r
136 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
139 /*-----------------------------------------------------------*/
\r
141 /* Variables that are incremented on each iteration of the reg test tasks -
\r
142 provided the tasks have not reported any errors. The check timer inspects these
\r
143 variables to ensure they are still incrementing as expected. If a variable
\r
144 stops incrementing then it is likely that its associated task has stalled. */
\r
145 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
147 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
149 static TimerHandle_t xCheckTimer = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
153 void main_full( void )
\r
155 /* Start the reg test tasks which test the context switching mechanism. */
\r
156 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
157 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
159 /* Create the standard demo tasks. */
\r
160 vCreateBlockTimeTasks();
\r
161 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
162 vStartRecursiveMutexTasks();
\r
164 /* The suicide tasks must be created last as they need to know how many
\r
165 tasks were running prior to their creation in order to ascertain whether
\r
166 or not the correct/expected number of tasks are running at any given time. */
\r
167 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
169 /* Create the software timer that performs the 'check' functionality,
\r
170 as described at the top of this file. */
\r
171 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
172 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
173 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
174 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
175 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
178 configASSERT( xCheckTimer );
\r
180 /* Start the check timer. It will actually start when the scheduler is
\r
182 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
184 /* Start the tasks running. */
\r
185 vTaskStartScheduler();
\r
187 /* If all is well execution will never reach here as the scheduler will be
\r
188 running. If this null loop is reached then it is likely there was
\r
189 insufficient FreeRTOS heap available for the idle task and/or timer task to
\r
190 be created. See http://www.freertos.org/a00111.html. */
\r
193 /*-----------------------------------------------------------*/
\r
195 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
197 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
198 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
200 /* Remove compiler warnings about unused parameters. */
\r
203 /* Check the standard demo tasks are running without error. */
\r
204 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
206 lErrorStatus = pdFAIL;
\r
208 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
210 lErrorStatus = pdFAIL;
\r
212 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
214 lErrorStatus = pdFAIL;
\r
216 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
218 lErrorStatus = pdFAIL;
\r
221 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
222 their loop counters if they encounter an error. */
\r
223 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
225 lErrorStatus = pdFAIL;
\r
228 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
230 lErrorStatus = pdFAIL;
\r
233 /* Remember the loop counter values this time around so they can be checked
\r
234 again the next time this callback function executes. */
\r
235 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
236 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
238 /* Toggle the check LED to give an indication of the system status. If
\r
239 the LED toggles every three seconds then everything is ok. A faster toggle
\r
240 indicates an error. */
\r
241 vParTestToggleLED( mainCHECK_LED );
\r
243 /* Was an error detected this time through the callback execution? */
\r
244 if( lErrorStatus != pdPASS )
\r
246 if( lChangedTimerPeriodAlready == pdFALSE )
\r
248 lChangedTimerPeriodAlready = pdTRUE;
\r
250 /* This call to xTimerChangePeriod() uses a zero block time.
\r
251 Functions called from inside of a timer callback function must
\r
252 *never* attempt to block. */
\r
253 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
257 /*-----------------------------------------------------------*/
\r
259 /* This function is explained in the comments at the top of this file. */
\r
260 static void prvRegTest1Task( void *pvParameters )
\r
262 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
264 /* The parameter did not contain the expected value. */
\r
267 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
268 taskDISABLE_INTERRUPTS();
\r
272 /* This is an inline asm function that never returns. */
\r
273 prvRegTest1Implementation();
\r
275 /*-----------------------------------------------------------*/
\r
277 /* This function is explained in the comments at the top of this file. */
\r
278 static void prvRegTest2Task( void *pvParameters )
\r
280 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
282 /* The parameter did not contain the expected value. */
\r
285 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
286 taskDISABLE_INTERRUPTS();
\r
290 /* This is an inline asm function that never returns. */
\r
291 prvRegTest2Implementation();
\r
293 /*-----------------------------------------------------------*/
\r
295 /* This function is explained in the comments at the top of this file. */
\r
296 #pragma inline_asm prvRegTest1Implementation
\r
297 static void prvRegTest1Implementation( void )
\r
299 ; Put a known value in each register.
\r
300 MOV.L #11111111H, R15
\r
302 MOV.L #22222222H, R15
\r
320 ; Loop, checking each iteration that each register still contains the
\r
324 ; Push the registers that are going to get clobbered.
\r
327 ; Increment the loop counter to show this task is still getting CPU time.
\r
328 MOV.L #_ulRegTest1CycleCount, R14
\r
333 ; Yield to extend the text coverage. Set the bit in the ITU SWINTR register.
\r
335 MOV.L #0872E0H, R15
\r
340 ;Check the accumulator value.
\r
342 CMP #11111111H, R15
\r
345 CMP #11112222H, R15
\r
348 ; Restore the clobbered registers.
\r
351 ; Now compare each register to ensure it still contains the value that was
\r
352 ; set before this loop was entered.
\r
384 ; All comparisons passed, start a new iteration of this loop.
\r
388 ; A compare failed, just loop here so the loop counter stops incrementing
\r
389 ; causing the check timer to indicate the error.
\r
392 /*-----------------------------------------------------------*/
\r
394 /* This function is explained in the comments at the top of this file. */
\r
395 #pragma inline_asm prvRegTest2Implementation
\r
396 static void prvRegTest2Implementation( void )
\r
398 ; Put a known value in each register.
\r
399 MOV.L #33333333H, R15
\r
401 MOV.L #44444444H, R15
\r
419 ; Loop, checking on each iteration that each register still contains the
\r
423 ; Push the registers that are going to get clobbered.
\r
426 ; Increment the loop counter to show this task is still getting CPU time.
\r
427 MOV.L #_ulRegTest2CycleCount, R14
\r
432 ;Check the accumulator value.
\r
434 CMP #33333333H, R15
\r
437 CMP #33334444H, R15
\r
440 ; Restore the clobbered registers.
\r
474 ; All comparisons passed, start a new itteratio of this loop.
\r
478 ; A compare failed, just loop here so the loop counter stops incrementing
\r
479 ; - causing the check timer to indicate the error.
\r
482 /*-----------------------------------------------------------*/
\r
484 #endif /* configCREATE_LOW_POWER_DEMO */
\r