]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_IAR/main_full.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / RX100-RSK_IAR / main_full.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  * This project includes a lot of tasks and tests and is therefore complex.\r
31  * If you would prefer a much simpler project to get started with then select\r
32  * the 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in\r
33  * FreeRTOSConfig.h.  When configCREATE_LOW_POWER_DEMO is set to 1 main() will\r
34  * call main_low_power() instead of main_full().\r
35  * ****************************************************************************\r
36  *\r
37  * Creates all the demo application tasks, then starts the scheduler.  The web\r
38  * documentation provides more details of the standard demo application tasks,\r
39  * which provide no particular functionality but do provide a good example of\r
40  * how to use the FreeRTOS API.\r
41  *\r
42  * In addition to the standard demo tasks, the following tasks and tests are\r
43  * defined and/or created within this file:\r
44  *\r
45  * "Reg test" tasks - These fill the registers with known values, then\r
46  * repeatedly check that each register still contains its expected value for\r
47  * the lifetime of the tasks.  Each task uses different values.  The tasks run\r
48  * with very low priority so get preempted very frequently.  A check variable\r
49  * is incremented on each iteration of the test loop.  A register containing an\r
50  * unexpected value is indicative of an error in the context switching\r
51  * mechanism and will result in a branch to a null loop - which in turn will\r
52  * prevent the check variable from incrementing any further and allow the check\r
53  * timer (described below) to determine that an error has occurred.  The nature\r
54  * of the reg test tasks necessitates that they are written in assembly code.\r
55  *\r
56  * "Check Timer" and Callback Function - The check timer period is initially\r
57  * set to three seconds.  The check timer callback function checks that all the\r
58  * standard demo tasks are not only still executing, but are executing without\r
59  * reporting any errors.  If the check timer discovers that a task has either\r
60  * stalled, or reported an error, then it changes its own period from the\r
61  * initial three seconds, to just 200ms.  The check timer callback function\r
62  * also toggles LED 0 each time it is called.  This provides a visual\r
63  * indication of the system status:  If the LED toggles every three seconds,\r
64  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
65  * an issue has been discovered with at least one task.\r
66  *\r
67  * *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.\r
68  * The PowerON_Reset_PC() supplied in resetprg.c with this demo has\r
69  * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.\r
70 */\r
71 \r
72 /* Standard includes. */\r
73 #include <string.h>\r
74 \r
75 /* Hardware specific includes. */\r
76 #include "iorx111.h"\r
77 \r
78 /* Kernel includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "timers.h"\r
82 #include "semphr.h"\r
83 \r
84 /* Standard demo includes. */\r
85 #include "partest.h"\r
86 #include "death.h"\r
87 #include "blocktim.h"\r
88 #include "GenQTest.h"\r
89 #include "recmutex.h"\r
90 \r
91 \r
92 /* Variables that are incremented on each iteration of the reg test tasks are\r
93 declared outside of the #if configCREATE_LOW_POWER_DEMO conditional compilation\r
94 to prevent linker issues when configCREATE_LOW_POWER_DEMO is set to 1.  The\r
95 check timer inspects these variables to ensure they are still incrementing as\r
96 expected.  If a variable stops incrementing then it is likely that its associate\r
97 task has stalled. */\r
98 unsigned long volatile ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
99 \r
100 \r
101 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set\r
102 to 0, otherwise the code in main_low_power.c is used. */\r
103 #if configCREATE_LOW_POWER_DEMO == 0\r
104 \r
105 \r
106 /* Values that are passed into the reg test tasks using the task parameter.\r
107 The tasks check that the values are passed in correctly. */\r
108 #define mainREG_TEST_1_PARAMETER        ( 0x12121212UL )\r
109 #define mainREG_TEST_2_PARAMETER        ( 0x12345678UL )\r
110 \r
111 /* Priorities at which the standard demo tasks are created. */\r
112 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
113 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
114 \r
115 /* The LED toggled by the check timer. */\r
116 #define mainCHECK_LED                           ( 0 )\r
117 \r
118 /* The period at which the check timer will expire, in ms, provided no errors\r
119 have been reported by any of the standard demo tasks.  ms are converted to the\r
120 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
121 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
122 \r
123 /* The period at which the check timer will expire, in ms, if an error has been\r
124 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
125 in ticks using the portTICK_PERIOD_MS constant. */\r
126 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
127 \r
128 /* A block time of zero simple means "Don't Block". */\r
129 #define mainDONT_BLOCK                          ( 0UL )\r
130 \r
131 /*\r
132  * The reg test tasks as described at the top of this file.\r
133  */\r
134 static void prvRegTest1Task( void *pvParameters );\r
135 static void prvRegTest2Task( void *pvParameters );\r
136 \r
137 /*\r
138  * The actual implementation of the reg test functionality, which, because of\r
139  * the direct register access, have to be in assembly.\r
140  */\r
141 void vRegTest1Implementation( void );\r
142 void vRegTest2Implementation( void );\r
143 \r
144 /*\r
145  * The check timer callback function, as described at the top of this file.\r
146  */\r
147 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
148 \r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
153 function. */\r
154 static TimerHandle_t xCheckTimer = NULL;\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 void main_full( void )\r
159 {\r
160         /* Start the reg test tasks which test the context switching mechanism. */\r
161         xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
162         xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
163 \r
164         /* Create the standard demo tasks. */\r
165         vCreateBlockTimeTasks();\r
166         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
167         vStartRecursiveMutexTasks();\r
168 \r
169         /* The suicide tasks must be created last as they need to know how many\r
170         tasks were running prior to their creation in order to ascertain whether\r
171         or not the correct/expected number of tasks are running at any given time. */\r
172         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
173 \r
174         /* Create the software timer that performs the 'check' functionality,\r
175         as described at the top of this file. */\r
176         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
177                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 5000ms (5s). */\r
178                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
179                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
180                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
181                                                           );\r
182 \r
183         configASSERT( xCheckTimer );\r
184 \r
185         /* Start the check timer.  It will actually start when the scheduler is\r
186         started. */\r
187         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
188 \r
189         /* Start the tasks running. */\r
190         vTaskStartScheduler();\r
191 \r
192         /* If all is well execution will never reach here as the scheduler will be\r
193         running.  If this null loop is reached then it is likely there was\r
194         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
195         be created.  See http://www.freertos.org/a00111.html. */\r
196         for( ;; );\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
201 {\r
202 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;\r
203 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
204 \r
205         /* Remove compiler warnings about unused parameters. */\r
206         ( void ) xTimer;\r
207 \r
208         /* Check the standard demo tasks are running without error. */\r
209         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
210         {\r
211                 lErrorStatus = pdFAIL;\r
212         }\r
213         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
214         {\r
215                 lErrorStatus = pdFAIL;\r
216         }\r
217         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
218         {\r
219                 lErrorStatus = pdFAIL;\r
220         }\r
221         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
222         {\r
223                 lErrorStatus = pdFAIL;\r
224         }\r
225 \r
226         /* Check the reg test tasks are still cycling.  They will stop incrementing\r
227         their loop counters if they encounter an error. */\r
228         if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
229         {\r
230                 lErrorStatus = pdFAIL;\r
231         }\r
232 \r
233         if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
234         {\r
235                 lErrorStatus = pdFAIL;\r
236         }\r
237 \r
238         /* Remember the loop counter values this time around so they can be checked\r
239         again the next time this callback function executes. */\r
240         ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
241         ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
242 \r
243         /* Toggle the check LED to give an indication of the system status.  If\r
244         the LED toggles every three seconds then everything is ok.  A faster toggle\r
245         indicates an error. */\r
246         vParTestToggleLED( mainCHECK_LED );\r
247 \r
248         /* Was an error detected this time through the callback execution? */\r
249         if( lErrorStatus != pdPASS )\r
250         {\r
251                 if( lChangedTimerPeriodAlready == pdFALSE )\r
252                 {\r
253                         lChangedTimerPeriodAlready = pdTRUE;\r
254 \r
255                         /* This call to xTimerChangePeriod() uses a zero block time.\r
256                         Functions called from inside of a timer callback function must\r
257                         *never* attempt to block. */\r
258                         xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
259                 }\r
260         }\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 /* This function is explained in the comments at the top of this file. */\r
265 static void prvRegTest1Task( void *pvParameters )\r
266 {\r
267         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )\r
268         {\r
269                 /* The parameter did not contain the expected value. */\r
270                 for( ;; )\r
271                 {\r
272                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
273                         taskDISABLE_INTERRUPTS();\r
274                 }\r
275         }\r
276 \r
277         /* This is an inline asm function that never returns. */\r
278         vRegTest1Implementation();\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 /* This function is explained in the comments at the top of this file. */\r
283 static void prvRegTest2Task( void *pvParameters )\r
284 {\r
285         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )\r
286         {\r
287                 /* The parameter did not contain the expected value. */\r
288                 for( ;; )\r
289                 {\r
290                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
291                         taskDISABLE_INTERRUPTS();\r
292                 }\r
293         }\r
294 \r
295         /* This is an inline asm function that never returns. */\r
296         vRegTest2Implementation();\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 #endif /* configCREATE_LOW_POWER_DEMO */\r