2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
30 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
31 * project, and a more comprehensive test and demo application. The
\r
32 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
33 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
34 * in main.c. This file implements the comprehensive test and demo version.
\r
36 * NOTE 2: This file only contains the source code that is specific to the
\r
37 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
38 * required to configure the hardware, are defined in main.c.
\r
39 ******************************************************************************
\r
41 * main_full() creates a set of standard demo tasks, some application specific
\r
42 * tasks, and four timers. It then starts the scheduler. The web documentation
\r
43 * provides more details of the standard demo application tasks, which provide
\r
44 * no particular functionality, but do provide a good example of how to use the
\r
47 * In addition to the standard demo tasks, the following tasks and timer are
\r
48 * defined and/or created within this file:
\r
50 * "Reg test" tasks - These fill the registers with known values, then check
\r
51 * that each register maintains its expected value for the lifetime of the
\r
52 * task. Each task uses a different set of values. The reg test tasks execute
\r
53 * with a very low priority, so get preempted very frequently. A register
\r
54 * containing an unexpected value is indicative of an error in the context
\r
55 * switching mechanism.
\r
57 * "Interrupt semaphore take" task - This task does nothing but block on a
\r
58 * semaphore that is 'given' from the tick hook function (which is defined in
\r
59 * main.c). It toggles the fourth LED each time it receives the semaphore. The
\r
60 * Semahore is given every 50ms, so LED 4 toggles every 50ms.
\r
62 * "Flash timers" - A software timer callback function is defined that does
\r
63 * nothing but toggle an LED. Three software timers are created that each
\r
64 * use the same callback function, but each toggles a different LED at a
\r
65 * different frequency. The timers control the first three LEDs.
\r
67 * "Check" software timer - The check timer period is initially set to three
\r
68 * seconds. Its callback function checks that all the standard demo tasks, and
\r
69 * the register check tasks, are not only still executing, but are executing
\r
70 * without reporting any errors. If the check timer callback discovers that a
\r
71 * task has either stalled, or reported an error, then it changes the period of
\r
72 * the check timer from the initial three seconds, to just 200ms. The callback
\r
73 * function also toggles the fifth LED each time it is called. This provides a
\r
74 * visual indication of the system status: If the LED toggles every three
\r
75 * seconds then no issues have been discovered. If the LED toggles every 200ms,
\r
76 * then an issue has been discovered with at least one task.
\r
79 /* Kernel includes. */
\r
80 #include "FreeRTOS.h"
\r
86 /* Common demo includes. */
\r
87 #include "blocktim.h"
\r
88 #include "countsem.h"
\r
89 #include "recmutex.h"
\r
90 #include "ParTest.h"
\r
91 #include "dynamic.h"
\r
92 #include "QueueOverwrite.h"
\r
93 #include "QueueSet.h"
\r
95 /* The period after which the check timer will expire provided no errors have
\r
96 been reported by any of the standard demo tasks. ms are converted to the
\r
97 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
98 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
100 /* The period at which the check timer will expire if an error has been
\r
101 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
102 in ticks using the portTICK_PERIOD_MS constant. */
\r
103 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
105 /* A block time of zero simply means "don't block". */
\r
106 #define mainDONT_BLOCK ( 0UL )
\r
108 /* The base toggle rate used by the flash timers. Each toggle rate is a
\r
109 multiple of this. */
\r
110 #define mainFLASH_TIMER_BASE_RATE ( 200UL / portTICK_PERIOD_MS )
\r
112 /* The LED toggle by the check timer. */
\r
113 #define mainCHECK_LED ( 4 )
\r
115 /* The LED toggled each time the task implemented by the prvSemaphoreTakeTask()
\r
116 function takes the semaphore that is given by the tick hook function. */
\r
117 #define mainSEMAPHORE_LED ( 3 )
\r
119 /*-----------------------------------------------------------*/
\r
122 * Register check tasks, as described at the top of this file. The nature of
\r
123 * these files necessitates that they are written in an assembly.
\r
125 extern void vRegTest1Task( void *pvParameters );
\r
126 extern void vRegTest2Task( void *pvParameters );
\r
129 * The hardware only has a single LED. Simply toggle it.
\r
131 extern void vMainToggleLED( void );
\r
134 * The check timer callback function, as described at the top of this file.
\r
136 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
139 * The flash timer callback function, as described at the top of this file.
\r
140 * This callback function is assigned to three separate software timers.
\r
142 static void prvFlashTimerCallback( TimerHandle_t xTimer );
\r
145 * The task that toggles an LED each time the semaphore 'given' by the tick
\r
146 * hook function (which is defined in main.c) is 'taken' in the task.
\r
148 static void prvSemaphoreTakeTask( void *pvParameters );
\r
151 * Called by main() to create the comprehensive test/demo application if
\r
152 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
\r
154 void main_full( void );
\r
156 /*-----------------------------------------------------------*/
\r
158 /* The following two variables are used to communicate the status of the
\r
159 register check tasks to the check software timer. If the variables keep
\r
160 incrementing, then the register check tasks have not discovered any errors. If
\r
161 a variable stops incrementing, then an error has been found. */
\r
162 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
164 /* The semaphore that is given by the tick hook function (defined in main.c)
\r
165 and taken by the task implemented by the prvSemaphoreTakeTask() function. The
\r
166 task toggles LED mainSEMAPHORE_LED each time the semaphore is taken. */
\r
167 SemaphoreHandle_t xLEDSemaphore = NULL;
\r
168 /*-----------------------------------------------------------*/
\r
170 void main_full( void )
\r
172 TimerHandle_t xTimer = NULL;
\r
173 unsigned long ulTimer;
\r
174 const unsigned long ulTimersToCreate = 3L;
\r
175 /* The register test tasks are asm functions that don't use a stack. The
\r
176 stack allocated just has to be large enough to hold the task context, and
\r
177 for the additional required for the stack overflow checking to work (if
\r
179 const size_t xRegTestStackSize = 25U;
\r
181 /* Create the standard demo tasks */
\r
182 vCreateBlockTimeTasks();
\r
183 vStartDynamicPriorityTasks();
\r
184 vStartCountingSemaphoreTasks();
\r
185 vStartRecursiveMutexTasks();
\r
186 vStartQueueOverwriteTask( tskIDLE_PRIORITY );
\r
187 vStartQueueSetTasks();
\r
189 /* Create that is given from the tick hook function, and the task that
\r
190 toggles an LED each time the semaphore is given. */
\r
191 vSemaphoreCreateBinary( xLEDSemaphore );
\r
192 xTaskCreate( prvSemaphoreTakeTask, /* Function that implements the task. */
\r
193 "Sem", /* Text name of the task. */
\r
194 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (in words). */
\r
195 NULL, /* The task parameter is not used. */
\r
196 configMAX_PRIORITIES - 2, /* The priority of the task. */
\r
197 NULL ); /* Don't receive a handle back, it is not needed. */
\r
199 /* Create the register test tasks as described at the top of this file.
\r
200 These are naked functions that don't use any stack. A stack still has
\r
201 to be allocated to hold the task context. */
\r
202 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
\r
203 "Reg1", /* Text name of the task. */
\r
204 xRegTestStackSize, /* Stack allocated to the task. */
\r
205 NULL, /* The task parameter is not used. */
\r
206 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
207 NULL ); /* Don't receive a handle back, it is not needed. */
\r
209 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
\r
210 "Reg2", /* Text name of the task. */
\r
211 xRegTestStackSize, /* Stack allocated to the task. */
\r
212 NULL, /* The task parameter is not used. */
\r
213 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
214 NULL ); /* Don't receive a handle back, it is not needed. */
\r
216 /* Create the three flash timers. */
\r
217 for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
\r
219 xTimer = xTimerCreate( "FlashTimer", /* A text name, purely to help debugging. */
\r
220 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ), /* The timer period, in this case 3000ms (3s). */
\r
221 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
222 ( void * ) ulTimer, /* The ID is used to hold the number of the LED that will be flashed. */
\r
223 prvFlashTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
226 if( xTimer != NULL )
\r
228 xTimerStart( xTimer, mainDONT_BLOCK );
\r
232 /* Create the software timer that performs the 'check' functionality,
\r
233 as described at the top of this file. */
\r
234 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
235 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
236 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
237 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
238 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
241 /* If the software timer was created successfully, start it. It won't
\r
242 actually start running until the scheduler starts. A block time of
\r
243 zero is used in this call, although any value could be used as the block
\r
244 time will be ignored because the scheduler has not started yet. */
\r
245 if( xTimer != NULL )
\r
247 xTimerStart( xTimer, mainDONT_BLOCK );
\r
250 /* Start the kernel. From here on, only tasks and interrupts will run. */
\r
251 vTaskStartScheduler();
\r
253 /* If all is well, the scheduler will now be running, and the following
\r
254 line will never be reached. If the following line does execute, then there
\r
255 was insufficient FreeRTOS heap memory available for the idle and/or timer
\r
256 tasks to be created. See the memory management section on the FreeRTOS web
\r
257 site, or the FreeRTOS tutorial books for more details. */
\r
260 /*-----------------------------------------------------------*/
\r
262 /* See the description at the top of this file. */
\r
263 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
265 static long lChangedTimerPeriodAlready = pdFALSE;
\r
266 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
267 unsigned long ulErrorFound = pdFALSE;
\r
269 /* Check all the demo and test tasks to ensure that they are all still
\r
270 running, and that none have detected an error. */
\r
271 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
273 ulErrorFound |= ( 0x01UL << 0UL );
\r
276 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
278 ulErrorFound |= ( 0x01UL << 1UL );
\r
281 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
\r
283 ulErrorFound |= ( 0x01UL << 2UL );
\r
286 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
\r
288 ulErrorFound |= ( 0x01UL << 3UL );
\r
291 /* Check that the register test 1 task is still running. */
\r
292 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
294 ulErrorFound |= ( 0x01UL << 4UL );
\r
296 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
298 /* Check that the register test 2 task is still running. */
\r
299 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
301 ulErrorFound |= ( 0x01UL << 5UL );
\r
303 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
305 if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
307 ulErrorFound |= ( 0x01UL << 6UL );
\r
310 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
312 ulErrorFound |= ( 0x01UL << 7UL );
\r
315 /* Toggle the check LED to give an indication of the system status. If
\r
316 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
317 everything is ok. A faster toggle indicates an error. */
\r
318 vParTestToggleLED( mainCHECK_LED );
\r
320 /* Have any errors been latched in ulErrorFound? If so, shorten the
\r
321 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
322 This will result in an increase in the rate at which mainCHECK_LED
\r
324 if( ulErrorFound != pdFALSE )
\r
326 if( lChangedTimerPeriodAlready == pdFALSE )
\r
328 lChangedTimerPeriodAlready = pdTRUE;
\r
330 /* This call to xTimerChangePeriod() uses a zero block time.
\r
331 Functions called from inside of a timer callback function must
\r
332 *never* attempt to block. */
\r
333 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
337 /*-----------------------------------------------------------*/
\r
339 static void prvSemaphoreTakeTask( void *pvParameters )
\r
341 configASSERT( xLEDSemaphore );
\r
345 /* Wait to obtain the semaphore - which is given by the tick hook
\r
346 function every 50ms. */
\r
347 xSemaphoreTake( xLEDSemaphore, portMAX_DELAY );
\r
348 vParTestToggleLED( mainSEMAPHORE_LED );
\r
351 /*-----------------------------------------------------------*/
\r
353 static void prvFlashTimerCallback( TimerHandle_t xTimer )
\r
355 unsigned long ulLED;
\r
357 /* This callback function is assigned to three separate software timers.
\r
358 Each timer toggles a different LED. Obtain the number of the LED that
\r
359 this timer is toggling. */
\r
360 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
\r
362 /* Toggle the LED. */
\r
363 vParTestToggleLED( ulLED );
\r