2 FreeRTOS V8.2.1 - Copyright (C) 2015 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
71 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
72 * documentation provides more details of the demo application tasks.
\r
74 * Main.c also creates a task called "Check". This only executes every three
\r
75 * seconds but has the highest priority so is guaranteed to get processor time.
\r
76 * Its main function is to check that all the other tasks are still operational.
\r
77 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
78 * incremented each time the task successfully completes its function. Should
\r
79 * any error occur within such a task the count is permanently halted. The
\r
80 * check task inspects the count of each task to ensure it has changed since
\r
81 * the last time the check task executed. If all the count variables have
\r
82 * changed all the tasks are still executing error free, and the check task
\r
83 * toggles the onboard LED. Should any task contain an error at any time
\r
84 * the LED toggle rate will change from 3 seconds to 500ms.
\r
86 * To check the operation of the memory allocator the check task also
\r
87 * dynamically creates a task before delaying, and deletes it again when it
\r
88 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
89 * will fail and an error is signalled. The dynamically created task itself
\r
90 * allocates and frees memory just to give the allocator a bit more exercise.
\r
94 /* Standard includes. */
\r
98 /* Scheduler include files. */
\r
99 #include "FreeRTOS.h"
\r
102 /* Demo application file headers. */
\r
104 #include "integer.h"
\r
106 #include "comtest2.h"
\r
107 #include "semtest.h"
\r
109 #include "dynamic.h"
\r
110 #include "BlockQ.h"
\r
111 #include "serial.h"
\r
112 #include "partest.h"
\r
114 /* Priority definitions for most of the tasks in the demo application. Some
\r
115 tasks just use the idle priority. */
\r
116 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
120 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
124 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
126 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
127 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
128 #define mainCOM_TEST_LED ( 3 )
\r
130 /* LED that is toggled by the check task. The check task periodically checks
\r
131 that all the other tasks are operating without error. If no errors are found
\r
132 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
133 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
134 #define mainCHECK_TASK_LED ( 5 )
\r
135 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
136 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
138 /* Constants used by the vMemCheckTask() task. */
\r
139 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
140 #define mainNO_TASK ( 0 )
\r
142 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
143 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
144 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
145 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
148 * The 'Check' task.
\r
150 static void vErrorChecks( void *pvParameters );
\r
153 * Checks the unique counts of other tasks to ensure they are still operational.
\r
155 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
158 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
159 * task. This is done to check the operation of the memory allocator.
\r
160 * See the top of vErrorChecks for more details.
\r
162 static void vMemCheckTask( void *pvParameters );
\r
164 /*-----------------------------------------------------------*/
\r
167 * Start all the tasks then start the scheduler.
\r
171 /* Setup the LED's for output. */
\r
172 vParTestInitialise();
\r
174 /* Start the various standard demo application tasks. */
\r
175 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
176 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
177 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
178 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
179 vStartMathTasks( tskIDLE_PRIORITY );
\r
180 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
181 vStartDynamicPriorityTasks();
\r
182 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
184 /* Start the 'Check' task. */
\r
185 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
187 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
188 as 1 in portmacro.h. To use the cooperative scheduler define
\r
189 configUSE_PREEMPTION as 0. */
\r
190 vTaskStartScheduler();
\r
192 /* Should never get here! */
\r
195 /*-----------------------------------------------------------*/
\r
198 * Cycle for ever, delaying then checking all the other tasks are still
\r
199 * operating without error. If an error is detected then the delay period
\r
200 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
201 * the on board LED flash rate will increase.
\r
203 * In addition to the standard tests the memory allocator is tested through
\r
204 * the dynamic creation and deletion of a task each cycle. Each time the
\r
205 * task is created memory must be allocated for its stack. When the task is
\r
206 * deleted this memory is returned to the heap. If the task cannot be created
\r
207 * then it is likely that the memory allocation failed. In addition the
\r
208 * dynamically created task allocates and frees memory while it runs.
\r
210 static void vErrorChecks( void *pvParameters )
\r
212 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
213 volatile unsigned long ulMemCheckTaskRunningCount;
\r
214 TaskHandle_t xCreatedTask;
\r
215 TickType_t xLastWakeTime;
\r
217 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
218 functions correctly. */
\r
219 xLastWakeTime = xTaskGetTickCount();
\r
223 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
224 later that it has changed. */
\r
225 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
227 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
229 xCreatedTask = mainNO_TASK;
\r
230 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
232 /* Could not create the task - we have probably run out of heap. */
\r
233 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
237 /* Delay until it is time to execute again. The delay period is
\r
238 shorter following an error. */
\r
239 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
242 /* Delete the dynamically created task. */
\r
243 if( xCreatedTask != mainNO_TASK )
\r
245 vTaskDelete( xCreatedTask );
\r
248 /* Check all the standard demo application tasks are executing without
\r
249 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
250 modified by the task just deleted. */
\r
251 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
253 /* An error has been detected in one of the tasks - flash faster. */
\r
254 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
257 vParTestToggleLED( mainCHECK_TASK_LED );
\r
260 /*-----------------------------------------------------------*/
\r
263 * Check each set of tasks in turn to see if they have experienced any
\r
264 * error conditions.
\r
266 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
268 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
270 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
272 lNoErrorsDiscovered = pdFALSE;
\r
275 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
277 lNoErrorsDiscovered = pdFALSE;
\r
280 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
282 lNoErrorsDiscovered = pdFALSE;
\r
285 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
287 lNoErrorsDiscovered = pdFALSE;
\r
290 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
292 lNoErrorsDiscovered = pdFALSE;
\r
295 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
297 lNoErrorsDiscovered = pdFALSE;
\r
300 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
302 lNoErrorsDiscovered = pdFALSE;
\r
305 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
307 /* The vMemCheckTask task did not increment the counter - it must
\r
309 lNoErrorsDiscovered = pdFALSE;
\r
312 return lNoErrorsDiscovered;
\r
314 /*-----------------------------------------------------------*/
\r
316 static void vMemCheckTask( void *pvParameters )
\r
318 unsigned long *pulMemCheckTaskRunningCounter;
\r
319 void *pvMem1, *pvMem2, *pvMem3;
\r
320 static long lErrorOccurred = pdFALSE;
\r
322 /* This task is dynamically created then deleted during each cycle of the
\r
323 vErrorChecks task to check the operation of the memory allocator. Each time
\r
324 the task is created memory is allocated for the stack and TCB. Each time
\r
325 the task is deleted this memory is returned to the heap. This task itself
\r
326 exercises the allocator by allocating and freeing blocks.
\r
328 The task executes at the idle priority so does not require a delay.
\r
330 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
331 vErrorChecks() task that this task is still executing without error. */
\r
333 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
337 if( lErrorOccurred == pdFALSE )
\r
339 /* We have never seen an error so increment the counter. */
\r
340 ( *pulMemCheckTaskRunningCounter )++;
\r
344 /* Reset the count so an error is detected by the
\r
345 prvCheckOtherTasksAreStillRunning() function. */
\r
346 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
349 /* Allocate some memory - just to give the allocator some extra
\r
350 exercise. This has to be in a critical section to ensure the
\r
351 task does not get deleted while it has memory allocated. */
\r
354 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
355 if( pvMem1 == NULL )
\r
357 lErrorOccurred = pdTRUE;
\r
361 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
362 vPortFree( pvMem1 );
\r
367 /* Again - with a different size block. */
\r
370 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
371 if( pvMem2 == NULL )
\r
373 lErrorOccurred = pdTRUE;
\r
377 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
378 vPortFree( pvMem2 );
\r
383 /* Again - with a different size block. */
\r
386 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
387 if( pvMem3 == NULL )
\r
389 lErrorOccurred = pdTRUE;
\r
393 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
394 vPortFree( pvMem3 );
\r
400 /*-----------------------------------------------------------*/
\r
403 * Called by the startup code. Initial processor setup can be placed in this
\r
406 void hw_initialise (void)
\r