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.
64 + The vErrorChecks() task now dynamically creates then deletes a task each
65 cycle. This tests the operation of the memory allocator.
69 + vParTestInitialise() is called during initialisation to ensure all the
74 /* Standard includes. */
78 /* Scheduler includes. */
82 /* Demo application includes. */
94 /*-----------------------------------------------------------*/
96 /* Constants to setup I/O. */
97 #define mainTX_ENABLE ( ( unsigned long ) 0x0001 )
98 #define mainRX_ENABLE ( ( unsigned long ) 0x0004 )
99 #define mainP0_14 ( ( unsigned long ) 0x4000 )
100 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
102 /* Constants to setup the PLL. */
103 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
104 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
105 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
106 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
107 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
108 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
109 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
111 /* Constants to setup the MAM. */
112 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
113 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
115 /* Constants to setup the peripheral bus. */
116 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
118 /* Constants for the ComTest tasks. */
119 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
120 #define mainCOM_TEST_LED ( 3 )
122 /* Priorities for the demo application tasks. */
123 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
124 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
125 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 0 )
126 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
127 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 0 )
128 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
130 /* The rate at which the on board LED will toggle when there is/is not an
132 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
133 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
134 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
136 /* Constants used by the vMemCheckTask() task. */
137 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
138 #define mainNO_TASK ( 0 )
140 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
141 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
142 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
143 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
145 /*-----------------------------------------------------------*/
148 * The Olimex demo board has a single built in LED. This function simply
151 void prvToggleOnBoardLED( void );
154 * Checks that all the demo application tasks are still executing without error
155 * - as described at the top of the file.
157 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
160 * The task that executes at the highest priority and calls
161 * prvCheckOtherTasksAreStillRunning(). See the description at the top
164 static void vErrorChecks( void *pvParameters );
167 * Dynamically created and deleted during each cycle of the vErrorChecks()
168 * task. This is done to check the operation of the memory allocator.
169 * See the top of vErrorChecks for more details.
171 static void vMemCheckTask( void *pvParameters );
174 * Configure the processor for use with the Olimex demo board. This includes
175 * setup for the I/O, system clock, and access timings.
177 static void prvSetupHardware( void );
179 /*-----------------------------------------------------------*/
182 * Starts all the other tasks, then starts the scheduler.
186 /* Setup the hardware for use with the Olimex demo board. */
189 /* Start the demo/test application tasks. */
190 vStartIntegerMathTasks( tskIDLE_PRIORITY );
191 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
192 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
193 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
194 vStartMathTasks( tskIDLE_PRIORITY );
195 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
196 vStartDynamicPriorityTasks();
197 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
199 /* Start the check task - which is defined in this file. */
200 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
202 /* Now all the tasks have been started - start the scheduler.
204 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
205 The processor MUST be in supervisor mode when vTaskStartScheduler is
206 called. The demo applications included in the FreeRTOS.org download switch
207 to supervisor mode prior to main being called. If you are not using one of
208 these demo application projects then ensure Supervisor mode is used here. */
209 vTaskStartScheduler();
211 /* Should never reach here! */
214 /*-----------------------------------------------------------*/
216 static void vErrorChecks( void *pvParameters )
218 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
219 unsigned long ulMemCheckTaskRunningCount;
220 TaskHandle_t xCreatedTask;
222 /* The parameters are not used in this function. */
223 ( void ) pvParameters;
225 /* Cycle for ever, delaying then checking all the other tasks are still
226 operating without error. If an error is detected then the delay period
227 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
228 the on board LED flash rate will increase.
230 In addition to the standard tests the memory allocator is tested through
231 the dynamic creation and deletion of a task each cycle. Each time the
232 task is created memory must be allocated for its stack. When the task is
233 deleted this memory is returned to the heap. If the task cannot be created
234 then it is likely that the memory allocation failed. */
238 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
240 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
241 xCreatedTask = mainNO_TASK;
243 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
245 /* Could not create the task - we have probably run out of heap. */
246 xDelayPeriod = mainERROR_FLASH_PERIOD;
249 /* Delay until it is time to execute again. */
250 vTaskDelay( xDelayPeriod );
252 /* Delete the dynamically created task. */
253 if( xCreatedTask != mainNO_TASK )
255 vTaskDelete( xCreatedTask );
258 /* Check all the standard demo application tasks are executing without
259 error. ulMemCheckTaskRunningCount is checked to ensure it was
260 modified by the task just deleted. */
261 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
263 /* An error has been detected in one of the tasks - flash faster. */
264 xDelayPeriod = mainERROR_FLASH_PERIOD;
267 prvToggleOnBoardLED();
270 /*-----------------------------------------------------------*/
272 static void prvSetupHardware( void )
275 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
279 /* Configure the RS2332 pins. All other pins remain at their default of 0. */
280 PCB_PINSEL0 |= mainTX_ENABLE;
281 PCB_PINSEL0 |= mainRX_ENABLE;
283 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
284 The JTAG pins are left as input as I'm not sure what will happen if the
285 Wiggler is connected after powerup - not that it would be a good idea to
287 GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );
289 /* Setup the PLL to multiply the XTAL input by 4. */
290 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
292 /* Activate the PLL by turning it on then feeding the correct sequence of
294 SCB_PLLCON = mainPLL_ENABLE;
295 SCB_PLLFEED = mainPLL_FEED_BYTE1;
296 SCB_PLLFEED = mainPLL_FEED_BYTE2;
298 /* Wait for the PLL to lock... */
299 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
301 /* ...before connecting it using the feed sequence again. */
302 SCB_PLLCON = mainPLL_CONNECT;
303 SCB_PLLFEED = mainPLL_FEED_BYTE1;
304 SCB_PLLFEED = mainPLL_FEED_BYTE2;
306 /* Setup and turn on the MAM. Three cycle access is used due to the fast
307 PLL used. It is possible faster overall performance could be obtained by
308 tuning the MAM and PLL settings. */
309 MAM_TIM = mainMAM_TIM_3;
310 MAM_CR = mainMAM_MODE_FULL;
312 /* Setup the peripheral bus to be the same as the PLL output. */
313 SCB_VPBDIV = mainBUS_CLK_FULL;
315 /* Initialise LED outputs. */
316 vParTestInitialise();
318 /*-----------------------------------------------------------*/
320 void prvToggleOnBoardLED( void )
322 unsigned long ulState;
324 ulState = GPIO0_IOPIN;
325 if( ulState & mainON_BOARD_LED_BIT )
327 GPIO_IOCLR = mainON_BOARD_LED_BIT;
331 GPIO_IOSET = mainON_BOARD_LED_BIT;
334 /*-----------------------------------------------------------*/
336 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
338 long lReturn = ( long ) pdPASS;
340 /* Check all the demo tasks (other than the flash tasks) to ensure
341 that they are all still running, and that none of them have detected
344 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
346 lReturn = ( long ) pdFAIL;
349 if( xAreComTestTasksStillRunning() != pdTRUE )
351 lReturn = ( long ) pdFAIL;
354 if( xArePollingQueuesStillRunning() != pdTRUE )
356 lReturn = ( long ) pdFAIL;
359 if( xAreMathsTaskStillRunning() != pdTRUE )
361 lReturn = ( long ) pdFAIL;
364 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
366 lReturn = ( long ) pdFAIL;
369 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
371 lReturn = ( long ) pdFAIL;
374 if( xAreBlockingQueuesStillRunning() != pdTRUE )
376 lReturn = ( long ) pdFAIL;
379 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
381 /* The vMemCheckTask did not increment the counter - it must
383 lReturn = ( long ) pdFAIL;
388 /*-----------------------------------------------------------*/
390 static void vMemCheckTask( void *pvParameters )
392 unsigned long *pulMemCheckTaskRunningCounter;
393 void *pvMem1, *pvMem2, *pvMem3;
394 static long lErrorOccurred = pdFALSE;
396 /* This task is dynamically created then deleted during each cycle of the
397 vErrorChecks task to check the operation of the memory allocator. Each time
398 the task is created memory is allocated for the stack and TCB. Each time
399 the task is deleted this memory is returned to the heap. This task itself
400 exercises the allocator by allocating and freeing blocks.
402 The task executes at the idle priority so does not require a delay.
404 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
405 vErrorChecks() task that this task is still executing without error. */
407 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
411 if( lErrorOccurred == pdFALSE )
413 /* We have never seen an error so increment the counter. */
414 ( *pulMemCheckTaskRunningCounter )++;
417 /* Allocate some memory - just to give the allocator some extra
418 exercise. This has to be in a critical section to ensure the
419 task does not get deleted while it has memory allocated. */
422 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
425 lErrorOccurred = pdTRUE;
429 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
435 /* Again - with a different size block. */
438 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
441 lErrorOccurred = pdTRUE;
445 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
451 /* Again - with a different size block. */
454 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
457 lErrorOccurred = pdTRUE;
461 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );