]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100-RSK_IAR/main_full.c
2234b52c7e0dbcb5ae68fedb07684637a7b81a7b
[freertos] / FreeRTOS / Demo / RX100-RSK_IAR / main_full.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 "iorx111.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 \r
91 /* Variables that are incremented on each iteration of the reg test tasks are\r
92 declared outside of the #if configCREATE_LOW_POWER_DEMO conditional compilation\r
93 to prevent linker issues when configCREATE_LOW_POWER_DEMO is set to 1.  The\r
94 check timer inspects these variables to ensure they are still incrementing as\r
95 expected.  If a variable stops incrementing then it is likely that its associate\r
96 task has stalled. */\r
97 unsigned long volatile ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;\r
98 \r
99 \r
100 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set\r
101 to 0, otherwise the code in main_low_power.c is used. */\r
102 #if configCREATE_LOW_POWER_DEMO == 0\r
103 \r
104 \r
105 /* Values that are passed into the reg test tasks using the task parameter.\r
106 The tasks check that the values are passed in correctly. */\r
107 #define mainREG_TEST_1_PARAMETER        ( 0x12121212UL )\r
108 #define mainREG_TEST_2_PARAMETER        ( 0x12345678UL )\r
109 \r
110 /* Priorities at which the standard demo tasks are created. */\r
111 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )\r
112 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )\r
113 \r
114 /* The LED toggled by the check timer. */\r
115 #define mainCHECK_LED                           ( 0 )\r
116 \r
117 /* The period at which the check timer will expire, in ms, provided no errors\r
118 have been reported by any of the standard demo tasks.  ms are converted to the\r
119 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
120 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
121 \r
122 /* The period at which the check timer will expire, in ms, if an error has been\r
123 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
124 in ticks using the portTICK_PERIOD_MS constant. */\r
125 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
126 \r
127 /* A block time of zero simple means "Don't Block". */\r
128 #define mainDONT_BLOCK                          ( 0UL )\r
129 \r
130 /*\r
131  * The reg test tasks as described at the top of this file.\r
132  */\r
133 static void prvRegTest1Task( void *pvParameters );\r
134 static void prvRegTest2Task( void *pvParameters );\r
135 \r
136 /*\r
137  * The actual implementation of the reg test functionality, which, because of\r
138  * the direct register access, have to be in assembly.\r
139  */\r
140 void vRegTest1Implementation( void );\r
141 void vRegTest2Implementation( void );\r
142 \r
143 /*\r
144  * The check timer callback function, as described at the top of this file.\r
145  */\r
146 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
147 \r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /* The check timer.  This uses prvCheckTimerCallback() as its callback\r
152 function. */\r
153 static TimerHandle_t xCheckTimer = NULL;\r
154 \r
155 /*-----------------------------------------------------------*/\r
156 \r
157 void main_full( void )\r
158 {\r
159         /* Start the reg test tasks which test the context switching mechanism. */\r
160         xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );\r
161         xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );\r
162 \r
163         /* Create the standard demo tasks. */\r
164         vCreateBlockTimeTasks();\r
165         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
166         vStartRecursiveMutexTasks();\r
167 \r
168         /* The suicide tasks must be created last as they need to know how many\r
169         tasks were running prior to their creation in order to ascertain whether\r
170         or not the correct/expected number of tasks are running at any given time. */\r
171         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
172 \r
173         /* Create the software timer that performs the 'check' functionality,\r
174         as described at the top of this file. */\r
175         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */\r
176                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 5000ms (5s). */\r
177                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
178                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
179                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
180                                                           );\r
181 \r
182         configASSERT( xCheckTimer );\r
183 \r
184         /* Start the check timer.  It will actually start when the scheduler is\r
185         started. */\r
186         xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
187 \r
188         /* Start the tasks running. */\r
189         vTaskStartScheduler();\r
190 \r
191         /* If all is well execution will never reach here as the scheduler will be\r
192         running.  If this null loop is reached then it is likely there was\r
193         insufficient FreeRTOS heap available for the idle task and/or timer task to\r
194         be created.  See http://www.freertos.org/a00111.html. */\r
195         for( ;; );\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
200 {\r
201 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;\r
202 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;\r
203 \r
204         /* Remove compiler warnings about unused parameters. */\r
205         ( void ) xTimer;\r
206 \r
207         /* Check the standard demo tasks are running without error. */\r
208         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
209         {\r
210                 lErrorStatus = pdFAIL;\r
211         }\r
212         else if( xIsCreateTaskStillRunning() != pdTRUE )\r
213         {\r
214                 lErrorStatus = pdFAIL;\r
215         }\r
216         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
217         {\r
218                 lErrorStatus = pdFAIL;\r
219         }\r
220         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
221         {\r
222                 lErrorStatus = pdFAIL;\r
223         }\r
224 \r
225         /* Check the reg test tasks are still cycling.  They will stop incrementing\r
226         their loop counters if they encounter an error. */\r
227         if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )\r
228         {\r
229                 lErrorStatus = pdFAIL;\r
230         }\r
231 \r
232         if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )\r
233         {\r
234                 lErrorStatus = pdFAIL;\r
235         }\r
236 \r
237         /* Remember the loop counter values this time around so they can be checked\r
238         again the next time this callback function executes. */\r
239         ulLastRegTest1CycleCount = ulRegTest1CycleCount;\r
240         ulLastRegTest2CycleCount = ulRegTest2CycleCount;\r
241 \r
242         /* Toggle the check LED to give an indication of the system status.  If\r
243         the LED toggles every three seconds then everything is ok.  A faster toggle\r
244         indicates an error. */\r
245         vParTestToggleLED( mainCHECK_LED );\r
246 \r
247         /* Was an error detected this time through the callback execution? */\r
248         if( lErrorStatus != pdPASS )\r
249         {\r
250                 if( lChangedTimerPeriodAlready == pdFALSE )\r
251                 {\r
252                         lChangedTimerPeriodAlready = pdTRUE;\r
253 \r
254                         /* This call to xTimerChangePeriod() uses a zero block time.\r
255                         Functions called from inside of a timer callback function must\r
256                         *never* attempt to block. */\r
257                         xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
258                 }\r
259         }\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 /* This function is explained in the comments at the top of this file. */\r
264 static void prvRegTest1Task( void *pvParameters )\r
265 {\r
266         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )\r
267         {\r
268                 /* The parameter did not contain the expected value. */\r
269                 for( ;; )\r
270                 {\r
271                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
272                         taskDISABLE_INTERRUPTS();\r
273                 }\r
274         }\r
275 \r
276         /* This is an inline asm function that never returns. */\r
277         vRegTest1Implementation();\r
278 }\r
279 /*-----------------------------------------------------------*/\r
280 \r
281 /* This function is explained in the comments at the top of this file. */\r
282 static void prvRegTest2Task( void *pvParameters )\r
283 {\r
284         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )\r
285         {\r
286                 /* The parameter did not contain the expected value. */\r
287                 for( ;; )\r
288                 {\r
289                         /* Stop the tick interrupt so its obvious something has gone wrong. */\r
290                         taskDISABLE_INTERRUPTS();\r
291                 }\r
292         }\r
293 \r
294         /* This is an inline asm function that never returns. */\r
295         vRegTest2Implementation();\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 #endif /* configCREATE_LOW_POWER_DEMO */\r