2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /******************************************************************************
\r
71 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
72 * project, and a more comprehensive test and demo application. The
\r
73 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
74 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
75 * in main.c. This file implements the comprehensive test and demo version.
\r
77 * NOTE 2: This file only contains the source code that is specific to the
\r
78 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
79 * required to configure the hardware, are defined in main.c.
\r
80 ******************************************************************************
\r
82 * main_full() creates a set of standard demo tasks, some application specific
\r
83 * tasks, and four timers. It then starts the scheduler. The web documentation
\r
84 * provides more details of the standard demo application tasks, which provide
\r
85 * no particular functionality, but do provide a good example of how to use the
\r
88 * In addition to the standard demo tasks, the following tasks and timer are
\r
89 * defined and/or created within this file:
\r
91 * "Reg test" tasks - These fill the registers with known values, then check
\r
92 * that each register maintains its expected value for the lifetime of the
\r
93 * task. Each task uses a different set of values. The reg test tasks execute
\r
94 * with a very low priority, so get preempted very frequently. A register
\r
95 * containing an unexpected value is indicative of an error in the context
\r
96 * switching mechanism.
\r
98 * "Interrupt semaphore take" task - This task does nothing but block on a
\r
99 * semaphore that is 'given' from the tick hook function (which is defined in
\r
100 * main.c). It toggles the fourth LED each time it receives the semaphore. The
\r
101 * Semahore is given every 50ms, so LED 4 toggles every 50ms.
\r
103 * "Flash timers" - A software timer callback function is defined that does
\r
104 * nothing but toggle an LED. Three software timers are created that each
\r
105 * use the same callback function, but each toggles a different LED at a
\r
106 * different frequency. The timers control the first three LEDs.
\r
108 * "Check" software timer - The check timer period is initially set to three
\r
109 * seconds. Its callback function checks that all the standard demo tasks, and
\r
110 * the register check tasks, are not only still executing, but are executing
\r
111 * without reporting any errors. If the check timer callback discovers that a
\r
112 * task has either stalled, or reported an error, then it changes the period of
\r
113 * the check timer from the initial three seconds, to just 200ms. The callback
\r
114 * function also toggles the fifth LED each time it is called. This provides a
\r
115 * visual indication of the system status: If the LED toggles every three
\r
116 * seconds then no issues have been discovered. If the LED toggles every 200ms,
\r
117 * then an issue has been discovered with at least one task.
\r
120 /* Kernel includes. */
\r
121 #include "FreeRTOS.h"
\r
124 #include "semphr.h"
\r
125 #include "timers.h"
\r
127 /* Common demo includes. */
\r
128 #include "blocktim.h"
\r
129 #include "countsem.h"
\r
130 #include "recmutex.h"
\r
131 #include "ParTest.h"
\r
132 #include "dynamic.h"
\r
133 #include "QueueOverwrite.h"
\r
134 #include "QueueSet.h"
\r
136 /* The period after which the check timer will expire provided no errors have
\r
137 been reported by any of the standard demo tasks. ms are converted to the
\r
138 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
139 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
141 /* The period at which the check timer will expire if an error has been
\r
142 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
143 in ticks using the portTICK_PERIOD_MS constant. */
\r
144 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
146 /* A block time of zero simply means "don't block". */
\r
147 #define mainDONT_BLOCK ( 0UL )
\r
149 /* The base toggle rate used by the flash timers. Each toggle rate is a
\r
150 multiple of this. */
\r
151 #define mainFLASH_TIMER_BASE_RATE ( 200UL / portTICK_PERIOD_MS )
\r
153 /* The LED toggle by the check timer. */
\r
154 #define mainCHECK_LED ( 4 )
\r
156 /* The LED toggled each time the task implemented by the prvSemaphoreTakeTask()
\r
157 function takes the semaphore that is given by the tick hook function. */
\r
158 #define mainSEMAPHORE_LED ( 3 )
\r
160 /*-----------------------------------------------------------*/
\r
163 * Register check tasks, as described at the top of this file. The nature of
\r
164 * these files necessitates that they are written in an assembly.
\r
166 extern void vRegTest1Task( void *pvParameters );
\r
167 extern void vRegTest2Task( void *pvParameters );
\r
170 * The hardware only has a single LED. Simply toggle it.
\r
172 extern void vMainToggleLED( void );
\r
175 * The check timer callback function, as described at the top of this file.
\r
177 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
180 * The flash timer callback function, as described at the top of this file.
\r
181 * This callback function is assigned to three separate software timers.
\r
183 static void prvFlashTimerCallback( TimerHandle_t xTimer );
\r
186 * The task that toggles an LED each time the semaphore 'given' by the tick
\r
187 * hook function (which is defined in main.c) is 'taken' in the task.
\r
189 static void prvSemaphoreTakeTask( void *pvParameters );
\r
192 * Called by main() to create the comprehensive test/demo application if
\r
193 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.
\r
195 void main_full( void );
\r
197 /*-----------------------------------------------------------*/
\r
199 /* The following two variables are used to communicate the status of the
\r
200 register check tasks to the check software timer. If the variables keep
\r
201 incrementing, then the register check tasks have not discovered any errors. If
\r
202 a variable stops incrementing, then an error has been found. */
\r
203 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;
\r
205 /* The semaphore that is given by the tick hook function (defined in main.c)
\r
206 and taken by the task implemented by the prvSemaphoreTakeTask() function. The
\r
207 task toggles LED mainSEMAPHORE_LED each time the semaphore is taken. */
\r
208 SemaphoreHandle_t xLEDSemaphore = NULL;
\r
209 /*-----------------------------------------------------------*/
\r
211 void main_full( void )
\r
213 TimerHandle_t xTimer = NULL;
\r
214 unsigned long ulTimer;
\r
215 const unsigned long ulTimersToCreate = 3L;
\r
216 /* The register test tasks are asm functions that don't use a stack. The
\r
217 stack allocated just has to be large enough to hold the task context, and
\r
218 for the additional required for the stack overflow checking to work (if
\r
220 const size_t xRegTestStackSize = 25U;
\r
222 /* Create the standard demo tasks */
\r
223 vCreateBlockTimeTasks();
\r
224 vStartDynamicPriorityTasks();
\r
225 vStartCountingSemaphoreTasks();
\r
226 vStartRecursiveMutexTasks();
\r
227 vStartQueueOverwriteTask( tskIDLE_PRIORITY );
\r
228 vStartQueueSetTasks();
\r
230 /* Create that is given from the tick hook function, and the task that
\r
231 toggles an LED each time the semaphore is given. */
\r
232 vSemaphoreCreateBinary( xLEDSemaphore );
\r
233 xTaskCreate( prvSemaphoreTakeTask, /* Function that implements the task. */
\r
234 "Sem", /* Text name of the task. */
\r
235 configMINIMAL_STACK_SIZE, /* Stack allocated to the task (in words). */
\r
236 NULL, /* The task parameter is not used. */
\r
237 configMAX_PRIORITIES - 2, /* The priority of the task. */
\r
238 NULL ); /* Don't receive a handle back, it is not needed. */
\r
240 /* Create the register test tasks as described at the top of this file.
\r
241 These are naked functions that don't use any stack. A stack still has
\r
242 to be allocated to hold the task context. */
\r
243 xTaskCreate( vRegTest1Task, /* Function that implements the task. */
\r
244 "Reg1", /* Text name of the task. */
\r
245 xRegTestStackSize, /* Stack allocated to the task. */
\r
246 NULL, /* The task parameter is not used. */
\r
247 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
248 NULL ); /* Don't receive a handle back, it is not needed. */
\r
250 xTaskCreate( vRegTest2Task, /* Function that implements the task. */
\r
251 "Reg2", /* Text name of the task. */
\r
252 xRegTestStackSize, /* Stack allocated to the task. */
\r
253 NULL, /* The task parameter is not used. */
\r
254 tskIDLE_PRIORITY, /* The priority to assign to the task. */
\r
255 NULL ); /* Don't receive a handle back, it is not needed. */
\r
257 /* Create the three flash timers. */
\r
258 for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )
\r
260 xTimer = xTimerCreate( "FlashTimer", /* A text name, purely to help debugging. */
\r
261 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ), /* The timer period, in this case 3000ms (3s). */
\r
262 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
263 ( void * ) ulTimer, /* The ID is used to hold the number of the LED that will be flashed. */
\r
264 prvFlashTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
267 if( xTimer != NULL )
\r
269 xTimerStart( xTimer, mainDONT_BLOCK );
\r
273 /* Create the software timer that performs the 'check' functionality,
\r
274 as described at the top of this file. */
\r
275 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
276 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
277 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
278 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
279 prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
\r
282 /* If the software timer was created successfully, start it. It won't
\r
283 actually start running until the scheduler starts. A block time of
\r
284 zero is used in this call, although any value could be used as the block
\r
285 time will be ignored because the scheduler has not started yet. */
\r
286 if( xTimer != NULL )
\r
288 xTimerStart( xTimer, mainDONT_BLOCK );
\r
291 /* Start the kernel. From here on, only tasks and interrupts will run. */
\r
292 vTaskStartScheduler();
\r
294 /* If all is well, the scheduler will now be running, and the following
\r
295 line will never be reached. If the following line does execute, then there
\r
296 was insufficient FreeRTOS heap memory available for the idle and/or timer
\r
297 tasks to be created. See the memory management section on the FreeRTOS web
\r
298 site, or the FreeRTOS tutorial books for more details. */
\r
301 /*-----------------------------------------------------------*/
\r
303 /* See the description at the top of this file. */
\r
304 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
306 static long lChangedTimerPeriodAlready = pdFALSE;
\r
307 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;
\r
308 unsigned long ulErrorFound = pdFALSE;
\r
310 /* Check all the demo and test tasks to ensure that they are all still
\r
311 running, and that none have detected an error. */
\r
312 if( xAreDynamicPriorityTasksStillRunning() != pdPASS )
\r
314 ulErrorFound |= ( 0x01UL << 0UL );
\r
317 if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
\r
319 ulErrorFound |= ( 0x01UL << 1UL );
\r
322 if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )
\r
324 ulErrorFound |= ( 0x01UL << 2UL );
\r
327 if( xAreRecursiveMutexTasksStillRunning() != pdPASS )
\r
329 ulErrorFound |= ( 0x01UL << 3UL );
\r
332 /* Check that the register test 1 task is still running. */
\r
333 if( ulLastRegTest1Value == ulRegTest1LoopCounter )
\r
335 ulErrorFound |= ( 0x01UL << 4UL );
\r
337 ulLastRegTest1Value = ulRegTest1LoopCounter;
\r
339 /* Check that the register test 2 task is still running. */
\r
340 if( ulLastRegTest2Value == ulRegTest2LoopCounter )
\r
342 ulErrorFound |= ( 0x01UL << 5UL );
\r
344 ulLastRegTest2Value = ulRegTest2LoopCounter;
\r
346 if( xAreQueueSetTasksStillRunning() != pdPASS )
\r
348 ulErrorFound |= ( 0x01UL << 6UL );
\r
351 if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
\r
353 ulErrorFound |= ( 0x01UL << 7UL );
\r
356 /* Toggle the check LED to give an indication of the system status. If
\r
357 the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
\r
358 everything is ok. A faster toggle indicates an error. */
\r
359 vParTestToggleLED( mainCHECK_LED );
\r
361 /* Have any errors been latched in ulErrorFound? If so, shorten the
\r
362 period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
\r
363 This will result in an increase in the rate at which mainCHECK_LED
\r
365 if( ulErrorFound != pdFALSE )
\r
367 if( lChangedTimerPeriodAlready == pdFALSE )
\r
369 lChangedTimerPeriodAlready = pdTRUE;
\r
371 /* This call to xTimerChangePeriod() uses a zero block time.
\r
372 Functions called from inside of a timer callback function must
\r
373 *never* attempt to block. */
\r
374 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvSemaphoreTakeTask( void *pvParameters )
\r
382 configASSERT( xLEDSemaphore );
\r
386 /* Wait to obtain the semaphore - which is given by the tick hook
\r
387 function every 50ms. */
\r
388 xSemaphoreTake( xLEDSemaphore, portMAX_DELAY );
\r
389 vParTestToggleLED( mainSEMAPHORE_LED );
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvFlashTimerCallback( TimerHandle_t xTimer )
\r
396 unsigned long ulLED;
\r
398 /* This callback function is assigned to three separate software timers.
\r
399 Each timer toggles a different LED. Obtain the number of the LED that
\r
400 this timer is toggling. */
\r
401 ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );
\r
403 /* Toggle the LED. */
\r
404 vParTestToggleLED( ulLED );
\r