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, along with an example of how to write an
38 * interrupt service routine, are defined in main.c.
39 ******************************************************************************
41 * main_full() creates all the demo application tasks and two software timers,
42 * then starts the scheduler. The web documentation provides more details of
43 * the standard demo application tasks, which provide no particular
44 * functionality, but do provide a good example of how to use the FreeRTOS API.
46 * In addition to the standard demo tasks, the following tasks, tests and
47 * timers are created within this file:
49 * "Reg test" tasks - These fill the registers with known values, then check
50 * that each register still contains its expected value. Each task uses a
51 * different set of values. The reg test tasks execute with a very low priority,
52 * so get preempted very frequently. A register containing an unexpected value
53 * is indicative of an error in the context switching mechanism.
55 * The "Demo" Timer and Callback Function:
56 * The demo timer callback function does nothing more than increment a variable.
57 * The period of the demo timer is set relative to the period of the check timer
58 * (described below). This allows the check timer to know how many times the
59 * demo timer callback function should execute between each execution of the
60 * check timer callback function. The variable incremented in the demo timer
61 * callback function is used to determine how many times the callback function
64 * The "Check" Timer and Callback Function:
65 * The check timer period is initially set to three seconds. The check timer
66 * callback function checks that all the standard demo tasks, the reg test
67 * tasks, and the demo timer are not only still executing, but are executing
68 * without reporting any errors. If the check timer discovers that a task or
69 * timer has stalled, or reported an error, then it changes its own period from
70 * the initial three seconds, to just 200ms. The check timer callback function
71 * also toggles an LED each time it is called. This provides a visual
72 * indication of the system status: If the LED toggles every three seconds,
73 * then no issues have been discovered. If the LED toggles every 200ms, then
74 * an issue has been discovered with at least one task.
76 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
77 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
78 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
82 /* Scheduler include files. */
87 /* Standard demo includes. */
92 /* Hardware includes. */
93 #include "demo_specific_io.h"
95 /* The period at which the check timer will expire, in ms, provided no errors
96 have been reported by any of the standard demo tasks. ms are converted to the
97 equivalent in ticks using the portTICK_PERIOD_MS constant. */
98 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
100 /* The period at which the check timer will expire, in ms, if an error has been
101 reported in one of the standard demo tasks, the check tasks, or the demo timer.
102 ms are converted to the equivalent in ticks using the portTICK_PERIOD_MS
104 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
106 /* These two definitions are used to set the period of the demo timer. The demo
107 timer period is always relative to the check timer period, so the check timer
108 can determine if the demo timer has expired the expected number of times between
109 its own executions. */
110 #define mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT ( 100UL )
111 #define mainDEMO_TIMER_PERIOD_MS ( mainCHECK_TIMER_PERIOD_MS / mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT )
113 /* A block time of zero simply means "don't block". */
114 #define mainDONT_BLOCK ( 0U )
116 /* Values that are passed as parameters into the reg test tasks (purely to
117 ensure task parameters are passed correctly). */
118 #define mainREG_TEST_1_PARAMETER ( ( void * ) 0x1234 )
119 #define mainREG_TEST_2_PARAMETER ( ( void * ) 0x5678 )
121 /*-----------------------------------------------------------*/
124 * The 'check' timer callback function, as described at the top of this file.
126 static void prvCheckTimerCallback( TimerHandle_t xTimer );
129 * The 'demo' timer callback function, as described at the top of this file.
131 static void prvDemoTimerCallback( TimerHandle_t xTimer );
134 * Functions that define the RegTest tasks, as described at the top of this
135 * file. The RegTest tasks are written (necessarily) in assembler. Their
136 * entry points are written in C to allow for easy checking of the task
139 extern void vRegTest1Task( void );
140 extern void vRegTest2Task( void );
141 static void prvRegTest1Entry( void *pvParameters );
142 static void prvRegTest2Entry( void *pvParameters );
145 * Called if a RegTest task discovers an error as a mechanism to stop the
146 * tasks loop counter incrementing (so the check task can detect that an
149 void vRegTestError( void );
152 * Called by main() to create the more comprehensive application if
153 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
155 void main_full( void );
157 /*-----------------------------------------------------------*/
159 /* Variables that are incremented on each cycle of the two reg tests to allow
160 the check timer to know that they are still executing. */
161 unsigned short usRegTest1LoopCounter = 0, usRegTest2LoopCounter;
163 /* The check timer. This uses prvCheckTimerCallback() as its callback
165 static TimerHandle_t xCheckTimer = NULL;
167 /* The demo timer. This uses prvDemoTimerCallback() as its callback function. */
168 static TimerHandle_t xDemoTimer = NULL;
170 /* This variable is incremented each time the demo timer expires. */
171 static volatile unsigned long ulDemoSoftwareTimerCounter = 0UL;
173 /*-----------------------------------------------------------*/
175 void main_full( void )
177 /* Creates all the tasks and timers, then starts the scheduler. */
179 /* First create the 'standard demo' tasks. These are used to demonstrate
180 API functions being used and also to test the kernel port. More information
181 is provided on the FreeRTOS.org WEB site. */
182 vStartDynamicPriorityTasks();
183 vStartPolledQueueTasks( tskIDLE_PRIORITY );
184 vCreateBlockTimeTasks();
186 /* Create the RegTest tasks as described at the top of this file. */
187 xTaskCreate( prvRegTest1Entry, /* The function that implements the task. */
188 "Reg1", /* Text name for the task - to assist debugging only, not used by the kernel. */
189 configMINIMAL_STACK_SIZE, /* The size of the stack allocated to the task (in words, not bytes). */
190 mainREG_TEST_1_PARAMETER, /* The parameter passed into the task. */
191 tskIDLE_PRIORITY, /* The priority at which the task will execute. */
192 NULL ); /* Used to pass the handle of the created task out to the function caller - not used in this case. */
194 xTaskCreate( prvRegTest2Entry, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
196 /* Create the software timer that performs the 'check' functionality,
197 as described at the top of this file. */
198 xCheckTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
199 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
200 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
201 ( void * ) 0, /* The ID is not used, so can be set to anything. */
202 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
205 /* Create the software timer that just increments a variable for demo
207 xDemoTimer = xTimerCreate( "DemoTimer",/* A text name, purely to help debugging. */
208 ( mainDEMO_TIMER_PERIOD_MS ), /* The timer period, in this case it is always calculated relative to the check timer period (see the definition of mainDEMO_TIMER_PERIOD_MS). */
209 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
210 ( void * ) 0, /* The ID is not used, so can be set to anything. */
211 prvDemoTimerCallback /* The callback function that inspects the status of all the other tasks. */
214 /* Start both the check timer and the demo timer. The timers won't actually
215 start until the scheduler is started. */
216 xTimerStart( xCheckTimer, mainDONT_BLOCK );
217 xTimerStart( xDemoTimer, mainDONT_BLOCK );
219 /* Finally start the scheduler running. */
220 vTaskStartScheduler();
222 /* If all is well execution will never reach here as the scheduler will be
223 running. If this null loop is reached then it is likely there was
224 insufficient FreeRTOS heap available for the idle task and/or timer task to
225 be created. See http://www.freertos.org/a00111.html. */
228 /*-----------------------------------------------------------*/
230 static void prvDemoTimerCallback( TimerHandle_t xTimer )
232 /* Remove compiler warning about unused parameter. */
235 /* The demo timer has expired. All it does is increment a variable. The
236 period of the demo timer is relative to that of the check timer, so the
237 check timer knows how many times this variable should have been incremented
238 between each execution of the check timer's own callback. */
239 ulDemoSoftwareTimerCounter++;
241 /*-----------------------------------------------------------*/
243 static void prvCheckTimerCallback( TimerHandle_t xTimer )
245 static portBASE_TYPE xChangedTimerPeriodAlready = pdFALSE, xErrorStatus = pdPASS;
246 static unsigned short usLastRegTest1Counter = 0, usLastRegTest2Counter = 0;
248 /* Remove compiler warning about unused parameter. */
251 /* Inspect the status of the standard demo tasks. */
252 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
254 xErrorStatus = pdFAIL;
257 if( xArePollingQueuesStillRunning() != pdTRUE )
259 xErrorStatus = pdFAIL;
262 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
264 xErrorStatus = pdFAIL;
267 /* Indicate an error if either of the reg test loop counters have not
268 incremented since the last time this function was called. */
269 if( usLastRegTest1Counter == usRegTest1LoopCounter )
271 xErrorStatus = pdFAIL;
275 usLastRegTest1Counter = usRegTest1LoopCounter;
278 if( usLastRegTest2Counter == usRegTest2LoopCounter )
280 xErrorStatus = pdFAIL;
284 usLastRegTest2Counter = usRegTest2LoopCounter;
287 /* Ensure that the demo software timer has expired
288 mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT times in between
289 each call of this function. A critical section is not required to access
290 ulDemoSoftwareTimerCounter as the variable is only accessed from another
291 software timer callback, and only one software timer callback can be
292 executing at any time. */
293 if( ( ulDemoSoftwareTimerCounter < ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT - 1 ) ) ||
294 ( ulDemoSoftwareTimerCounter > ( mainDEMO_TIMER_INCREMENTS_PER_CHECK_TIMER_TIMEOUT + 1 ) )
297 xErrorStatus = pdFAIL;
301 ulDemoSoftwareTimerCounter = 0UL;
304 if( ( xErrorStatus == pdFAIL ) && ( xChangedTimerPeriodAlready == pdFALSE ) )
306 /* An error has occurred, but the timer's period has not yet been changed,
307 change it now, and remember that it has been changed. Shortening the
308 timer's period means the LED will toggle at a faster rate, giving a
309 visible indication that something has gone wrong. */
310 xChangedTimerPeriodAlready = pdTRUE;
312 /* This call to xTimerChangePeriod() uses a zero block time. Functions
313 called from inside of a timer callback function must *never* attempt to
315 xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
318 /* Toggle the LED. The toggle rate will depend on whether or not an error
319 has been found in any tasks. */
322 /*-----------------------------------------------------------*/
324 void vRegTestError( void )
326 /* Called by both reg test tasks if an error is found. There is no way out
327 of this function so the loop counter of the calling task will stop
328 incrementing, which will result in the check timer signaling an error. */
331 /*-----------------------------------------------------------*/
333 static void prvRegTest1Entry( void *pvParameters )
335 /* If the parameter has its expected value then start the first reg test
336 task (this is only done to test that the RTOS port is correctly handling
338 if( pvParameters == mainREG_TEST_1_PARAMETER )
347 /* It is not possible to get here as neither of the two functions called
348 above will ever return. */
350 /*-----------------------------------------------------------*/
352 static void prvRegTest2Entry( void *pvParameters )
354 /* If the parameter has its expected value then start the first reg test
355 task (this is only done to test that the RTOS port is correctly handling
357 if( pvParameters == mainREG_TEST_2_PARAMETER )
366 /* It is not possible to get here as neither of the two functions called
367 above will ever return. */
369 /*-----------------------------------------------------------*/