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