]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB91460_Softune/SRC/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / MB91460_Softune / SRC / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /*\r
30  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
31  * documentation provides more details of the demo application tasks.\r
32  *\r
33  * In addition to the standard demo tasks, the follow demo specific tasks are\r
34  * create:\r
35  *\r
36  * The "Check" task.  This only executes every three seconds but has the highest\r
37  * priority so is guaranteed to get processor time.  Its main function is to\r
38  * check that all the other tasks are still operational.  Most tasks maintain\r
39  * a unique count that is incremented each time the task successfully completes\r
40  * its function.  Should any error occur within such a task the count is\r
41  * permanently halted.  The check task inspects the count of each task to ensure\r
42  * it has changed since the last time the check task executed.  If all the count\r
43  * variables have changed all the tasks are still executing error free, and the\r
44  * check task toggles the onboard LED.  Should any task contain an error at any time\r
45  * the LED toggle rate will change from 3 seconds to 500ms.\r
46  *\r
47  * The "Register Check" tasks.  These tasks fill the CPU registers with known\r
48  * values, then check that each register still contains the expected value 0 the\r
49  * discovery of an unexpected value being indicative of an error in the RTOS\r
50  * context switch mechanism.  The register check tasks operate at low priority\r
51  * so are switched in and out frequently.\r
52  *\r
53  * The "Trace Utility" task.  This can be used to obtain trace and debug\r
54  * information via UART5.\r
55  */\r
56 \r
57 \r
58 /* Hardware specific includes. */\r
59 #include "mb91467d.h"\r
60 #include "vectors.h"\r
61 #include "watchdog.h"\r
62 \r
63 /* Scheduler includes. */\r
64 #include "FreeRTOS.h"\r
65 #include "task.h"\r
66 \r
67 /* Demo app includes. */\r
68 #include "flash.h"\r
69 #include "integer.h"\r
70 #include "comtest2.h"\r
71 #include "semtest.h"\r
72 #include "BlockQ.h"\r
73 #include "dynamic.h"\r
74 #include "flop.h"\r
75 #include "GenQTest.h"\r
76 #include "QPeek.h"\r
77 #include "blocktim.h"\r
78 #include "death.h"\r
79 #include "taskutility.h"\r
80 #include "partest.h"\r
81 #include "crflash.h"\r
82 \r
83 /* Demo task priorities. */\r
84 #define mainWATCHDOG_TASK_PRIORITY              ( tskIDLE_PRIORITY + 5 )\r
85 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 4 )\r
86 #define mainUTILITY_TASK_PRIORITY               ( tskIDLE_PRIORITY )\r
87 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
88 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
89 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 2 )\r
90 #define mainDEATH_PRIORITY                              ( tskIDLE_PRIORITY + 1 )\r
91 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
92 #define mainGENERIC_QUEUE_PRIORITY              ( tskIDLE_PRIORITY )\r
93 \r
94 /* Baud rate used by the COM test tasks. */\r
95 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 19200 )\r
96 \r
97 /* The frequency at which the 'Check' tasks executes.  See the comments at the\r
98 top of the page.  When the system is operating error free the 'Check' task\r
99 toggles an LED every three seconds.  If an error is discovered in any task the\r
100 rate is increased to 500 milliseconds.  [in this case the '*' characters on the\r
101 LCD represent LEDs]*/\r
102 #define mainNO_ERROR_CHECK_DELAY                ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
103 #define mainERROR_CHECK_DELAY                   ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
104 \r
105 /* The total number of LEDs available. */\r
106 #define mainNO_CO_ROUTINE_LEDs  ( 8 )\r
107 \r
108 /* The first LED used by the comtest tasks. */\r
109 #define mainCOM_TEST_LED                ( 0x05 )\r
110 \r
111 /* The LED used by the check task. */\r
112 #define mainCHECK_TEST_LED              ( 0x07 )\r
113 \r
114 /* The number of interrupt levels to use. */\r
115 #define mainINTERRUPT_LEVELS    ( 31 )\r
116 \r
117 /* The number of 'flash' co-routines to create - each toggles a different LED. */\r
118 #define mainNUM_FLASH_CO_ROUTINES       ( 8 )\r
119 \r
120 /*---------------------------------------------------------------------------*/\r
121 \r
122 /*\r
123  * The function that implements the Check task.  See the comments at the head\r
124  * of the page for implementation details.\r
125  */\r
126 static void prvErrorChecks( void *pvParameters );\r
127 \r
128 /*\r
129  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
130  * to be operating without error - otherwise returns pdFAIL.\r
131  */\r
132 static short prvCheckOtherTasksAreStillRunning( void );\r
133 \r
134 /*\r
135  * Setup the microcontroller as used by this demo.\r
136  */\r
137 static void prvSetupHardware( void );\r
138 \r
139 /*\r
140  * Tasks that test the context switch mechanism by filling the CPU registers\r
141  * with known values then checking that each register contains the value\r
142  * expected.  Each of the two tasks use different values, and as low priority\r
143  * tasks, get swapped in and out regularly.\r
144  */\r
145 static void vFirstRegisterTestTask( void *pvParameters );\r
146 static void vSecondRegisterTestTask( void *pvParameters );\r
147 \r
148 /*---------------------------------------------------------------------------*/\r
149 \r
150 /* The variable that is set to true should an error be found in one of the\r
151 register test tasks. */\r
152 unsigned long ulRegTestError = pdFALSE;\r
153 \r
154 /*---------------------------------------------------------------------------*/\r
155 \r
156 /* Start all the demo application tasks, then start the scheduler. */\r
157 void main(void)\r
158 {\r
159         /* Initialise the hardware ready for the demo. */\r
160         prvSetupHardware();\r
161 \r
162         /* Start the standard demo application tasks. */\r
163         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
164         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
165         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
166         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
167         vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );\r
168         vStartDynamicPriorityTasks();\r
169         vStartMathTasks( tskIDLE_PRIORITY );\r
170         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
171         vStartQueuePeekTasks();\r
172         vCreateBlockTimeTasks();\r
173         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
174 \r
175         /* Start the 'Check' task which is defined in this file. */\r
176         xTaskCreate( prvErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
177 \r
178         /* Start the 'Register Test' tasks as described at the top of this file. */\r
179         xTaskCreate( vFirstRegisterTestTask, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
180         xTaskCreate( vSecondRegisterTestTask, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
181 \r
182         /* Start the task that write trace information to the UART. */\r
183         vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );\r
184 \r
185         /* If we are going to service the watchdog from within a task, then create\r
186         the task here. */\r
187         #if WATCHDOG == WTC_IN_TASK\r
188                 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );\r
189         #endif\r
190 \r
191         /* The suicide tasks must be started last as they record the number of other\r
192         tasks that exist within the system.  The value is then used to ensure at run\r
193         time the number of tasks that exists is within expected bounds. */\r
194         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
195 \r
196         /* Now start the scheduler.  Following this call the created tasks should\r
197         be executing. */\r
198         vTaskStartScheduler( );\r
199 \r
200         /* vTaskStartScheduler() will only return if an error occurs while the\r
201         idle task is being created. */\r
202         for( ;; );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvErrorChecks( void *pvParameters )\r
207 {\r
208 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;\r
209 \r
210         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
211         works correctly. */\r
212         xLastExecutionTime = xTaskGetTickCount();\r
213 \r
214         /* Cycle for ever, delaying then checking all the other tasks are still\r
215         operating without error. */\r
216         for( ;; )\r
217         {\r
218                 /* Wait until it is time to check again.  The time we wait here depends\r
219                 on whether an error has been detected or not.  When an error is\r
220                 detected the time is shortened resulting in a faster LED flash rate. */\r
221                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
222                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
223 \r
224                 /* See if the other tasks are all ok. */\r
225                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
226                 {\r
227                         /* An error occurred in one of the tasks so shorten the delay\r
228                         period - which has the effect of increasing the frequency of the\r
229                         LED toggle. */\r
230                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
231                 }\r
232 \r
233                 /* Flash! */\r
234                 vParTestToggleLED( mainCHECK_TEST_LED );\r
235         }\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 static short prvCheckOtherTasksAreStillRunning( void )\r
240 {\r
241 portBASE_TYPE lReturn = pdPASS;\r
242 \r
243         /* The demo tasks maintain a count that increments every cycle of the task\r
244         provided that the task has never encountered an error.  This function\r
245         checks the counts maintained by the tasks to ensure they are still being\r
246         incremented.  A count remaining at the same value between calls therefore\r
247         indicates that an error has been detected. */\r
248 \r
249         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
250         {\r
251                 lReturn = pdFAIL;\r
252         }\r
253 \r
254         if( xAreComTestTasksStillRunning() != pdTRUE )\r
255         {\r
256                 lReturn = pdFAIL;\r
257         }\r
258 \r
259         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
260         {\r
261                 lReturn = pdFAIL;\r
262         }\r
263 \r
264         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
265         {\r
266                 lReturn = pdFAIL;\r
267         }\r
268 \r
269         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
270         {\r
271                 lReturn = pdFAIL;\r
272         }\r
273 \r
274         if( xAreMathsTaskStillRunning() != pdTRUE )\r
275         {\r
276                 lReturn = pdFAIL;\r
277         }\r
278 \r
279         if( xIsCreateTaskStillRunning() != pdTRUE )\r
280         {\r
281                 lReturn = pdFAIL;\r
282         }\r
283 \r
284         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
285         {\r
286                 lReturn = pdFAIL;\r
287         }\r
288 \r
289         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
290         {\r
291                 lReturn = pdFAIL;\r
292         }\r
293 \r
294         if ( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
295         {\r
296                 lReturn = pdFAIL;\r
297         }\r
298 \r
299         /* Have the register test tasks found any errors? */\r
300         if( ulRegTestError != pdFALSE )\r
301         {\r
302                 lReturn = pdFAIL;\r
303         }\r
304 \r
305         return lReturn;\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static void prvSetupHardware( void )\r
310 {\r
311         /* Allow all interrupt levels. */\r
312         __set_il( mainINTERRUPT_LEVELS );\r
313 \r
314         /* Initialise interrupts. */\r
315         InitIrqLevels();\r
316 \r
317         /* Initialise the ports used by the LEDs. */\r
318         vParTestInitialise();\r
319 \r
320         /* If we are going to use the watchdog, then initialise it now. */\r
321         #if WATCHDOG != WTC_NONE\r
322                 InitWatchdog();\r
323         #endif\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 /* Idle hook function. */\r
328 #if configUSE_IDLE_HOOK == 1\r
329         void vApplicationIdleHook( void )\r
330         {\r
331                 /* Are we using the idle task to kick the watchdog?  See watchdog.h\r
332                 for watchdog kicking options. Note this is for demonstration only\r
333                 and is not a suggested method of servicing the watchdog in a real\r
334                 application. */\r
335                 #if WATCHDOG == WTC_IN_IDLE\r
336                         Kick_Watchdog();\r
337                 #endif\r
338 \r
339                 vCoRoutineSchedule();\r
340         }\r
341 #else\r
342         #if WATCHDOG == WTC_IN_IDLE\r
343                 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.\r
344         #endif\r
345 #endif\r
346 \r
347 /*-----------------------------------------------------------*/\r
348 \r
349 /* Tick hook function. */\r
350 #if configUSE_TICK_HOOK == 1\r
351         void vApplicationTickHook( void )\r
352         {\r
353                 /* Are we using the tick to kick the watchdog?  See watchdog.h\r
354                 for watchdog kicking options.  Note this is for demonstration\r
355                 only and is not a suggested method of servicing the watchdog in\r
356                 a real application. */\r
357                 #if WATCHDOG == WTC_IN_TICK\r
358                         Kick_Watchdog();\r
359                 #endif\r
360         }\r
361 #else\r
362         #if WATCHDOG == WTC_IN_TICK\r
363                 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.\r
364         #endif\r
365 #endif\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 static void vFirstRegisterTestTask( void *pvParameters )\r
369 {\r
370 extern volatile unsigned long ulCriticalNesting;\r
371 \r
372         /* Fills the registers with known values (different to the values\r
373         used in vSecondRegisterTestTask()), then checks that the registers still\r
374         all contain the expected value.  This is done to test the context save\r
375         and restore mechanism as this task is swapped onto and off of the CPU. */\r
376 \r
377         for( ;; )\r
378         {\r
379                 #pragma asm\r
380                         ;Load known values into each register.\r
381                         LDI     #0x11111111, R0\r
382                         LDI     #0x22222222, R1\r
383                         LDI     #0x33333333, R2\r
384                         LDI #0x44444444, R3\r
385                         LDI     #0x55555555, R4\r
386                         LDI     #0x66666666, R5\r
387                         LDI     #0x77777777, R6\r
388                         LDI     #0x88888888, R7\r
389                         LDI     #0x99999999, R8\r
390                         LDI     #0xaaaaaaaa, R9\r
391                         LDI     #0xbbbbbbbb, R10\r
392                         LDI     #0xcccccccc, R11\r
393                         LDI     #0xdddddddd, R12\r
394 \r
395                         ;Check each register still contains the expected value.\r
396                         LDI #0x11111111, R13\r
397                         CMP R13, R0\r
398                         BNE First_Set_Error\r
399 \r
400                         LDI #0x22222222, R13\r
401                         CMP R13, R1\r
402                         BNE First_Set_Error\r
403 \r
404                         LDI #0x33333333, R13\r
405                         CMP R13, R2\r
406                         BNE First_Set_Error\r
407 \r
408                         LDI #0x44444444, R13\r
409                         CMP R13, R3\r
410                         BNE First_Set_Error\r
411 \r
412                         LDI #0x55555555, R13\r
413                         CMP R13, R4\r
414                         BNE First_Set_Error\r
415 \r
416                         LDI #0x66666666, R13\r
417                         CMP R13, R5\r
418                         BNE First_Set_Error\r
419 \r
420                         LDI #0x77777777, R13\r
421                         CMP R13, R6\r
422                         BNE First_Set_Error\r
423 \r
424                         LDI #0x88888888, R13\r
425                         CMP R13, R7\r
426                         BNE First_Set_Error\r
427 \r
428                         LDI #0x99999999, R13\r
429                         CMP R13, R8\r
430                         BNE First_Set_Error\r
431 \r
432                         LDI #0xaaaaaaaa, R13\r
433                         CMP R13, R9\r
434                         BNE First_Set_Error\r
435 \r
436                         LDI #0xbbbbbbbb, R13\r
437                         CMP R13, R10\r
438                         BNE First_Set_Error\r
439 \r
440                         LDI #0xcccccccc, R13\r
441                         CMP R13, R11\r
442                         BNE First_Set_Error\r
443 \r
444                         LDI #0xdddddddd, R13\r
445                         CMP R13, R12\r
446                         BNE First_Set_Error\r
447 \r
448                         BRA First_Start_Next_Loop\r
449 \r
450                 First_Set_Error:\r
451 \r
452                         ; Latch that an error has occurred.\r
453                         LDI #_ulRegTestError, R0\r
454                         LDI #0x00000001, R1\r
455                         ST R1, @R0\r
456 \r
457 \r
458                 First_Start_Next_Loop:\r
459 \r
460 \r
461                 #pragma endasm\r
462         }\r
463 }\r
464 /*-----------------------------------------------------------*/\r
465 \r
466 static void vSecondRegisterTestTask( void *pvParameters )\r
467 {\r
468 extern volatile unsigned long ulCriticalNesting;\r
469 \r
470         /* Fills the registers with known values (different to the values\r
471         used in vFirstRegisterTestTask()), then checks that the registers still\r
472         all contain the expected value.  This is done to test the context save\r
473         and restore mechanism as this task is swapped onto and off of the CPU. */\r
474 \r
475         for( ;; )\r
476         {\r
477                 #pragma asm\r
478                         ;Load known values into each register.\r
479                         LDI     #0x11111111, R1\r
480                         LDI     #0x22222222, R2\r
481                         INT #40H\r
482                         LDI     #0x33333333, R3\r
483                         LDI #0x44444444, R4\r
484                         LDI     #0x55555555, R5\r
485                         LDI     #0x66666666, R6\r
486                         LDI     #0x77777777, R7\r
487                         LDI     #0x88888888, R8\r
488                         LDI     #0x99999999, R9\r
489                         INT #40H\r
490                         LDI     #0xaaaaaaaa, R10\r
491                         LDI     #0xbbbbbbbb, R11\r
492                         LDI     #0xcccccccc, R12\r
493                         LDI     #0xdddddddd, R0\r
494 \r
495                         ;Check each register still contains the expected value.\r
496                         LDI #0x11111111, R13\r
497                         CMP R13, R1\r
498                         BNE Second_Set_Error\r
499 \r
500                         LDI #0x22222222, R13\r
501                         CMP R13, R2\r
502                         BNE Second_Set_Error\r
503 \r
504                         LDI #0x33333333, R13\r
505                         CMP R13, R3\r
506                         BNE Second_Set_Error\r
507 \r
508                         LDI #0x44444444, R13\r
509                         CMP R13, R4\r
510                         BNE Second_Set_Error\r
511 \r
512                         LDI #0x55555555, R13\r
513                         CMP R13, R5\r
514                         BNE Second_Set_Error\r
515 \r
516                         INT #40H\r
517 \r
518                         LDI #0x66666666, R13\r
519                         CMP R13, R6\r
520                         BNE Second_Set_Error\r
521 \r
522                         LDI #0x77777777, R13\r
523                         CMP R13, R7\r
524                         BNE Second_Set_Error\r
525 \r
526                         LDI #0x88888888, R13\r
527                         CMP R13, R8\r
528                         BNE Second_Set_Error\r
529 \r
530                         LDI #0x99999999, R13\r
531                         CMP R13, R9\r
532                         BNE Second_Set_Error\r
533 \r
534                         INT #40H\r
535 \r
536                         LDI #0xaaaaaaaa, R13\r
537                         CMP R13, R10\r
538                         BNE Second_Set_Error\r
539 \r
540                         LDI #0xbbbbbbbb, R13\r
541                         CMP R13, R11\r
542                         BNE Second_Set_Error\r
543 \r
544                         LDI #0xcccccccc, R13\r
545                         CMP R13, R12\r
546                         BNE Second_Set_Error\r
547 \r
548                         LDI #0xdddddddd, R13\r
549                         CMP R13, R0\r
550                         BNE Second_Set_Error\r
551 \r
552                         BRA Second_Start_Next_Loop\r
553 \r
554                 Second_Set_Error:\r
555 \r
556                         ; Latch that an error has occurred.\r
557                         LDI #_ulRegTestError, R0\r
558                         LDI #0x00000001, R1\r
559                         ST R1, @R0\r
560 \r
561 \r
562                 Second_Start_Next_Loop:\r
563 \r
564 \r
565                 #pragma endasm\r
566         }\r
567 }\r
568 /*-----------------------------------------------------------*/\r
569 \r
570 \r