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