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