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
30 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
31 * documentation provides more details of the demo application tasks.
\r
33 * Main.c also creates a task called "Check". This only executes every three
\r
34 * seconds but has the highest priority so is guaranteed to get processor time.
\r
35 * Its main function is to check that all the other tasks are still operational.
\r
36 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
37 * incremented each time the task successfully completes its function. Should
\r
38 * any error occur within such a task the count is permanently halted. The
\r
39 * check task inspects the count of each task to ensure it has changed since
\r
40 * the last time the check task executed. If all the count variables have
\r
41 * changed all the tasks are still executing error free, and the check task
\r
42 * toggles the onboard LED. Should any task contain an error at any time
\r
43 * the LED toggle rate will change from 3 seconds to 500ms.
\r
45 * To check the operation of the memory allocator the check task also
\r
46 * dynamically creates a task before delaying, and deletes it again when it
\r
47 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
48 * will fail and an error is signalled. The dynamically created task itself
\r
49 * allocates and frees memory just to give the allocator a bit more exercise.
\r
53 /* Standard includes. */
\r
57 /* Scheduler include files. */
\r
58 #include "FreeRTOS.h"
\r
61 /* Demo application file headers. */
\r
63 #include "integer.h"
\r
65 #include "comtest2.h"
\r
66 #include "semtest.h"
\r
68 #include "dynamic.h"
\r
71 #include "partest.h"
\r
73 /* Priority definitions for most of the tasks in the demo application. Some
\r
74 tasks just use the idle priority. */
\r
75 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
76 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
77 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
78 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
79 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
80 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
82 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
83 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
85 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
86 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
87 #define mainCOM_TEST_LED ( 3 )
\r
89 /* LED that is toggled by the check task. The check task periodically checks
\r
90 that all the other tasks are operating without error. If no errors are found
\r
91 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
92 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
93 #define mainCHECK_TASK_LED ( 5 )
\r
94 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
95 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
97 /* Constants used by the vMemCheckTask() task. */
\r
98 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
99 #define mainNO_TASK ( 0 )
\r
101 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
102 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
103 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
104 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
107 * The 'Check' task.
\r
109 static void vErrorChecks( void *pvParameters );
\r
112 * Checks the unique counts of other tasks to ensure they are still operational.
\r
114 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
117 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
118 * task. This is done to check the operation of the memory allocator.
\r
119 * See the top of vErrorChecks for more details.
\r
121 static void vMemCheckTask( void *pvParameters );
\r
123 /*-----------------------------------------------------------*/
\r
126 * Start all the tasks then start the scheduler.
\r
130 /* Setup the LED's for output. */
\r
131 vParTestInitialise();
\r
133 /* Start the various standard demo application tasks. */
\r
134 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
135 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
136 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
137 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
138 vStartMathTasks( tskIDLE_PRIORITY );
\r
139 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
140 vStartDynamicPriorityTasks();
\r
141 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
143 /* Start the 'Check' task. */
\r
144 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
146 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
147 as 1 in portmacro.h. To use the cooperative scheduler define
\r
148 configUSE_PREEMPTION as 0. */
\r
149 vTaskStartScheduler();
\r
151 /* Should never get here! */
\r
154 /*-----------------------------------------------------------*/
\r
157 * Cycle for ever, delaying then checking all the other tasks are still
\r
158 * operating without error. If an error is detected then the delay period
\r
159 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
160 * the on board LED flash rate will increase.
\r
162 * In addition to the standard tests the memory allocator is tested through
\r
163 * the dynamic creation and deletion of a task each cycle. Each time the
\r
164 * task is created memory must be allocated for its stack. When the task is
\r
165 * deleted this memory is returned to the heap. If the task cannot be created
\r
166 * then it is likely that the memory allocation failed. In addition the
\r
167 * dynamically created task allocates and frees memory while it runs.
\r
169 static void vErrorChecks( void *pvParameters )
\r
171 TickType_t xDelayPeriod = mainCHECK_PERIOD;
\r
172 volatile unsigned long ulMemCheckTaskRunningCount;
\r
173 TaskHandle_t xCreatedTask;
\r
174 TickType_t xLastWakeTime;
\r
176 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
177 functions correctly. */
\r
178 xLastWakeTime = xTaskGetTickCount();
\r
182 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
183 later that it has changed. */
\r
184 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
186 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
188 xCreatedTask = mainNO_TASK;
\r
189 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
191 /* Could not create the task - we have probably run out of heap. */
\r
192 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
196 /* Delay until it is time to execute again. The delay period is
\r
197 shorter following an error. */
\r
198 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
201 /* Delete the dynamically created task. */
\r
202 if( xCreatedTask != mainNO_TASK )
\r
204 vTaskDelete( xCreatedTask );
\r
207 /* Check all the standard demo application tasks are executing without
\r
208 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
209 modified by the task just deleted. */
\r
210 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
212 /* An error has been detected in one of the tasks - flash faster. */
\r
213 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
216 vParTestToggleLED( mainCHECK_TASK_LED );
\r
219 /*-----------------------------------------------------------*/
\r
222 * Check each set of tasks in turn to see if they have experienced any
\r
223 * error conditions.
\r
225 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
227 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
229 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
231 lNoErrorsDiscovered = pdFALSE;
\r
234 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
236 lNoErrorsDiscovered = pdFALSE;
\r
239 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
241 lNoErrorsDiscovered = pdFALSE;
\r
244 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
246 lNoErrorsDiscovered = pdFALSE;
\r
249 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
251 lNoErrorsDiscovered = pdFALSE;
\r
254 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
256 lNoErrorsDiscovered = pdFALSE;
\r
259 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
261 lNoErrorsDiscovered = pdFALSE;
\r
264 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
266 /* The vMemCheckTask task did not increment the counter - it must
\r
268 lNoErrorsDiscovered = pdFALSE;
\r
271 return lNoErrorsDiscovered;
\r
273 /*-----------------------------------------------------------*/
\r
275 static void vMemCheckTask( void *pvParameters )
\r
277 unsigned long *pulMemCheckTaskRunningCounter;
\r
278 void *pvMem1, *pvMem2, *pvMem3;
\r
279 static long lErrorOccurred = pdFALSE;
\r
281 /* This task is dynamically created then deleted during each cycle of the
\r
282 vErrorChecks task to check the operation of the memory allocator. Each time
\r
283 the task is created memory is allocated for the stack and TCB. Each time
\r
284 the task is deleted this memory is returned to the heap. This task itself
\r
285 exercises the allocator by allocating and freeing blocks.
\r
287 The task executes at the idle priority so does not require a delay.
\r
289 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
290 vErrorChecks() task that this task is still executing without error. */
\r
292 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
296 if( lErrorOccurred == pdFALSE )
\r
298 /* We have never seen an error so increment the counter. */
\r
299 ( *pulMemCheckTaskRunningCounter )++;
\r
303 /* Reset the count so an error is detected by the
\r
304 prvCheckOtherTasksAreStillRunning() function. */
\r
305 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
308 /* Allocate some memory - just to give the allocator some extra
\r
309 exercise. This has to be in a critical section to ensure the
\r
310 task does not get deleted while it has memory allocated. */
\r
313 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
314 if( pvMem1 == NULL )
\r
316 lErrorOccurred = pdTRUE;
\r
320 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
321 vPortFree( pvMem1 );
\r
326 /* Again - with a different size block. */
\r
329 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
330 if( pvMem2 == NULL )
\r
332 lErrorOccurred = pdTRUE;
\r
336 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
337 vPortFree( pvMem2 );
\r
342 /* Again - with a different size block. */
\r
345 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
346 if( pvMem3 == NULL )
\r
348 lErrorOccurred = pdTRUE;
\r
352 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
353 vPortFree( pvMem3 );
\r
359 /*-----------------------------------------------------------*/
\r
362 * Called by the startup code. Initial processor setup can be placed in this
\r
365 void hw_initialise (void)
\r