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