]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_Renesas_e2studio/RTOSDemo/main_full.c
44d874f3fc0559efd7127c28b31902caad8f5eb6
[freertos] / FreeRTOS / Demo / RX100-RSK_Renesas_e2studio / RTOSDemo / main_full.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  * 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
35  *\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
40  *\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
43  *\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
54  *\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
65  *\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
69 */\r
70 \r
71 /* Standard includes. */\r
72 #include <string.h>\r
73 \r
74 /* Hardware specific includes. */\r
75 #include "iodefine.h"\r
76 \r
77 /* Kernel includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "timers.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Standard demo includes. */\r
84 #include "partest.h"\r
85 #include "death.h"\r
86 #include "blocktim.h"\r
87 #include "GenQTest.h"\r
88 #include "recmutex.h"\r
89 \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
93 \r
94 \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
99 \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
103 \r
104 /* The LED toggled by the check timer. */\r
105 #define mainCHECK_LED                           ( 0 )\r
106 \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
111 \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
116 \r
117 /* A block time of zero simple means "Don't Block". */\r
118 #define mainDONT_BLOCK                          ( 0UL )\r
119 \r
120 /*\r
121  * The reg test tasks as described at the top of this file.\r
122  */\r
123 static void prvRegTest1Task( void *pvParameters );\r
124 static void prvRegTest2Task( void *pvParameters );\r
125 \r
126 /*\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
129  */\r
130 static void prvRegTest1Implementation( void );\r
131 static void prvRegTest2Implementation( void );\r
132 \r
133 /*\r
134  * The check timer callback function, as described at the top of this file.\r
135  */\r
136 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
137 \r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \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
146 \r
147 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
148 function. */\r
149 static TimerHandle_t xCheckTimer = NULL;\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 void main_full( void )\r
154 {\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
158 \r
159         /* Create the standard demo tasks. */\r
160         vCreateBlockTimeTasks();\r
161         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
162         vStartRecursiveMutexTasks();\r
163 \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
168 \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
176                                                           );\r
177 \r
178         configASSERT( xCheckTimer );\r
179 \r
180         /* Start the check timer.  It will actually start when the scheduler is\r
181         started. */\r
182         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
183 \r
184         /* Start the tasks running. */\r
185         vTaskStartScheduler();\r
186 \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
191         for( ;; );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
196 {\r
197 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;\r
198 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
199 \r
200         /* Remove compiler warnings about unused parameters. */\r
201         ( void ) xTimer;\r
202 \r
203         /* Check the standard demo tasks are running without error. */\r
204         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
205         {\r
206                 lErrorStatus = pdFAIL;\r
207         }\r
208         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
209         {\r
210                 lErrorStatus = pdFAIL;\r
211         }\r
212         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
213         {\r
214                 lErrorStatus = pdFAIL;\r
215         }\r
216         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
217         {\r
218                 lErrorStatus = pdFAIL;\r
219         }\r
220 \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
224         {\r
225                 lErrorStatus = pdFAIL;\r
226         }\r
227 \r
228         if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
229         {\r
230                 lErrorStatus = pdFAIL;\r
231         }\r
232 \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
237 \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
242 \r
243         /* Was an error detected this time through the callback execution? */\r
244         if( lErrorStatus != pdPASS )\r
245         {\r
246                 if( lChangedTimerPeriodAlready == pdFALSE )\r
247                 {\r
248                         lChangedTimerPeriodAlready = pdTRUE;\r
249 \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
254                 }\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 /* This function is explained in the comments at the top of this file. */\r
260 static void prvRegTest1Task( void *pvParameters )\r
261 {\r
262         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )\r
263         {\r
264                 /* The parameter did not contain the expected value. */\r
265                 for( ;; )\r
266                 {\r
267                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
268                         taskDISABLE_INTERRUPTS();\r
269                 }\r
270         }\r
271 \r
272         /* This is an inline asm function that never returns. */\r
273         prvRegTest1Implementation();\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 /* This function is explained in the comments at the top of this file. */\r
278 static void prvRegTest2Task( void *pvParameters )\r
279 {\r
280         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )\r
281         {\r
282                 /* The parameter did not contain the expected value. */\r
283                 for( ;; )\r
284                 {\r
285                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
286                         taskDISABLE_INTERRUPTS();\r
287                 }\r
288         }\r
289 \r
290         /* This is an inline asm function that never returns. */\r
291         prvRegTest2Implementation();\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \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
298 {\r
299         ; Put a known value in each register.\r
300         MOV.L   #11111111H, R15\r
301         MVTACHI R15\r
302         MOV.L   #22222222H, R15\r
303         MVTACLO R15\r
304         MOV.L   #1, R1\r
305         MOV.L   #2, R2\r
306         MOV.L   #3, R3\r
307         MOV.L   #4, R4\r
308         MOV.L   #5, R5\r
309         MOV.L   #6, R6\r
310         MOV.L   #7, R7\r
311         MOV.L   #8, R8\r
312         MOV.L   #9, R9\r
313         MOV.L   #10, R10\r
314         MOV.L   #11, R11\r
315         MOV.L   #12, R12\r
316         MOV.L   #13, R13\r
317         MOV.L   #14, R14\r
318         MOV.L   #15, R15\r
319 \r
320         ; Loop, checking each iteration that each register still contains the\r
321         ; expected value.\r
322 TestLoop1:\r
323 \r
324         ; Push the registers that are going to get clobbered.\r
325         PUSHM   R14-R15\r
326 \r
327         ; Increment the loop counter to show this task is still getting CPU time.\r
328         MOV.L   #_ulRegTest1CycleCount, R14\r
329         MOV.L   [ R14 ], R15\r
330         ADD             #1, R15\r
331         MOV.L   R15, [ R14 ]\r
332 \r
333         ; Yield to extend the text coverage.  Set the bit in the ITU SWINTR register.\r
334         MOV.L   #1, R14\r
335         MOV.L   #0872E0H, R15\r
336         MOV.B   R14, [R15]\r
337         NOP\r
338         NOP\r
339 \r
340         ;Check the accumulator value.\r
341         MVFACHI R15\r
342         CMP             #11111111H, R15\r
343         BNE             RegTest2Error\r
344         MVFACMI R15\r
345         CMP             #11112222H, R15\r
346         BNE             RegTest2Error\r
347 \r
348         ; Restore the clobbered registers.\r
349         POPM    R14-R15\r
350 \r
351         ; Now compare each register to ensure it still contains the value that was\r
352         ; set before this loop was entered.\r
353         CMP             #1, R1\r
354         BNE             RegTest1Error\r
355         CMP             #2, R2\r
356         BNE             RegTest1Error\r
357         CMP             #3, R3\r
358         BNE             RegTest1Error\r
359         CMP             #4, R4\r
360         BNE             RegTest1Error\r
361         CMP             #5, R5\r
362         BNE             RegTest1Error\r
363         CMP             #6, R6\r
364         BNE             RegTest1Error\r
365         CMP             #7, R7\r
366         BNE             RegTest1Error\r
367         CMP             #8, R8\r
368         BNE             RegTest1Error\r
369         CMP             #9, R9\r
370         BNE             RegTest1Error\r
371         CMP             #10, R10\r
372         BNE             RegTest1Error\r
373         CMP             #11, R11\r
374         BNE             RegTest1Error\r
375         CMP             #12, R12\r
376         BNE             RegTest1Error\r
377         CMP             #13, R13\r
378         BNE             RegTest1Error\r
379         CMP             #14, R14\r
380         BNE             RegTest1Error\r
381         CMP             #15, R15\r
382         BNE             RegTest1Error\r
383 \r
384         ; All comparisons passed, start a new iteration of this loop.\r
385         BRA             TestLoop1\r
386 \r
387 RegTest1Error:\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
390         BRA RegTest1Error\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \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
397 {\r
398         ; Put a known value in each register.\r
399         MOV.L   #33333333H, R15\r
400         MVTACHI R15\r
401         MOV.L   #44444444H, R15\r
402         MVTACLO R15\r
403         MOV.L   #10, R1\r
404         MOV.L   #20, R2\r
405         MOV.L   #30, R3\r
406         MOV.L   #40, R4\r
407         MOV.L   #50, R5\r
408         MOV.L   #60, R6\r
409         MOV.L   #70, R7\r
410         MOV.L   #80, R8\r
411         MOV.L   #90, R9\r
412         MOV.L   #100, R10\r
413         MOV.L   #110, R11\r
414         MOV.L   #120, R12\r
415         MOV.L   #130, R13\r
416         MOV.L   #140, R14\r
417         MOV.L   #150, R15\r
418 \r
419         ; Loop, checking on each iteration that each register still contains the\r
420         ; expected value.\r
421 TestLoop2:\r
422 \r
423         ; Push the registers that are going to get clobbered.\r
424         PUSHM   R14-R15\r
425 \r
426         ; Increment the loop counter to show this task is still getting CPU time.\r
427         MOV.L   #_ulRegTest2CycleCount, R14\r
428         MOV.L   [ R14 ], R15\r
429         ADD             #1, R15\r
430         MOV.L   R15, [ R14 ]\r
431 \r
432         ;Check the accumulator value.\r
433         MVFACHI R15\r
434         CMP             #33333333H, R15\r
435         BNE             RegTest2Error\r
436         MVFACMI R15\r
437         CMP             #33334444H, R15\r
438         BNE             RegTest2Error\r
439 \r
440         ; Restore the clobbered registers.\r
441         POPM    R14-R15\r
442 \r
443         CMP             #10, R1\r
444         BNE             RegTest2Error\r
445         CMP             #20, R2\r
446         BNE             RegTest2Error\r
447         CMP             #30, R3\r
448         BNE             RegTest2Error\r
449         CMP             #40, R4\r
450         BNE             RegTest2Error\r
451         CMP             #50, R5\r
452         BNE             RegTest2Error\r
453         CMP             #60, R6\r
454         BNE             RegTest2Error\r
455         CMP             #70, R7\r
456         BNE             RegTest2Error\r
457         CMP             #80, R8\r
458         BNE             RegTest2Error\r
459         CMP             #90, R9\r
460         BNE             RegTest2Error\r
461         CMP             #100, R10\r
462         BNE             RegTest2Error\r
463         CMP             #110, R11\r
464         BNE             RegTest2Error\r
465         CMP             #120, R12\r
466         BNE             RegTest2Error\r
467         CMP             #130, R13\r
468         BNE             RegTest2Error\r
469         CMP             #140, R14\r
470         BNE             RegTest2Error\r
471         CMP             #150, R15\r
472         BNE             RegTest2Error\r
473 \r
474         ; All comparisons passed, start a new itteratio of this loop.\r
475         BRA             TestLoop2\r
476 \r
477 RegTest2Error:\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
480         BRA RegTest2Error\r
481 }\r
482 /*-----------------------------------------------------------*/\r
483 \r
484 #endif /* configCREATE_LOW_POWER_DEMO */\r