]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_Renesas_e2studio/RTOSDemo/main_full.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / RX100-RSK_Renesas_e2studio / RTOSDemo / main_full.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /* ****************************************************************************\r
71  * This project includes a lot of tasks and tests and is therefore complex.\r
72  * If you would prefer a much simpler project to get started with then select\r
73  * the 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in\r
74  * FreeRTOSConfig.h.  When configCREATE_LOW_POWER_DEMO is set to 1 main() will\r
75  * call main_low_power() instead of main_full().\r
76  * ****************************************************************************\r
77  *\r
78  * Creates all the demo application tasks, then starts the scheduler.  The web\r
79  * documentation provides more details of the standard demo application tasks,\r
80  * which provide no particular functionality but do provide a good example of\r
81  * how to use the FreeRTOS API.\r
82  *\r
83  * In addition to the standard demo tasks, the following tasks and tests are\r
84  * defined and/or created within this file:\r
85  *\r
86  * "Reg test" tasks - These fill the registers with known values, then\r
87  * repeatedly check that each register still contains its expected value for\r
88  * the lifetime of the tasks.  Each task uses different values.  The tasks run\r
89  * with very low priority so get preempted very frequently.  A check variable\r
90  * is incremented on each iteration of the test loop.  A register containing an\r
91  * unexpected value is indicative of an error in the context switching\r
92  * mechanism and will result in a branch to a null loop - which in turn will\r
93  * prevent the check variable from incrementing any further and allow the check\r
94  * timer (described below) to determine that an error has occurred.  The nature\r
95  * of the reg test tasks necessitates that they are written in assembly code.\r
96  *\r
97  * "Check Timer" and Callback Function - The check timer period is initially\r
98  * set to three seconds.  The check timer callback function checks that all the\r
99  * standard demo tasks are not only still executing, but are executing without\r
100  * reporting any errors.  If the check timer discovers that a task has either\r
101  * stalled, or reported an error, then it changes its own period from the\r
102  * initial three seconds, to just 200ms.  The check timer callback function\r
103  * also toggles LED 0 each time it is called.  This provides a visual\r
104  * indication of the system status:  If the LED toggles every three seconds,\r
105  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
106  * an issue has been discovered with at least one task.\r
107  *\r
108  * *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.\r
109  * The PowerON_Reset_PC() supplied in resetprg.c with this demo has\r
110  * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.\r
111 */\r
112 \r
113 /* Standard includes. */\r
114 #include <string.h>\r
115 \r
116 /* Hardware specific includes. */\r
117 #include "iodefine.h"\r
118 \r
119 /* Kernel includes. */\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 #include "timers.h"\r
123 #include "semphr.h"\r
124 \r
125 /* Standard demo includes. */\r
126 #include "partest.h"\r
127 #include "death.h"\r
128 #include "blocktim.h"\r
129 #include "GenQTest.h"\r
130 #include "recmutex.h"\r
131 \r
132 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set\r
133 to 0, otherwise the code in main_low_power.c is used. */\r
134 #if configCREATE_LOW_POWER_DEMO == 0\r
135 \r
136 \r
137 /* Values that are passed into the reg test tasks using the task parameter.\r
138 The tasks check that the values are passed in correctly. */\r
139 #define mainREG_TEST_1_PARAMETER        ( 0x12121212UL )\r
140 #define mainREG_TEST_2_PARAMETER        ( 0x12345678UL )\r
141 \r
142 /* Priorities at which the standard demo tasks are created. */\r
143 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
144 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
145 \r
146 /* The LED toggled by the check timer. */\r
147 #define mainCHECK_LED                           ( 0 )\r
148 \r
149 /* The period at which the check timer will expire, in ms, provided no errors\r
150 have been reported by any of the standard demo tasks.  ms are converted to the\r
151 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
152 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
153 \r
154 /* The period at which the check timer will expire, in ms, if an error has been\r
155 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
156 in ticks using the portTICK_PERIOD_MS constant. */\r
157 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
158 \r
159 /* A block time of zero simple means "Don't Block". */\r
160 #define mainDONT_BLOCK                          ( 0UL )\r
161 \r
162 /*\r
163  * The reg test tasks as described at the top of this file.\r
164  */\r
165 static void prvRegTest1Task( void *pvParameters );\r
166 static void prvRegTest2Task( void *pvParameters );\r
167 \r
168 /*\r
169  * The actual implementation of the reg test functionality, which, because of\r
170  * the direct register access, have to be in assembly.\r
171  */\r
172 static void prvRegTest1Implementation( void );\r
173 static void prvRegTest2Implementation( void );\r
174 \r
175 /*\r
176  * The check timer callback function, as described at the top of this file.\r
177  */\r
178 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
179 \r
180 \r
181 /*-----------------------------------------------------------*/\r
182 \r
183 /* Variables that are incremented on each iteration of the reg test tasks -\r
184 provided the tasks have not reported any errors.  The check timer inspects these\r
185 variables to ensure they are still incrementing as expected.  If a variable\r
186 stops incrementing then it is likely that its associated task has stalled. */\r
187 unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
188 \r
189 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
190 function. */\r
191 static TimerHandle_t xCheckTimer = NULL;\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void main_full( void )\r
196 {\r
197         /* Start the reg test tasks which test the context switching mechanism. */\r
198         xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
199         xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
200 \r
201         /* Create the standard demo tasks. */\r
202         vCreateBlockTimeTasks();\r
203         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
204         vStartRecursiveMutexTasks();\r
205 \r
206         /* The suicide tasks must be created last as they need to know how many\r
207         tasks were running prior to their creation in order to ascertain whether\r
208         or not the correct/expected number of tasks are running at any given time. */\r
209         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
210 \r
211         /* Create the software timer that performs the 'check' functionality,\r
212         as described at the top of this file. */\r
213         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
214                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 5000ms (5s). */\r
215                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
216                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
217                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
218                                                           );\r
219 \r
220         configASSERT( xCheckTimer );\r
221 \r
222         /* Start the check timer.  It will actually start when the scheduler is\r
223         started. */\r
224         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
225 \r
226         /* Start the tasks running. */\r
227         vTaskStartScheduler();\r
228 \r
229         /* If all is well execution will never reach here as the scheduler will be\r
230         running.  If this null loop is reached then it is likely there was\r
231         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
232         be created.  See http://www.freertos.org/a00111.html. */\r
233         for( ;; );\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
238 {\r
239 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;\r
240 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
241 \r
242         /* Remove compiler warnings about unused parameters. */\r
243         ( void ) xTimer;\r
244 \r
245         /* Check the standard demo tasks are running without error. */\r
246         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
247         {\r
248                 lErrorStatus = pdFAIL;\r
249         }\r
250         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
251         {\r
252                 lErrorStatus = pdFAIL;\r
253         }\r
254         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
255         {\r
256                 lErrorStatus = pdFAIL;\r
257         }\r
258         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
259         {\r
260                 lErrorStatus = pdFAIL;\r
261         }\r
262 \r
263         /* Check the reg test tasks are still cycling.  They will stop incrementing\r
264         their loop counters if they encounter an error. */\r
265         if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
266         {\r
267                 lErrorStatus = pdFAIL;\r
268         }\r
269 \r
270         if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
271         {\r
272                 lErrorStatus = pdFAIL;\r
273         }\r
274 \r
275         /* Remember the loop counter values this time around so they can be checked\r
276         again the next time this callback function executes. */\r
277         ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
278         ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
279 \r
280         /* Toggle the check LED to give an indication of the system status.  If\r
281         the LED toggles every three seconds then everything is ok.  A faster toggle\r
282         indicates an error. */\r
283         vParTestToggleLED( mainCHECK_LED );\r
284 \r
285         /* Was an error detected this time through the callback execution? */\r
286         if( lErrorStatus != pdPASS )\r
287         {\r
288                 if( lChangedTimerPeriodAlready == pdFALSE )\r
289                 {\r
290                         lChangedTimerPeriodAlready = pdTRUE;\r
291 \r
292                         /* This call to xTimerChangePeriod() uses a zero block time.\r
293                         Functions called from inside of a timer callback function must\r
294                         *never* attempt to block. */\r
295                         xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
296                 }\r
297         }\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 /* This function is explained in the comments at the top of this file. */\r
302 static void prvRegTest1Task( void *pvParameters )\r
303 {\r
304         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )\r
305         {\r
306                 /* The parameter did not contain the expected value. */\r
307                 for( ;; )\r
308                 {\r
309                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
310                         taskDISABLE_INTERRUPTS();\r
311                 }\r
312         }\r
313 \r
314         /* This is an inline asm function that never returns. */\r
315         prvRegTest1Implementation();\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 /* This function is explained in the comments at the top of this file. */\r
320 static void prvRegTest2Task( void *pvParameters )\r
321 {\r
322         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )\r
323         {\r
324                 /* The parameter did not contain the expected value. */\r
325                 for( ;; )\r
326                 {\r
327                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
328                         taskDISABLE_INTERRUPTS();\r
329                 }\r
330         }\r
331 \r
332         /* This is an inline asm function that never returns. */\r
333         prvRegTest2Implementation();\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /* This function is explained in the comments at the top of this file. */\r
338 #pragma inline_asm prvRegTest1Implementation\r
339 static void prvRegTest1Implementation( void )\r
340 {\r
341         ; Put a known value in each register.\r
342         MOV.L   #11111111H, R15\r
343         MVTACHI R15\r
344         MOV.L   #22222222H, R15\r
345         MVTACLO R15\r
346         MOV.L   #1, R1\r
347         MOV.L   #2, R2\r
348         MOV.L   #3, R3\r
349         MOV.L   #4, R4\r
350         MOV.L   #5, R5\r
351         MOV.L   #6, R6\r
352         MOV.L   #7, R7\r
353         MOV.L   #8, R8\r
354         MOV.L   #9, R9\r
355         MOV.L   #10, R10\r
356         MOV.L   #11, R11\r
357         MOV.L   #12, R12\r
358         MOV.L   #13, R13\r
359         MOV.L   #14, R14\r
360         MOV.L   #15, R15\r
361 \r
362         ; Loop, checking each iteration that each register still contains the\r
363         ; expected value.\r
364 TestLoop1:\r
365 \r
366         ; Push the registers that are going to get clobbered.\r
367         PUSHM   R14-R15\r
368 \r
369         ; Increment the loop counter to show this task is still getting CPU time.\r
370         MOV.L   #_ulRegTest1CycleCount, R14\r
371         MOV.L   [ R14 ], R15\r
372         ADD             #1, R15\r
373         MOV.L   R15, [ R14 ]\r
374 \r
375         ; Yield to extend the text coverage.  Set the bit in the ITU SWINTR register.\r
376         MOV.L   #1, R14\r
377         MOV.L   #0872E0H, R15\r
378         MOV.B   R14, [R15]\r
379         NOP\r
380         NOP\r
381 \r
382         ;Check the accumulator value.\r
383         MVFACHI R15\r
384         CMP             #11111111H, R15\r
385         BNE             RegTest2Error\r
386         MVFACMI R15\r
387         CMP             #11112222H, R15\r
388         BNE             RegTest2Error\r
389 \r
390         ; Restore the clobbered registers.\r
391         POPM    R14-R15\r
392 \r
393         ; Now compare each register to ensure it still contains the value that was\r
394         ; set before this loop was entered.\r
395         CMP             #1, R1\r
396         BNE             RegTest1Error\r
397         CMP             #2, R2\r
398         BNE             RegTest1Error\r
399         CMP             #3, R3\r
400         BNE             RegTest1Error\r
401         CMP             #4, R4\r
402         BNE             RegTest1Error\r
403         CMP             #5, R5\r
404         BNE             RegTest1Error\r
405         CMP             #6, R6\r
406         BNE             RegTest1Error\r
407         CMP             #7, R7\r
408         BNE             RegTest1Error\r
409         CMP             #8, R8\r
410         BNE             RegTest1Error\r
411         CMP             #9, R9\r
412         BNE             RegTest1Error\r
413         CMP             #10, R10\r
414         BNE             RegTest1Error\r
415         CMP             #11, R11\r
416         BNE             RegTest1Error\r
417         CMP             #12, R12\r
418         BNE             RegTest1Error\r
419         CMP             #13, R13\r
420         BNE             RegTest1Error\r
421         CMP             #14, R14\r
422         BNE             RegTest1Error\r
423         CMP             #15, R15\r
424         BNE             RegTest1Error\r
425 \r
426         ; All comparisons passed, start a new iteration of this loop.\r
427         BRA             TestLoop1\r
428 \r
429 RegTest1Error:\r
430         ; A compare failed, just loop here so the loop counter stops incrementing\r
431         ; causing the check timer to indicate the error.\r
432         BRA RegTest1Error\r
433 }\r
434 /*-----------------------------------------------------------*/\r
435 \r
436 /* This function is explained in the comments at the top of this file. */\r
437 #pragma inline_asm prvRegTest2Implementation\r
438 static void prvRegTest2Implementation( void )\r
439 {\r
440         ; Put a known value in each register.\r
441         MOV.L   #33333333H, R15\r
442         MVTACHI R15\r
443         MOV.L   #44444444H, R15\r
444         MVTACLO R15\r
445         MOV.L   #10, R1\r
446         MOV.L   #20, R2\r
447         MOV.L   #30, R3\r
448         MOV.L   #40, R4\r
449         MOV.L   #50, R5\r
450         MOV.L   #60, R6\r
451         MOV.L   #70, R7\r
452         MOV.L   #80, R8\r
453         MOV.L   #90, R9\r
454         MOV.L   #100, R10\r
455         MOV.L   #110, R11\r
456         MOV.L   #120, R12\r
457         MOV.L   #130, R13\r
458         MOV.L   #140, R14\r
459         MOV.L   #150, R15\r
460 \r
461         ; Loop, checking on each iteration that each register still contains the\r
462         ; expected value.\r
463 TestLoop2:\r
464 \r
465         ; Push the registers that are going to get clobbered.\r
466         PUSHM   R14-R15\r
467 \r
468         ; Increment the loop counter to show this task is still getting CPU time.\r
469         MOV.L   #_ulRegTest2CycleCount, R14\r
470         MOV.L   [ R14 ], R15\r
471         ADD             #1, R15\r
472         MOV.L   R15, [ R14 ]\r
473 \r
474         ;Check the accumulator value.\r
475         MVFACHI R15\r
476         CMP             #33333333H, R15\r
477         BNE             RegTest2Error\r
478         MVFACMI R15\r
479         CMP             #33334444H, R15\r
480         BNE             RegTest2Error\r
481 \r
482         ; Restore the clobbered registers.\r
483         POPM    R14-R15\r
484 \r
485         CMP             #10, R1\r
486         BNE             RegTest2Error\r
487         CMP             #20, R2\r
488         BNE             RegTest2Error\r
489         CMP             #30, R3\r
490         BNE             RegTest2Error\r
491         CMP             #40, R4\r
492         BNE             RegTest2Error\r
493         CMP             #50, R5\r
494         BNE             RegTest2Error\r
495         CMP             #60, R6\r
496         BNE             RegTest2Error\r
497         CMP             #70, R7\r
498         BNE             RegTest2Error\r
499         CMP             #80, R8\r
500         BNE             RegTest2Error\r
501         CMP             #90, R9\r
502         BNE             RegTest2Error\r
503         CMP             #100, R10\r
504         BNE             RegTest2Error\r
505         CMP             #110, R11\r
506         BNE             RegTest2Error\r
507         CMP             #120, R12\r
508         BNE             RegTest2Error\r
509         CMP             #130, R13\r
510         BNE             RegTest2Error\r
511         CMP             #140, R14\r
512         BNE             RegTest2Error\r
513         CMP             #150, R15\r
514         BNE             RegTest2Error\r
515 \r
516         ; All comparisons passed, start a new itteratio of this loop.\r
517         BRA             TestLoop2\r
518 \r
519 RegTest2Error:\r
520         ; A compare failed, just loop here so the loop counter stops incrementing\r
521         ; - causing the check timer to indicate the error.\r
522         BRA RegTest2Error\r
523 }\r
524 /*-----------------------------------------------------------*/\r
525 \r
526 #endif /* configCREATE_LOW_POWER_DEMO */\r