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 all the demo application tasks and software timers,
\r
42 * then starts the scheduler. The web documentation provides more details of
\r
43 * the standard demo application tasks, which provide no particular
\r
44 * functionality, but do provide a good example of how to use the FreeRTOS API.
\r
46 * In addition to the standard demo tasks, the following tasks and tests are
\r
47 * defined and/or created within this file:
\r
49 * "Check" timer - The check software timer period is set to three seconds.
\r
50 * The callback function associated with the check software timer checks that
\r
51 * all the standard demo tasks are not only still executing, but are executing
\r
52 * without reporting any errors. If the check software timer discovers that a
\r
53 * task has either stalled, or reported an error, then the error is logged and
\r
54 * the check software timer toggles the red LEDs. If an error has never been
\r
55 * latched, the check software timer toggles the green LEDs. Therefore, if the
\r
56 * system is executing correctly, the green LEDs will toggle every three
\r
57 * seconds, and if an error has ever been detected, the red LEDs will toggle
\r
58 * every three seconds.
\r
60 * "Reg test" tasks - These fill both the core and floating point registers
\r
61 * with known values, then check that each register maintains its expected
\r
62 * value for the lifetime of the tasks. Each task uses a different set of
\r
63 * values. The reg test tasks execute with a very low priority, so get
\r
64 * preempted very frequently. A register containing an unexpected value is
\r
65 * indicative of an error in the context switching mechanism.
\r
67 * "LED" software timer - The callback function associated with the LED
\r
68 * software time maintains a pattern of spinning white LEDs.
\r
70 * See the documentation page for this demo on the FreeRTOS.org web site for
\r
71 * full information, including hardware setup requirements.
\r
74 /* Standard includes. */
\r
77 /* Kernel includes. */
\r
78 #include "FreeRTOS.h"
\r
83 /* Standard demo application includes. */
\r
84 #include "integer.h"
\r
86 #include "semtest.h"
\r
87 #include "dynamic.h"
\r
89 #include "blocktim.h"
\r
90 #include "countsem.h"
\r
91 #include "GenQTest.h"
\r
92 #include "recmutex.h"
\r
94 #include "partest.h"
\r
97 #include "comtest.h"
\r
99 /* Priorities for the demo application tasks. */
\r
100 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
101 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1UL )
\r
102 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2UL )
\r
103 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3UL )
\r
104 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
105 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainFLOP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
108 /* A block time of zero simply means "don't block". */
\r
109 #define mainDONT_BLOCK ( 0UL )
\r
111 /* The period after which the check timer will expire, converted to ticks. */
\r
112 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
114 /* The period after which the LED timer will expire, converted to ticks. */
\r
115 #define mainLED_TIMER_PERIOD_MS ( 75UL / portTICK_PERIOD_MS )
\r
117 /* Constants for the ComTest tasks. */
\r
118 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
\r
119 #define mainCOM_TEST_LED ( 100 )
\r
121 /*-----------------------------------------------------------*/
\r
124 * The check timer callback function, as described at the top of this file.
\r
126 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
129 * The LED timer callback function, as described at the top of this file.
\r
131 static void prvLEDTimerCallback( TimerHandle_t xTimer );
\r
134 * The reg test tasks, as described at the top of this file.
\r
136 extern void vRegTestTask1( void *pvParameters );
\r
137 extern void vRegTestTask2( void *pvParameters );
\r
139 /*-----------------------------------------------------------*/
\r
141 /* Variables that are incremented on each iteration of the reg test tasks -
\r
142 provided the tasks have not reported any errors. The check task inspects these
\r
143 variables to ensure they are still incrementing as expected. If a variable
\r
144 stops incrementing then it is likely that its associate task has stalled. */
\r
145 volatile unsigned long ulRegTest1Counter = 0, ulRegTest2Counter = 0;
\r
147 /*-----------------------------------------------------------*/
\r
149 void main_full( void )
\r
151 TimerHandle_t xTimer = NULL;
\r
153 /* Start all the standard demo/test tasks. These have not particular
\r
154 functionality, but do demonstrate how to use the FreeRTOS API, and test the
\r
156 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
157 vStartDynamicPriorityTasks();
\r
158 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
159 vCreateBlockTimeTasks();
\r
160 vStartCountingSemaphoreTasks();
\r
161 vStartGenericQueueTasks( tskIDLE_PRIORITY );
\r
162 vStartRecursiveMutexTasks();
\r
163 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
164 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
165 vStartMathTasks( mainFLOP_TASK_PRIORITY );
\r
166 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
168 /* Create the register test tasks, as described at the top of this file. */
\r
169 xTaskCreate( vRegTestTask1, "Reg1...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
170 xTaskCreate( vRegTestTask2, "Reg2...", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
173 /* Create the software timer that performs the 'check' functionality,
\r
174 as described at the top of this file. */
\r
175 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
176 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
177 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
178 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
179 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
182 if( xTimer != NULL )
\r
184 xTimerStart( xTimer, mainDONT_BLOCK );
\r
187 /* Create the software timer that performs the 'LED spin' functionality,
\r
188 as described at the top of this file. */
\r
189 xTimer = xTimerCreate( "LEDTimer", /* A text name, purely to help debugging. */
\r
190 ( mainLED_TIMER_PERIOD_MS ),/* The timer period, in this case 75ms. */
\r
191 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
192 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
193 prvLEDTimerCallback /* The callback function that toggles the white LEDs. */
\r
196 if( xTimer != NULL )
\r
198 xTimerStart( xTimer, mainDONT_BLOCK );
\r
201 /* The set of tasks created by the following function call have to be
\r
202 created last as they keep account of the number of tasks they expect to see
\r
204 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
206 /* Start the scheduler. */
\r
207 vTaskStartScheduler();
\r
209 /* If all is well, the scheduler will now be running, and the following line
\r
210 will never be reached. If the following line does execute, then there was
\r
211 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
212 to be created. See the memory management section on the FreeRTOS web site
\r
213 for more details. */
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
220 static long lChangeToRedLEDsAlready = pdFALSE;
\r
221 static unsigned long ulLastRegTest1Counter = 0, ulLastRegTest2Counter = 0;
\r
222 unsigned long ulErrorFound = pdFALSE;
\r
223 /* LEDs are defaulted to use the Green LEDs. The Red LEDs are used if an error
\r
225 static unsigned long ulLED1 = 8, ulLED2 = 11;
\r
226 const unsigned long ulRedLED1 = 6, ulRedLED2 = 9;
\r
228 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
229 they are all still running, and that none have detected an error. */
\r
231 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
233 ulErrorFound = pdTRUE;
\r
236 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
238 ulErrorFound = pdTRUE;
\r
241 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
243 ulErrorFound = pdTRUE;
\r
246 if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
248 ulErrorFound = pdTRUE;
\r
251 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
253 ulErrorFound = pdTRUE;
\r
256 if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
258 ulErrorFound = pdTRUE;
\r
261 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
263 ulErrorFound = pdTRUE;
\r
266 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
268 ulErrorFound = pdTRUE;
\r
271 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
273 ulErrorFound = pdTRUE;
\r
276 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
278 ulErrorFound = pdTRUE;
\r
281 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
283 ulErrorFound = pdTRUE;
\r
286 /* Check the reg test tasks are still cycling. They will stop
\r
287 incrementing their loop counters if they encounter an error. */
\r
288 if( ulRegTest1Counter == ulLastRegTest1Counter )
\r
290 ulErrorFound = pdTRUE;
\r
293 if( ulRegTest2Counter == ulLastRegTest2Counter )
\r
295 ulErrorFound = pdTRUE;
\r
298 ulLastRegTest1Counter = ulRegTest1Counter;
\r
299 ulLastRegTest2Counter = ulRegTest2Counter;
\r
301 /* Toggle the check LEDs to give an indication of the system status. If
\r
302 the green LEDs are toggling, then no errors have been detected. If the red
\r
303 LEDs are toggling, then an error has been reported in at least one task. */
\r
304 vParTestToggleLED( ulLED1 );
\r
305 vParTestToggleLED( ulLED2 );
\r
307 /* Have any errors been latch in ulErrorFound? If so, ensure the gree LEDs
\r
308 are off, then switch to using the red LEDs. */
\r
309 if( ulErrorFound != pdFALSE )
\r
311 if( lChangeToRedLEDsAlready == pdFALSE )
\r
313 lChangeToRedLEDsAlready = pdTRUE;
\r
315 /* An error has been found. Switch to use the red LEDs. */
\r
316 vParTestSetLED( ulLED1, pdFALSE );
\r
317 vParTestSetLED( ulLED2, pdFALSE );
\r
318 ulLED1 = ulRedLED1;
\r
319 ulLED2 = ulRedLED2;
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvLEDTimerCallback( TimerHandle_t xTimer )
\r
327 const unsigned long ulNumWhiteLEDs = 6;
\r
328 static unsigned long ulLit1 = 2, ulLit2 = 1;
\r
330 vParTestSetLED( ulLit2, pdFALSE );
\r
335 if( ulLit1 >= ulNumWhiteLEDs )
\r
340 vParTestSetLED( ulLit1, pdTRUE );
\r
342 /*-----------------------------------------------------------*/
\r