2 * FreeRTOS Kernel V10.0.1
3 * Copyright (C) 2017 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
29 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
30 The processor MUST be in supervisor mode when vTaskStartScheduler is
31 called. The demo applications included in the FreeRTOS.org download switch
32 to supervisor mode prior to main being called. If you are not using one of
33 these demo application projects then ensure Supervisor mode is used.
38 * Creates all the demo application tasks, then starts the scheduler. The WEB
39 * documentation provides more details of the demo application tasks.
41 * Main.c also creates a task called "Check". This only executes every three
42 * seconds but has the highest priority so is guaranteed to get processor time.
43 * Its main function is to check that all the other tasks are still operational.
44 * Each task (other than the "flash" tasks) maintains a unique count that is
45 * incremented each time the task successfully completes its function. Should
46 * any error occur within such a task the count is permanently halted. The
47 * check task inspects the count of each task to ensure it has changed since
48 * the last time the check task executed. If all the count variables have
49 * changed all the tasks are still executing error free, and the check task
50 * toggles the onboard LED. Should any task contain an error at any time
51 * the LED toggle rate will change from 3 seconds to 500ms.
53 * To check the operation of the memory allocator the check task also
54 * dynamically creates a task before delaying, and deletes it again when it
55 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
56 * will fail and an error is signalled. The dynamically created task itself
57 * allocates and frees memory just to give the allocator a bit more exercise.
61 /* Standard includes. */
65 /* Scheduler includes. */
69 /* Demo application includes. */
81 /* Hardware specific definitions. */
85 /*-----------------------------------------------------------*/
87 /* Constants for the ComTest tasks. */
88 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
89 #define mainCOM_TEST_LED ( 5 )
91 /* Priorities for the demo application tasks. */
92 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
93 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
94 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
95 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
96 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
97 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
99 /* The rate at which the on board LED will toggle when there is/is not an
101 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
102 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
103 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 7 )
105 /* Constants used by the vMemCheckTask() task. */
106 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
107 #define mainNO_TASK ( 0 )
109 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
110 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
111 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
112 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
114 #define MAX_WAIT_STATES 8
115 static const unsigned long ululCSRWaitValues[ MAX_WAIT_STATES + 1 ] =
117 WaitState1,/* There is no "zero wait state" value, so use one wait state */
127 /*-----------------------------------------------------------*/
130 * Checks that all the demo application tasks are still executing without error
131 * - as described at the top of the file.
133 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
136 * The task that executes at the highest priority and calls
137 * prvCheckOtherTasksAreStillRunning(). See the description at the top
140 static void vErrorChecks( void *pvParameters );
143 * Dynamically created and deleted during each cycle of the vErrorChecks()
144 * task. This is done to check the operation of the memory allocator.
145 * See the top of vErrorChecks for more details.
147 static void vMemCheckTask( void *pvParameters );
150 * Configure the processor for use with the Olimex demo board. This includes
151 * setup for the I/O, system clock, and access timings.
153 static void prvSetupHardware( void );
155 /*-----------------------------------------------------------*/
158 * Starts all the other tasks, then starts the scheduler.
162 /* Setup the hardware for use with the Olimex demo board. */
165 /* Start the demo/test application tasks. */
166 vStartIntegerMathTasks( tskIDLE_PRIORITY );
167 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
168 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
169 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
170 vStartMathTasks( tskIDLE_PRIORITY );
171 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
172 vStartDynamicPriorityTasks();
173 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
175 /* Start the check task - which is defined in this file. */
176 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
178 /* Now all the tasks have been started - start the scheduler.
180 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
181 The processor MUST be in supervisor mode when vTaskStartScheduler is
182 called. The demo applications included in the FreeRTOS.org download switch
183 to supervisor mode prior to main being called. If you are not using one of
184 these demo application projects then ensure Supervisor mode is used here. */
185 vTaskStartScheduler();
187 /* Should never reach here! */
190 /*-----------------------------------------------------------*/
192 static void vErrorChecks( void *pvParameters )
194 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
195 unsigned long ulMemCheckTaskRunningCount;
196 TaskHandle_t xCreatedTask;
198 /* Just to stop compiler warnings. */
199 ( void ) pvParameters;
201 /* Cycle for ever, delaying then checking all the other tasks are still
202 operating without error. If an error is detected then the delay period
203 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
204 the on board LED flash rate will increase.
206 In addition to the standard tests the memory allocator is tested through
207 the dynamic creation and deletion of a task each cycle. Each time the
208 task is created memory must be allocated for its stack. When the task is
209 deleted this memory is returned to the heap. If the task cannot be created
210 then it is likely that the memory allocation failed. */
214 /* Reset xCreatedTask. This is modified by the task about to be
215 created so we can tell if it is executing correctly or not. */
216 xCreatedTask = mainNO_TASK;
218 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
220 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
221 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
223 /* Could not create the task - we have probably run out of heap. */
224 xDelayPeriod = mainERROR_FLASH_PERIOD;
227 /* Delay until it is time to execute again. */
228 vTaskDelay( xDelayPeriod );
230 /* Delete the dynamically created task. */
231 if( xCreatedTask != mainNO_TASK )
233 vTaskDelete( xCreatedTask );
236 /* Check all the standard demo application tasks are executing without
237 error. ulMemCheckTaskRunningCount is checked to ensure it was
238 modified by the task just deleted. */
239 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
241 /* An error has been detected in one of the tasks - flash faster. */
242 xDelayPeriod = mainERROR_FLASH_PERIOD;
245 /* The toggle rate of the LED depends on how long this task delays for.
246 An error reduces the delay period and so increases the toggle rate. */
247 vParTestToggleLED( mainON_BOARD_LED_BIT );
250 /*-----------------------------------------------------------*/
252 static void prvSetupHardware( void )
258 portFLOAT nsecsPerClockTick;
260 unsigned long ulCSRWaitValue;
262 /* We are compiling to run from ROM (either on-chip or off-chip flash).
263 Leave the RAM/flash mapped the way they are on reset
264 (flash @ 0x00000000, RAM @ 0x00300000), and set up the
265 proper flash wait states (starts out at the maximum number
266 of wait states on reset, so we should be able to reduce it).
267 Most of this code will probably get removed by the compiler
268 if optimization is enabled, since these calculations are
269 based on constants. But the compiler should still produce
270 a correct wait state register value. */
271 nsecsPerClockTick = ( portFLOAT ) 1000000000 / configCPU_CLOCK_HZ;
272 lNumWaitStates = ( long )( ( configFLASH_SPEED_NSEC / nsecsPerClockTick ) + 0.5 ) - 1;
274 if( lNumWaitStates < 0 )
279 if( lNumWaitStates > MAX_WAIT_STATES )
281 lNumWaitStates = MAX_WAIT_STATES;
284 ulCSRWaitValue = ululCSRWaitValues[ lNumWaitStates ];
285 ulCSRWaitValue = WaitState5;
287 AT91C_BASE_EBI->EBI_CSR[ 0 ] = ulCSRWaitValue | DataBus16 | WaitStateEnable
288 | PageSize1M | tDF_0cycle
289 | ByteWriteAccessType | CSEnable
290 | 0x00000000 /* Base Address */;
292 #else /* else we are compiling to run from on-chip RAM */
294 /* If compiling to run from RAM, we expect the on-chip RAM to already
295 be mapped at 0x00000000. This is typically done with an initialization
296 script for the JTAG emulator you are using to download and run the
297 demo application. So there is nothing to do here in this case. */
301 /* Disable all interrupts at the AIC level initially... */
302 AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF;
304 /* Set all SVR and SMR entries to default values (start with a clean slate)... */
305 for( lCount = 0; lCount < 32; lCount++ )
307 AT91C_BASE_AIC->AIC_SVR[ lCount ] = (unsigned long) 0;
308 AT91C_BASE_AIC->AIC_SMR[ lCount ] = AIC_SRCTYPE_INT_EDGE_TRIGGERED;
311 /* Disable clocks to all peripherals initially... */
312 AT91C_BASE_PS->PS_PCDR = 0xFFFFFFFF;
314 /* Clear all interrupts at the AIC level initially... */
315 AT91C_BASE_AIC->AIC_ICCR = 0xFFFFFFFF;
317 /* Perform 8 "End Of Interrupt" cmds to make sure AIC will not Lock out
319 for( lCount = 0; lCount < 8; lCount++ )
321 AT91C_BASE_AIC->AIC_EOICR = 0;
324 /* Initialise LED outputs. */
325 vParTestInitialise();
327 /*-----------------------------------------------------------*/
329 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
331 long lReturn = ( long ) pdPASS;
333 /* Check all the demo tasks (other than the flash tasks) to ensure
334 that they are all still running, and that none of them have detected
337 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
339 lReturn = ( long ) pdFAIL;
342 if( xAreComTestTasksStillRunning() != pdTRUE )
344 lReturn = ( long ) pdFAIL;
347 if( xArePollingQueuesStillRunning() != pdTRUE )
349 lReturn = ( long ) pdFAIL;
352 if( xAreMathsTaskStillRunning() != pdTRUE )
354 lReturn = ( long ) pdFAIL;
357 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
359 lReturn = ( long ) pdFAIL;
362 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
364 lReturn = ( long ) pdFAIL;
367 if( xAreBlockingQueuesStillRunning() != pdTRUE )
369 lReturn = ( long ) pdFAIL;
372 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
374 /* The vMemCheckTask did not increment the counter - it must
376 lReturn = ( long ) pdFAIL;
381 /*-----------------------------------------------------------*/
383 static void vMemCheckTask( void *pvParameters )
385 unsigned long *pulMemCheckTaskRunningCounter;
386 void *pvMem1, *pvMem2, *pvMem3;
387 static long lErrorOccurred = pdFALSE;
389 /* This task is dynamically created then deleted during each cycle of the
390 vErrorChecks task to check the operation of the memory allocator. Each time
391 the task is created memory is allocated for the stack and TCB. Each time
392 the task is deleted this memory is returned to the heap. This task itself
393 exercises the allocator by allocating and freeing blocks.
395 The task executes at the idle priority so does not require a delay.
397 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
398 vErrorChecks() task that this task is still executing without error. */
400 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
404 if( lErrorOccurred == pdFALSE )
406 /* We have never seen an error so increment the counter. */
407 ( *pulMemCheckTaskRunningCounter )++;
411 /* There has been an error so reset the counter so the check task
412 can tell that an error occurred. */
413 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
416 /* Allocate some memory - just to give the allocator some extra
417 exercise. This has to be in a critical section to ensure the
418 task does not get deleted while it has memory allocated. */
421 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
424 lErrorOccurred = pdTRUE;
428 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
434 /* Again - with a different size block. */
437 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
440 lErrorOccurred = pdTRUE;
444 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
450 /* Again - with a different size block. */
453 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
456 lErrorOccurred = pdTRUE;
460 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );