2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /* ****************************************************************************
\r
30 * This project includes a lot of tasks and tests and is therefore complex.
\r
31 * If you would prefer a much simpler project to get started with then select
\r
32 * the 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in
\r
33 * FreeRTOSConfig.h. When configCREATE_LOW_POWER_DEMO is set to 1 main() will
\r
34 * call main_low_power() instead of main_full().
\r
35 * ****************************************************************************
\r
37 * Creates all the demo application tasks, then starts the scheduler. The web
\r
38 * documentation provides more details of the standard demo application tasks,
\r
39 * which provide no particular functionality but do provide a good example of
\r
40 * how to use the FreeRTOS API.
\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 three 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 three seconds, to just 200ms. The check timer callback function
\r
62 * also toggles LED 0 each time it is called. This provides a visual
\r
63 * indication of the system status: If the LED toggles every three 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 * *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.
\r
68 * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
\r
69 * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
\r
72 /* Standard includes. */
\r
75 /* Hardware specific includes. */
\r
76 #include "iodefine.h"
\r
78 /* Kernel includes. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Standard demo includes. */
\r
85 #include "partest.h"
\r
87 #include "blocktim.h"
\r
88 #include "GenQTest.h"
\r
89 #include "recmutex.h"
\r
91 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set
\r
92 to 0, otherwise the code in main_low_power.c is used. */
\r
93 #if configCREATE_LOW_POWER_DEMO == 0
\r
96 /* Values that are passed into the reg test tasks using the task parameter.
\r
97 The tasks check that the values are passed in correctly. */
\r
98 #define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
\r
99 #define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
\r
101 /* Priorities at which the standard demo tasks are created. */
\r
102 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
103 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
105 /* The LED toggled by the check timer. */
\r
106 #define mainCHECK_LED ( 0 )
\r
108 /* The period at which the check timer will expire, in ms, provided no errors
\r
109 have been reported by any of the standard demo tasks. ms are converted to the
\r
110 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
111 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
113 /* The period at which the check timer will expire, in ms, if an error has been
\r
114 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
115 in ticks using the portTICK_PERIOD_MS constant. */
\r
116 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
118 /* A block time of zero simple means "Don't Block". */
\r
119 #define mainDONT_BLOCK ( 0UL )
\r
122 * The reg test tasks as described at the top of this file.
\r
124 static void prvRegTest1Task( void *pvParameters );
\r
125 static void prvRegTest2Task( void *pvParameters );
\r
128 * The actual implementation of the reg test functionality, which, because of
\r
129 * the direct register access, have to be in assembly.
\r
131 static void prvRegTest1Implementation( void ) __attribute__(( naked ));
\r
132 static void prvRegTest2Implementation( void ) __attribute__(( naked ));
\r
135 * The check timer callback function, as described at the top of this file.
\r
137 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
140 /*-----------------------------------------------------------*/
\r
142 /* Variables that are incremented on each iteration of the reg test tasks -
\r
143 provided the tasks have not reported any errors. The check timer inspects these
\r
144 variables to ensure they are still incrementing as expected. If a variable
\r
145 stops incrementing then it is likely that its associated task has stalled. */
\r
146 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
\r
148 /* The check timer. This uses prvCheckTimerCallback() as its callback
\r
150 static TimerHandle_t xCheckTimer = NULL;
\r
152 /*-----------------------------------------------------------*/
\r
154 void main_full( void )
\r
156 /* Start the reg test tasks which test the context switching mechanism. */
\r
157 xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
158 xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
\r
160 /* Create the standard demo tasks. */
\r
161 vCreateBlockTimeTasks();
\r
162 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
163 vStartRecursiveMutexTasks();
\r
165 /* The suicide tasks must be created last as they need to know how many
\r
166 tasks were running prior to their creation in order to ascertain whether
\r
167 or not the correct/expected number of tasks are running at any given time. */
\r
168 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
170 /* Create the software timer that performs the 'check' functionality,
\r
171 as described at the top of this file. */
\r
172 xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
\r
173 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
\r
174 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
175 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
176 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
179 configASSERT( xCheckTimer );
\r
181 /* Start the check timer. It will actually start when the scheduler is
\r
183 xTimerStart( xCheckTimer, mainDONT_BLOCK );
\r
185 /* Start the tasks running. */
\r
186 vTaskStartScheduler();
\r
188 /* If all is well execution will never reach here as the scheduler will be
\r
189 running. If this null loop is reached then it is likely there was
\r
190 insufficient FreeRTOS heap available for the idle task and/or timer task to
\r
191 be created. See http://www.freertos.org/a00111.html. */
\r
194 /*-----------------------------------------------------------*/
\r
196 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
198 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
\r
199 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
\r
201 /* Remove compiler warnings about unused parameters. */
\r
204 /* Check the standard demo tasks are running without error. */
\r
205 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
207 lErrorStatus = pdFAIL;
\r
209 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
211 lErrorStatus = pdFAIL;
\r
213 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
215 lErrorStatus = pdFAIL;
\r
217 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
219 lErrorStatus = pdFAIL;
\r
222 /* Check the reg test tasks are still cycling. They will stop incrementing
\r
223 their loop counters if they encounter an error. */
\r
224 if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
\r
226 lErrorStatus = pdFAIL;
\r
229 if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
\r
231 lErrorStatus = pdFAIL;
\r
234 /* Remember the loop counter values this time around so they can be checked
\r
235 again the next time this callback function executes. */
\r
236 ulLastRegTest1CycleCount = ulRegTest1CycleCount;
\r
237 ulLastRegTest2CycleCount = ulRegTest2CycleCount;
\r
239 /* Toggle the check LED to give an indication of the system status. If
\r
240 the LED toggles every three seconds then everything is ok. A faster toggle
\r
241 indicates an error. */
\r
242 vParTestToggleLED( mainCHECK_LED );
\r
244 /* Was an error detected this time through the callback execution? */
\r
245 if( lErrorStatus != pdPASS )
\r
247 if( lChangedTimerPeriodAlready == pdFALSE )
\r
249 lChangedTimerPeriodAlready = pdTRUE;
\r
251 /* This call to xTimerChangePeriod() uses a zero block time.
\r
252 Functions called from inside of a timer callback function must
\r
253 *never* attempt to block. */
\r
254 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
258 /*-----------------------------------------------------------*/
\r
260 /* This function is explained in the comments at the top of this file. */
\r
261 static void prvRegTest1Task( void *pvParameters )
\r
263 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
\r
265 /* The parameter did not contain the expected value. */
\r
268 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
269 taskDISABLE_INTERRUPTS();
\r
273 /* This is an inline asm function that never returns. */
\r
274 prvRegTest1Implementation();
\r
276 /*-----------------------------------------------------------*/
\r
278 /* This function is explained in the comments at the top of this file. */
\r
279 static void prvRegTest2Task( void *pvParameters )
\r
281 if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
\r
283 /* The parameter did not contain the expected value. */
\r
286 /* Stop the tick interrupt so its obvious something has gone wrong. */
\r
287 taskDISABLE_INTERRUPTS();
\r
291 /* This is an inline asm function that never returns. */
\r
292 prvRegTest2Implementation();
\r
294 /*-----------------------------------------------------------*/
\r
296 /* This function is explained in the comments at the top of this file. */
\r
297 static void prvRegTest1Implementation( void )
\r
301 /* Set each register to a known value. */
\r
302 " MOV.L #0x33333333, R15 \n\t"
\r
303 " MVTACHI R15 \n\t"
\r
304 " MOV.L #0x44444444, R15 \n\t"
\r
305 " MVTACLO R15 \n\t"
\r
306 " MOV.L #1, R1 \n\t"
\r
307 " MOV.L #2, R2 \n\t"
\r
308 " MOV.L #3, R3 \n\t"
\r
309 " MOV.L #4, R4 \n\t"
\r
310 " MOV.L #5, R5 \n\t"
\r
311 " MOV.L #6, R6 \n\t"
\r
312 " MOV.L #7, R7 \n\t"
\r
313 " MOV.L #8, R8 \n\t"
\r
314 " MOV.L #9, R9 \n\t"
\r
315 " MOV.L #10, R10 \n\t"
\r
316 " MOV.L #11, R11 \n\t"
\r
317 " MOV.L #12, R12 \n\t"
\r
318 " MOV.L #13, R13 \n\t"
\r
319 " MOV.L #14, R14 \n\t"
\r
320 " MOV.L #15, R15 \n\t"
\r
322 /* Loop, checking each iteration that each register still contains the
\r
326 /* Push the registers that are going to get clobbered. */
\r
327 " PUSHM R14-R15 \n\t"
\r
329 /* Increment the loop counter to show this task is still getting CPU
\r
331 " MOV.L #_ulRegTest1CycleCount, R14 \n\t"
\r
332 " MOV.L [ R14 ], R15 \n\t"
\r
333 " ADD #1, R15 \n\t"
\r
334 " MOV.L R15, [ R14 ] \n\t"
\r
336 /* Yield to extend the text coverage. Set the bit in the ITU SWINTR
\r
338 " MOV.L #1, R14 \n\t"
\r
339 " MOV.L #0872E0H, R15 \n\t"
\r
340 " MOV.B R14, [R15] \n\t"
\r
344 /* Check the accumulator value. */
\r
345 " MVFACHI R15 \n\t"
\r
346 " CMP #0x33333333, R15 \n\t"
\r
347 " BNE RegTest2Error \n\t"
\r
348 " MVFACMI R15 \n\t"
\r
349 " CMP #0x33334444, R15 \n\t"
\r
350 " BNE RegTest2Error \n\t"
\r
352 /* Restore the clobbered registers. */
\r
353 " POPM R14-R15 \n\t"
\r
355 /* Now compare each register to ensure it still contains the value that
\r
356 was set before this loop was entered. */
\r
358 " BNE RegTest1Error \n\t"
\r
360 " BNE RegTest1Error \n\t"
\r
362 " BNE RegTest1Error \n\t"
\r
364 " BNE RegTest1Error \n\t"
\r
366 " BNE RegTest1Error \n\t"
\r
368 " BNE RegTest1Error \n\t"
\r
370 " BNE RegTest1Error \n\t"
\r
372 " BNE RegTest1Error \n\t"
\r
374 " BNE RegTest1Error \n\t"
\r
375 " CMP #10, R10 \n\t"
\r
376 " BNE RegTest1Error \n\t"
\r
377 " CMP #11, R11 \n\t"
\r
378 " BNE RegTest1Error \n\t"
\r
379 " CMP #12, R12 \n\t"
\r
380 " BNE RegTest1Error \n\t"
\r
381 " CMP #13, R13 \n\t"
\r
382 " BNE RegTest1Error \n\t"
\r
383 " CMP #14, R14 \n\t"
\r
384 " BNE RegTest1Error \n\t"
\r
385 " CMP #15, R15 \n\t"
\r
386 " BNE RegTest1Error \n\t"
\r
388 /* All comparisons passed, start a new iteration of this loop. */
\r
389 " BRA TestLoop1 \n\t"
\r
391 /* A compare failed, just loop here so the loop counter stops
\r
392 incrementing causing the check timer to indicate the error. */
\r
393 "RegTest1Error: \n\t"
\r
394 " BRA RegTest1Error "
\r
397 /*-----------------------------------------------------------*/
\r
399 /* This function is explained in the comments at the top of this file. */
\r
400 static void prvRegTest2Implementation( void )
\r
404 /* Set each register to a known value. */
\r
405 " MOV.L #0x11111111, R15 \n\t"
\r
406 " MVTACHI R15 \n\t"
\r
407 " MOV.L #0x22222222, R15 \n\t"
\r
408 " MVTACLO R15 \n\t"
\r
409 " MOV.L #100, R1 \n\t"
\r
410 " MOV.L #200, R2 \n\t"
\r
411 " MOV.L #300, R3 \n\t"
\r
412 " MOV.L #400, R4 \n\t"
\r
413 " MOV.L #500, R5 \n\t"
\r
414 " MOV.L #600, R6 \n\t"
\r
415 " MOV.L #700, R7 \n\t"
\r
416 " MOV.L #800, R8 \n\t"
\r
417 " MOV.L #900, R9 \n\t"
\r
418 " MOV.L #1000, R10 \n\t"
\r
419 " MOV.L #1001, R11 \n\t"
\r
420 " MOV.L #1002, R12 \n\t"
\r
421 " MOV.L #1003, R13 \n\t"
\r
422 " MOV.L #1004, R14 \n\t"
\r
423 " MOV.L #1005, R15 \n\t"
\r
425 /* Loop, checking each iteration that each register still contains the
\r
429 /* Push the registers that are going to get clobbered. */
\r
430 " PUSHM R14-R15 \n\t"
\r
432 /* Increment the loop counter to show this task is still getting CPU
\r
434 " MOV.L #_ulRegTest2CycleCount, R14 \n\t"
\r
435 " MOV.L [ R14 ], R15 \n\t"
\r
436 " ADD #1, R15 \n\t"
\r
437 " MOV.L R15, [ R14 ] \n\t"
\r
439 /* Check the accumulator value. */
\r
440 " MVFACHI R15 \n\t"
\r
441 " CMP #0x11111111, R15 \n\t"
\r
442 " BNE RegTest2Error \n\t"
\r
443 " MVFACMI R15 \n\t"
\r
444 " CMP #0x11112222, R15 \n\t"
\r
445 " BNE RegTest2Error \n\t"
\r
447 /* Restore the clobbered registers. */
\r
448 " POPM R14-R15 \n\t"
\r
450 /* Now compare each register to ensure it still contains the value that
\r
451 was set before this loop was entered. */
\r
452 " CMP #100, R1 \n\t"
\r
453 " BNE RegTest2Error \n\t"
\r
454 " CMP #200, R2 \n\t"
\r
455 " BNE RegTest2Error \n\t"
\r
456 " CMP #300, R3 \n\t"
\r
457 " BNE RegTest2Error \n\t"
\r
458 " CMP #400, R4 \n\t"
\r
459 " BNE RegTest2Error \n\t"
\r
460 " CMP #500, R5 \n\t"
\r
461 " BNE RegTest2Error \n\t"
\r
462 " CMP #600, R6 \n\t"
\r
463 " BNE RegTest2Error \n\t"
\r
464 " CMP #700, R7 \n\t"
\r
465 " BNE RegTest2Error \n\t"
\r
466 " CMP #800, R8 \n\t"
\r
467 " BNE RegTest2Error \n\t"
\r
468 " CMP #900, R9 \n\t"
\r
469 " BNE RegTest2Error \n\t"
\r
470 " CMP #1000, R10 \n\t"
\r
471 " BNE RegTest2Error \n\t"
\r
472 " CMP #1001, R11 \n\t"
\r
473 " BNE RegTest2Error \n\t"
\r
474 " CMP #1002, R12 \n\t"
\r
475 " BNE RegTest2Error \n\t"
\r
476 " CMP #1003, R13 \n\t"
\r
477 " BNE RegTest2Error \n\t"
\r
478 " CMP #1004, R14 \n\t"
\r
479 " BNE RegTest2Error \n\t"
\r
480 " CMP #1005, R15 \n\t"
\r
481 " BNE RegTest2Error \n\t"
\r
483 /* All comparisons passed, start a new iteration of this loop. */
\r
484 " BRA TestLoop2 \n\t"
\r
486 /* A compare failed, just loop here so the loop counter stops
\r
487 incrementing causing the check timer to indicate the error. */
\r
488 "RegTest2Error: \n\t"
\r
489 " BRA RegTest2Error "
\r
492 /*-----------------------------------------------------------*/
\r
494 #endif /* configCREATE_LOW_POWER_DEMO */
\r