]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_M0_STM32F0518_IAR/main-full.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / CORTEX_M0_STM32F0518_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  * NOTE 1:  This project provides two demo applications.  A simple blinky style
72  * project, and a more comprehensive test and demo application.  The
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
74  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
75  * in main.c.  This file implements the comprehensive test and demo version.
76  *
77  * NOTE 2:  This file only contains the source code that is specific to the
78  * full demo.  Generic functions, such FreeRTOS hook functions, and functions
79  * required to configure the hardware, are defined in main.c.
80  ******************************************************************************
81  *
82  * main_full() creates a set of standard demo tasks, some application specific
83  * tasks, and four timers.  It then starts the scheduler.  The web documentation
84  * provides more details of the standard demo application tasks, which provide
85  * no particular functionality, but do provide a good example of how to use the
86  * FreeRTOS API.
87  *
88  * In addition to the standard demo tasks, the following tasks and timer are
89  * defined and/or created within this file:
90  *
91  * "Reg test" tasks - These fill the registers with known values, then check
92  * that each register maintains its expected value for the lifetime of the
93  * task.  Each task uses a different set of values.  The reg test tasks execute
94  * with a very low priority, so get preempted very frequently.  A register
95  * containing an unexpected value is indicative of an error in the context
96  * switching mechanism.
97  *
98  * "Flash timers" - A software timer callback function is defined that does
99  * nothing but toggle an LED.  Three software timers are created that each
100  * use the same callback function, but each toggles a different LED at a
101  * different frequency.  One software timer uses LED1, another LED2 and the
102  * third LED3.
103  *
104  * "Check" software timer - The check timer period is initially set to three
105  * seconds.  Its callback function checks that all the standard demo tasks, and
106  * the register check tasks, are not only still executing, but are executing
107  * without reporting any errors.  If the check timer callback discovers that a
108  * task has either stalled, or reported an error, then it changes the period of
109  * the check timer from the initial three seconds, to just 200ms.  The callback
110  * function also toggles LED 4 each time it is called.  This provides a visual
111  * indication of the system status:  If the LED toggles every three seconds,
112  * then no issues have been discovered.  If the LED toggles every 200ms, then
113  * an issue has been discovered with at least one task.
114  */
115
116 /* Kernel includes. */
117 #include "FreeRTOS.h"
118 #include "task.h"
119 #include "queue.h"
120 #include "timers.h"
121
122 /* Common demo includes. */
123 #include "blocktim.h"
124 #include "countsem.h"
125 #include "recmutex.h"
126 #include "ParTest.h"
127 #include "dynamic.h"
128
129 /* Hardware includes. */
130 #include "stm320518_eval.h"
131
132 /* The period after which the check timer will expire provided no errors have
133 been reported by any of the standard demo tasks.  ms are converted to the
134 equivalent in ticks using the portTICK_PERIOD_MS constant. */
135 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )
136
137 /* The period at which the check timer will expire if an error has been
138 reported in one of the standard demo tasks.  ms are converted to the equivalent
139 in ticks using the portTICK_PERIOD_MS constant. */
140 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )
141
142 /* A block time of zero simply means "don't block". */
143 #define mainDONT_BLOCK                                          ( 0UL )
144
145 /* The base toggle rate used by the flash timers.  Each toggle rate is a
146 multiple of this. */
147 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_PERIOD_MS )
148
149 /* The LED toggle by the check timer. */
150 #define mainCHECK_LED                                           ( 3 )
151 /*-----------------------------------------------------------*/
152
153 /*
154  * Register check tasks, as described at the top of this file.  The nature of
155  * these files necessitates that they are written in an assembly.
156  */
157 extern void vRegTest1Task( void *pvParameters );
158 extern void vRegTest2Task( void *pvParameters );
159
160 /*
161  * The hardware only has a single LED.  Simply toggle it.
162  */
163 extern void vMainToggleLED( void );
164
165 /*
166  * The check timer callback function, as described at the top of this file.
167  */
168 static void prvCheckTimerCallback( TimerHandle_t xTimer );
169
170 /*
171  * The flash timer callback function, as described at the top of this file.
172  * This callback function is assigned to three separate software timers.
173  */
174 static void prvFlashTimerCallback( TimerHandle_t xTimer );
175
176 /*
177  * Called by main() to create the comprehensive test/demo application if
178  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
179  */
180 void main_full( void );
181
182 /*-----------------------------------------------------------*/
183
184 /* The following two variables are used to communicate the status of the
185 register check tasks to the check software timer.  If the variables keep
186 incrementing, then the register check tasks have not discovered any errors.  If
187 a variable stops incrementing, then an error has been found. */
188 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
189
190 /*-----------------------------------------------------------*/
191
192 void main_full( void )
193 {
194 TimerHandle_t xTimer = NULL;
195 unsigned long ulTimer;
196 const unsigned long ulTimersToCreate = 3L;
197 /* The register test tasks are asm functions that don't use a stack.  The
198 stack allocated just has to be large enough to hold the task context, and
199 for the additional required for the stack overflow checking to work (if
200 configured). */
201 const size_t xRegTestStackSize = 25U;
202
203         /* Create the standard demo tasks */
204         vCreateBlockTimeTasks();
205         vStartCountingSemaphoreTasks();
206         vStartRecursiveMutexTasks();
207         vStartDynamicPriorityTasks();
208
209         /* Create the register test tasks as described at the top of this file.
210         These are naked functions that don't use any stack.  A stack still has
211         to be allocated to hold the task context. */
212         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */
213                                         "Reg1",                                 /* Text name of the task. */
214                                         xRegTestStackSize,              /* Stack allocated to the task. */
215                                         NULL,                                   /* The task parameter is not used. */
216                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */
217                                         NULL );                                 /* Don't receive a handle back, it is not needed. */
218
219         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */
220                                         "Reg2",                                 /* Text name of the task. */
221                                         xRegTestStackSize,              /* Stack allocated to the task. */
222                                         NULL,                                   /* The task parameter is not used. */
223                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */
224                                         NULL );                                 /* Don't receive a handle back, it is not needed. */
225
226         /* Create the three flash timers. */
227         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
228         {
229                 xTimer = xTimerCreate(  "FlashTimer",                           /* A text name, purely to help debugging. */
230                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */
231                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
232                                                                 ( void * ) ulTimer,                     /* The ID is used to hold the number of the LED that will be flashed. */
233                                                                 prvFlashTimerCallback           /* The callback function that inspects the status of all the other tasks. */
234                                                         );
235
236                 if( xTimer != NULL )
237                 {
238                         xTimerStart( xTimer, mainDONT_BLOCK );
239                 }
240         }
241
242         /* Create the software timer that performs the 'check' functionality,
243         as described at the top of this file. */
244         xTimer = xTimerCreate(  "CheckTimer",                                   /* A text name, purely to help debugging. */
245                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */
246                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
247                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
248                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */
249                                                 );
250
251         /* If the software timer was created successfully, start it.  It won't
252         actually start running until the scheduler starts.  A block time of
253         zero is used in this call, although any value could be used as the block
254         time will be ignored because the scheduler has not started yet. */
255         if( xTimer != NULL )
256         {
257                 xTimerStart( xTimer, mainDONT_BLOCK );
258         }
259
260         /* Start the kernel.  From here on, only tasks and interrupts will run. */
261         vTaskStartScheduler();
262
263         /* If all is well, the scheduler will now be running, and the following
264         line will never be reached.  If the following line does execute, then there
265         was     insufficient FreeRTOS heap memory available for the idle and/or timer
266         tasks to be created.  See the memory management section on the FreeRTOS web
267         site, or the FreeRTOS tutorial books for more details. */
268         for( ;; );
269 }
270 /*-----------------------------------------------------------*/
271
272 /* See the description at the top of this file. */
273 static void prvCheckTimerCallback( TimerHandle_t xTimer )
274 {
275 static long lChangedTimerPeriodAlready = pdFALSE;
276 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
277 unsigned long ulErrorFound = pdFALSE;
278
279         /* Check all the demo and test tasks to ensure that they are all still
280         running, and that none have detected an error. */
281         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
282         {
283                 ulErrorFound |= ( 0x01UL << 0UL );
284         }
285
286         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
287         {
288                 ulErrorFound |= ( 0x01UL << 1UL );
289         }
290
291         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
292         {
293                 ulErrorFound |= ( 0x01UL << 2UL );
294         }
295
296         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
297         {
298                 ulErrorFound |= ( 0x01UL << 3UL );
299         }
300
301         /* Check that the register test 1 task is still running. */
302         if( ulLastRegTest1Value == ulRegTest1LoopCounter )
303         {
304                 ulErrorFound |= ( 0x01UL << 4UL );
305         }
306         ulLastRegTest1Value = ulRegTest1LoopCounter;
307
308         /* Check that the register test 2 task is still running. */
309         if( ulLastRegTest2Value == ulRegTest2LoopCounter )
310         {
311                 ulErrorFound |= ( 0x01UL << 5UL );
312         }
313         ulLastRegTest2Value = ulRegTest2LoopCounter;
314
315         /* Toggle the check LED to give an indication of the system status.  If
316         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
317         everything is ok.  A faster toggle indicates an error. */
318         vParTestToggleLED( mainCHECK_LED );
319
320         /* Have any errors been latched in ulErrorFound?  If so, shorten the
321         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
322         This will result in an increase in the rate at which mainCHECK_LED
323         toggles. */
324         if( ulErrorFound != pdFALSE )
325         {
326                 if( lChangedTimerPeriodAlready == pdFALSE )
327                 {
328                         lChangedTimerPeriodAlready = pdTRUE;
329
330                         /* This call to xTimerChangePeriod() uses a zero block time.
331                         Functions called from inside of a timer callback function must
332                         *never* attempt to block. */
333                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
334                 }
335         }
336 }
337 /*-----------------------------------------------------------*/
338
339 static void prvFlashTimerCallback( TimerHandle_t xTimer )
340 {
341 unsigned long ulLED;
342
343         /* This callback function is assigned to three separate software timers.
344         Each timer toggles a different LED.  Obtain the number of the LED that
345         this timer is toggling. */
346         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
347
348         /* Toggle the LED. */
349         vParTestToggleLED( ulLED );
350 }
351