2 * FreeRTOS Kernel V10.2.1
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /******************************************************************************
29 * NOTE 1: This project provides two demo applications. A simple blinky style
30 * project, and a more comprehensive test and demo application. The
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
32 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
33 * in main.c. This file implements the comprehensive test and demo version.
35 * NOTE 2: This file only contains the source code that is specific to the
36 * full demo. Generic functions, such FreeRTOS hook functions, and functions
37 * required to configure the hardware, are defined in main.c.
38 ******************************************************************************
40 * main_full() creates a set of standard demo tasks, some application specific
41 * tasks, and four timers. It then starts the scheduler. The web documentation
42 * provides more details of the standard demo application tasks, which provide
43 * no particular functionality, but do provide a good example of how to use the
46 * In addition to the standard demo tasks, the following tasks and timer are
47 * defined and/or created within this file:
49 * "Reg test" tasks - These fill the registers with known values, then check
50 * that each register maintains its expected value for the lifetime of the
51 * task. Each task uses a different set of values. The reg test tasks execute
52 * with a very low priority, so get preempted very frequently. A register
53 * containing an unexpected value is indicative of an error in the context
54 * switching mechanism.
56 * "Flash timers" - A software timer callback function is defined that does
57 * nothing but toggle an LED. Three software timers are created that each
58 * use the same callback function, but each toggles a different LED at a
59 * different frequency. One software timer uses LED1, another LED2 and the
62 * "Check" software timer - The check timer period is initially set to three
63 * seconds. Its callback function checks that all the standard demo tasks, and
64 * the register check tasks, are not only still executing, but are executing
65 * without reporting any errors. If the check timer callback discovers that a
66 * task has either stalled, or reported an error, then it changes the period of
67 * the check timer from the initial three seconds, to just 200ms. The callback
68 * function also toggles LED 4 each time it is called. This provides a visual
69 * indication of the system status: If the LED toggles every three seconds,
70 * then no issues have been discovered. If the LED toggles every 200ms, then
71 * an issue has been discovered with at least one task.
74 /* Kernel includes. */
80 /* Common demo includes. */
87 /* Hardware includes. */
88 #include "stm320518_eval.h"
90 /* The period after which the check timer will expire provided no errors have
91 been reported by any of the standard demo tasks. ms are converted to the
92 equivalent in ticks using the portTICK_PERIOD_MS constant. */
93 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
95 /* The period at which the check timer will expire if an error has been
96 reported in one of the standard demo tasks. ms are converted to the equivalent
97 in ticks using the portTICK_PERIOD_MS constant. */
98 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
100 /* A block time of zero simply means "don't block". */
101 #define mainDONT_BLOCK ( 0UL )
103 /* The base toggle rate used by the flash timers. Each toggle rate is a
105 #define mainFLASH_TIMER_BASE_RATE ( 200UL / portTICK_PERIOD_MS )
107 /* The LED toggle by the check timer. */
108 #define mainCHECK_LED ( 3 )
109 /*-----------------------------------------------------------*/
112 * Register check tasks, as described at the top of this file. The nature of
113 * these files necessitates that they are written in an assembly.
115 extern void vRegTest1Task( void *pvParameters );
116 extern void vRegTest2Task( void *pvParameters );
119 * The hardware only has a single LED. Simply toggle it.
121 extern void vMainToggleLED( void );
124 * The check timer callback function, as described at the top of this file.
126 static void prvCheckTimerCallback( TimerHandle_t xTimer );
129 * The flash timer callback function, as described at the top of this file.
130 * This callback function is assigned to three separate software timers.
132 static void prvFlashTimerCallback( TimerHandle_t xTimer );
135 * Called by main() to create the comprehensive test/demo application if
136 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
138 void main_full( void );
140 /*-----------------------------------------------------------*/
142 /* The following two variables are used to communicate the status of the
143 register check tasks to the check software timer. If the variables keep
144 incrementing, then the register check tasks have not discovered any errors. If
145 a variable stops incrementing, then an error has been found. */
146 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
148 /*-----------------------------------------------------------*/
150 void main_full( void )
152 TimerHandle_t xTimer = NULL;
153 unsigned long ulTimer;
154 const unsigned long ulTimersToCreate = 3L;
155 /* The register test tasks are asm functions that don't use a stack. The
156 stack allocated just has to be large enough to hold the task context, and
157 for the additional required for the stack overflow checking to work (if
159 const size_t xRegTestStackSize = 25U;
161 /* Create the standard demo tasks */
162 vCreateBlockTimeTasks();
163 vStartCountingSemaphoreTasks();
164 vStartRecursiveMutexTasks();
165 vStartDynamicPriorityTasks();
167 /* Create the register test tasks as described at the top of this file.
168 These are naked functions that don't use any stack. A stack still has
169 to be allocated to hold the task context. */
170 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
171 "Reg1", /* Text name of the task. */
172 xRegTestStackSize, /* Stack allocated to the task. */
173 NULL, /* The task parameter is not used. */
174 tskIDLE_PRIORITY, /* The priority to assign to the task. */
175 NULL ); /* Don't receive a handle back, it is not needed. */
177 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
178 "Reg2", /* Text name of the task. */
179 xRegTestStackSize, /* Stack allocated to the task. */
180 NULL, /* The task parameter is not used. */
181 tskIDLE_PRIORITY, /* The priority to assign to the task. */
182 NULL ); /* Don't receive a handle back, it is not needed. */
184 /* Create the three flash timers. */
185 for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
187 xTimer = xTimerCreate( "FlashTimer", /* A text name, purely to help debugging. */
188 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ), /* The timer period, in this case 3000ms (3s). */
189 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
190 ( void * ) ulTimer, /* The ID is used to hold the number of the LED that will be flashed. */
191 prvFlashTimerCallback /* The callback function that inspects the status of all the other tasks. */
196 xTimerStart( xTimer, mainDONT_BLOCK );
200 /* Create the software timer that performs the 'check' functionality,
201 as described at the top of this file. */
202 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
203 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
204 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
205 ( void * ) 0, /* The ID is not used, so can be set to anything. */
206 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
209 /* If the software timer was created successfully, start it. It won't
210 actually start running until the scheduler starts. A block time of
211 zero is used in this call, although any value could be used as the block
212 time will be ignored because the scheduler has not started yet. */
215 xTimerStart( xTimer, mainDONT_BLOCK );
218 /* Start the kernel. From here on, only tasks and interrupts will run. */
219 vTaskStartScheduler();
221 /* If all is well, the scheduler will now be running, and the following
222 line will never be reached. If the following line does execute, then there
223 was insufficient FreeRTOS heap memory available for the idle and/or timer
224 tasks to be created. See the memory management section on the FreeRTOS web
225 site, or the FreeRTOS tutorial books for more details. */
228 /*-----------------------------------------------------------*/
230 /* See the description at the top of this file. */
231 static void prvCheckTimerCallback( TimerHandle_t xTimer )
233 static long lChangedTimerPeriodAlready = pdFALSE;
234 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
235 unsigned long ulErrorFound = pdFALSE;
237 /* Check all the demo and test tasks to ensure that they are all still
238 running, and that none have detected an error. */
239 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
241 ulErrorFound |= ( 0x01UL << 0UL );
244 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
246 ulErrorFound |= ( 0x01UL << 1UL );
249 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
251 ulErrorFound |= ( 0x01UL << 2UL );
254 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
256 ulErrorFound |= ( 0x01UL << 3UL );
259 /* Check that the register test 1 task is still running. */
260 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
262 ulErrorFound |= ( 0x01UL << 4UL );
264 ulLastRegTest1Value = ulRegTest1LoopCounter;
266 /* Check that the register test 2 task is still running. */
267 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
269 ulErrorFound |= ( 0x01UL << 5UL );
271 ulLastRegTest2Value = ulRegTest2LoopCounter;
273 /* Toggle the check LED to give an indication of the system status. If
274 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
275 everything is ok. A faster toggle indicates an error. */
276 vParTestToggleLED( mainCHECK_LED );
278 /* Have any errors been latched in ulErrorFound? If so, shorten the
279 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
280 This will result in an increase in the rate at which mainCHECK_LED
282 if( ulErrorFound != pdFALSE )
284 if( lChangedTimerPeriodAlready == pdFALSE )
286 lChangedTimerPeriodAlready = pdTRUE;
288 /* This call to xTimerChangePeriod() uses a zero block time.
289 Functions called from inside of a timer callback function must
290 *never* attempt to block. */
291 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
295 /*-----------------------------------------------------------*/
297 static void prvFlashTimerCallback( TimerHandle_t xTimer )
301 /* This callback function is assigned to three separate software timers.
302 Each timer toggles a different LED. Obtain the number of the LED that
303 this timer is toggling. */
304 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
306 /* Toggle the LED. */
307 vParTestToggleLED( ulLED );