]> begriffs open source - cmsis-freertos/blob - Demo/RX100-RSK_IAR/main_full.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / RX100-RSK_IAR / main_full.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /* ****************************************************************************
71  * This project includes a lot of tasks and tests and is therefore complex.
72  * If you would prefer a much simpler project to get started with then select
73  * the 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in
74  * FreeRTOSConfig.h.  When configCREATE_LOW_POWER_DEMO is set to 1 main() will
75  * call main_low_power() instead of main_full().
76  * ****************************************************************************
77  *
78  * Creates all the demo application tasks, then starts the scheduler.  The web
79  * documentation provides more details of the standard demo application tasks,
80  * which provide no particular functionality but do provide a good example of
81  * how to use the FreeRTOS API.
82  *
83  * In addition to the standard demo tasks, the following tasks and tests are
84  * defined and/or created within this file:
85  *
86  * "Reg test" tasks - These fill the registers with known values, then
87  * repeatedly check that each register still contains its expected value for
88  * the lifetime of the tasks.  Each task uses different values.  The tasks run
89  * with very low priority so get preempted very frequently.  A check variable
90  * is incremented on each iteration of the test loop.  A register containing an
91  * unexpected value is indicative of an error in the context switching
92  * mechanism and will result in a branch to a null loop - which in turn will
93  * prevent the check variable from incrementing any further and allow the check
94  * timer (described below) to determine that an error has occurred.  The nature
95  * of the reg test tasks necessitates that they are written in assembly code.
96  *
97  * "Check Timer" and Callback Function - The check timer period is initially
98  * set to three seconds.  The check timer callback function checks that all the
99  * standard demo tasks are not only still executing, but are executing without
100  * reporting any errors.  If the check timer discovers that a task has either
101  * stalled, or reported an error, then it changes its own period from the
102  * initial three seconds, to just 200ms.  The check timer callback function
103  * also toggles LED 0 each time it is called.  This provides a visual
104  * indication of the system status:  If the LED toggles every three seconds,
105  * then no issues have been discovered.  If the LED toggles every 200ms, then
106  * an issue has been discovered with at least one task.
107  *
108  * *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.
109  * The PowerON_Reset_PC() supplied in resetprg.c with this demo has
110  * Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
111 */
112
113 /* Standard includes. */
114 #include <string.h>
115
116 /* Hardware specific includes. */
117 #include "iorx111.h"
118
119 /* Kernel includes. */
120 #include "FreeRTOS.h"
121 #include "task.h"
122 #include "timers.h"
123 #include "semphr.h"
124
125 /* Standard demo includes. */
126 #include "partest.h"
127 #include "death.h"
128 #include "blocktim.h"
129 #include "GenQTest.h"
130 #include "recmutex.h"
131
132
133 /* Variables that are incremented on each iteration of the reg test tasks are
134 declared outside of the #if configCREATE_LOW_POWER_DEMO conditional compilation
135 to prevent linker issues when configCREATE_LOW_POWER_DEMO is set to 1.  The
136 check timer inspects these variables to ensure they are still incrementing as
137 expected.  If a variable stops incrementing then it is likely that its associate
138 task has stalled. */
139 unsigned long volatile ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
140
141
142 /* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set
143 to 0, otherwise the code in main_low_power.c is used. */
144 #if configCREATE_LOW_POWER_DEMO == 0
145
146
147 /* Values that are passed into the reg test tasks using the task parameter.
148 The tasks check that the values are passed in correctly. */
149 #define mainREG_TEST_1_PARAMETER        ( 0x12121212UL )
150 #define mainREG_TEST_2_PARAMETER        ( 0x12345678UL )
151
152 /* Priorities at which the standard demo tasks are created. */
153 #define mainGEN_QUEUE_TASK_PRIORITY     ( tskIDLE_PRIORITY )
154 #define mainCREATOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 3 )
155
156 /* The LED toggled by the check timer. */
157 #define mainCHECK_LED                           ( 0 )
158
159 /* The period at which the check timer will expire, in ms, provided no errors
160 have been reported by any of the standard demo tasks.  ms are converted to the
161 equivalent in ticks using the portTICK_PERIOD_MS constant. */
162 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )
163
164 /* The period at which the check timer will expire, in ms, if an error has been
165 reported in one of the standard demo tasks.  ms are converted to the equivalent
166 in ticks using the portTICK_PERIOD_MS constant. */
167 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )
168
169 /* A block time of zero simple means "Don't Block". */
170 #define mainDONT_BLOCK                          ( 0UL )
171
172 /*
173  * The reg test tasks as described at the top of this file.
174  */
175 static void prvRegTest1Task( void *pvParameters );
176 static void prvRegTest2Task( void *pvParameters );
177
178 /*
179  * The actual implementation of the reg test functionality, which, because of
180  * the direct register access, have to be in assembly.
181  */
182 void vRegTest1Implementation( void );
183 void vRegTest2Implementation( void );
184
185 /*
186  * The check timer callback function, as described at the top of this file.
187  */
188 static void prvCheckTimerCallback( TimerHandle_t xTimer );
189
190
191 /*-----------------------------------------------------------*/
192
193 /* The check timer.  This uses prvCheckTimerCallback() as its callback
194 function. */
195 static TimerHandle_t xCheckTimer = NULL;
196
197 /*-----------------------------------------------------------*/
198
199 void main_full( void )
200 {
201         /* Start the reg test tasks which test the context switching mechanism. */
202         xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
203         xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
204
205         /* Create the standard demo tasks. */
206         vCreateBlockTimeTasks();
207         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
208         vStartRecursiveMutexTasks();
209
210         /* The suicide tasks must be created last as they need to know how many
211         tasks were running prior to their creation in order to ascertain whether
212         or not the correct/expected number of tasks are running at any given time. */
213         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
214
215         /* Create the software timer that performs the 'check' functionality,
216         as described at the top of this file. */
217         xCheckTimer = xTimerCreate( "CheckTimer",/* A text name, purely to help debugging. */
218                                                                 ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 5000ms (5s). */
219                                                                 pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
220                                                                 ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */
221                                                                 prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */
222                                                           );
223
224         configASSERT( xCheckTimer );
225
226         /* Start the check timer.  It will actually start when the scheduler is
227         started. */
228         xTimerStart( xCheckTimer, mainDONT_BLOCK );
229
230         /* Start the tasks running. */
231         vTaskStartScheduler();
232
233         /* If all is well execution will never reach here as the scheduler will be
234         running.  If this null loop is reached then it is likely there was
235         insufficient FreeRTOS heap available for the idle task and/or timer task to
236         be created.  See http://www.freertos.org/a00111.html. */
237         for( ;; );
238 }
239 /*-----------------------------------------------------------*/
240
241 static void prvCheckTimerCallback( TimerHandle_t xTimer )
242 {
243 static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
244 static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
245
246         /* Remove compiler warnings about unused parameters. */
247         ( void ) xTimer;
248
249         /* Check the standard demo tasks are running without error. */
250         if( xAreGenericQueueTasksStillRunning() != pdTRUE )
251         {
252                 lErrorStatus = pdFAIL;
253         }
254         else if( xIsCreateTaskStillRunning() != pdTRUE )
255         {
256                 lErrorStatus = pdFAIL;
257         }
258         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
259         {
260                 lErrorStatus = pdFAIL;
261         }
262         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
263         {
264                 lErrorStatus = pdFAIL;
265         }
266
267         /* Check the reg test tasks are still cycling.  They will stop incrementing
268         their loop counters if they encounter an error. */
269         if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
270         {
271                 lErrorStatus = pdFAIL;
272         }
273
274         if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
275         {
276                 lErrorStatus = pdFAIL;
277         }
278
279         /* Remember the loop counter values this time around so they can be checked
280         again the next time this callback function executes. */
281         ulLastRegTest1CycleCount = ulRegTest1CycleCount;
282         ulLastRegTest2CycleCount = ulRegTest2CycleCount;
283
284         /* Toggle the check LED to give an indication of the system status.  If
285         the LED toggles every three seconds then everything is ok.  A faster toggle
286         indicates an error. */
287         vParTestToggleLED( mainCHECK_LED );
288
289         /* Was an error detected this time through the callback execution? */
290         if( lErrorStatus != pdPASS )
291         {
292                 if( lChangedTimerPeriodAlready == pdFALSE )
293                 {
294                         lChangedTimerPeriodAlready = pdTRUE;
295
296                         /* This call to xTimerChangePeriod() uses a zero block time.
297                         Functions called from inside of a timer callback function must
298                         *never* attempt to block. */
299                         xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
300                 }
301         }
302 }
303 /*-----------------------------------------------------------*/
304
305 /* This function is explained in the comments at the top of this file. */
306 static void prvRegTest1Task( void *pvParameters )
307 {
308         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
309         {
310                 /* The parameter did not contain the expected value. */
311                 for( ;; )
312                 {
313                         /* Stop the tick interrupt so its obvious something has gone wrong. */
314                         taskDISABLE_INTERRUPTS();
315                 }
316         }
317
318         /* This is an inline asm function that never returns. */
319         vRegTest1Implementation();
320 }
321 /*-----------------------------------------------------------*/
322
323 /* This function is explained in the comments at the top of this file. */
324 static void prvRegTest2Task( void *pvParameters )
325 {
326         if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
327         {
328                 /* The parameter did not contain the expected value. */
329                 for( ;; )
330                 {
331                         /* Stop the tick interrupt so its obvious something has gone wrong. */
332                         taskDISABLE_INTERRUPTS();
333                 }
334         }
335
336         /* This is an inline asm function that never returns. */
337         vRegTest2Implementation();
338 }
339 /*-----------------------------------------------------------*/
340
341 #endif /* configCREATE_LOW_POWER_DEMO */