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 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
31 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
32 called. The demo applications included in the FreeRTOS.org download switch
\r
33 to supervisor mode prior to main being called. If you are not using one of
\r
34 these demo application projects then ensure Supervisor mode is used.
\r
39 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
40 * documentation provides more details of the demo application tasks.
\r
42 * Main.c also creates a task called "Check". This only executes every three
\r
43 * seconds but has the highest priority so is guaranteed to get processor time.
\r
44 * Its main function is to check that all the other tasks are still operational.
\r
45 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
46 * incremented each time the task successfully completes its function. Should
\r
47 * any error occur within such a task the count is permanently halted. The
\r
48 * check task inspects the count of each task to ensure it has changed since
\r
49 * the last time the check task executed. If all the count variables have
\r
50 * changed all the tasks are still executing error free, and the check task
\r
51 * toggles the onboard LED. Should any task contain an error at any time
\r
52 * the LED toggle rate will change from 3 seconds to 500ms.
\r
54 * To check the operation of the memory allocator the check task also
\r
55 * dynamically creates a task before delaying, and deletes it again when it
\r
56 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
57 * will fail and an error is signalled. The dynamically created task itself
\r
58 * allocates and frees memory just to give the allocator a bit more exercise.
\r
65 + The vErrorChecks() task now dynamically creates then deletes a task each
\r
66 cycle. This tests the operation of the memory allocator.
\r
70 + vParTestInitialise() is called during initialisation to ensure all the
\r
75 /* Standard includes. */
\r
79 /* Scheduler includes. */
\r
80 #include "FreeRTOS.h"
\r
83 /* Demo application includes. */
\r
84 #include "partest.h"
\r
86 #include "integer.h"
\r
88 #include "comtest2.h"
\r
89 #include "semtest.h"
\r
91 #include "dynamic.h"
\r
95 /*-----------------------------------------------------------*/
\r
97 /* Constants to setup I/O. */
\r
98 #define mainTX_ENABLE ( ( unsigned long ) 0x0001 )
\r
99 #define mainRX_ENABLE ( ( unsigned long ) 0x0004 )
\r
100 #define mainP0_14 ( ( unsigned long ) 0x4000 )
\r
101 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
\r
103 /* Constants to setup the PLL. */
\r
104 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
\r
105 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
\r
106 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
\r
107 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
\r
108 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
\r
109 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
\r
110 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
\r
112 /* Constants to setup the MAM. */
\r
113 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
\r
114 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
\r
116 /* Constants to setup the peripheral bus. */
\r
117 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
119 /* Constants for the ComTest tasks. */
\r
120 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
121 #define mainCOM_TEST_LED ( 3 )
\r
123 /* Priorities for the demo application tasks. */
\r
124 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
125 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
126 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
127 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
128 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
129 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
131 /* The rate at which the on board LED will toggle when there is/is not an
\r
133 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
134 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
135 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
\r
137 /* Constants used by the vMemCheckTask() task. */
\r
138 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
139 #define mainNO_TASK ( 0 )
\r
141 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
142 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
143 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
144 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
146 /*-----------------------------------------------------------*/
\r
149 * The Olimex demo board has a single built in LED. This function simply
\r
150 * toggles its state.
\r
152 void prvToggleOnBoardLED( void );
\r
155 * Checks that all the demo application tasks are still executing without error
\r
156 * - as described at the top of the file.
\r
158 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
161 * The task that executes at the highest priority and calls
\r
162 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
165 static void vErrorChecks( void *pvParameters );
\r
168 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
169 * task. This is done to check the operation of the memory allocator.
\r
170 * See the top of vErrorChecks for more details.
\r
172 static void vMemCheckTask( void *pvParameters );
\r
175 * Configure the processor for use with the Olimex demo board. This includes
\r
176 * setup for the I/O, system clock, and access timings.
\r
178 static void prvSetupHardware( void );
\r
180 /*-----------------------------------------------------------*/
\r
183 * Starts all the other tasks, then starts the scheduler.
\r
187 /* Setup the hardware for use with the Olimex demo board. */
\r
188 prvSetupHardware();
\r
190 /* Start the demo/test application tasks. */
\r
191 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
192 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
193 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
194 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
195 vStartMathTasks( tskIDLE_PRIORITY );
\r
196 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
197 vStartDynamicPriorityTasks();
\r
198 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
200 /* Start the check task - which is defined in this file. */
\r
201 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
203 /* Now all the tasks have been started - start the scheduler.
\r
205 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
206 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
207 called. The demo applications included in the FreeRTOS.org download switch
\r
208 to supervisor mode prior to main being called. If you are not using one of
\r
209 these demo application projects then ensure Supervisor mode is used here. */
\r
210 vTaskStartScheduler();
\r
212 /* Should never reach here! */
\r
215 /*-----------------------------------------------------------*/
\r
217 static void vErrorChecks( void *pvParameters )
\r
219 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
\r
220 unsigned long ulMemCheckTaskRunningCount;
\r
221 TaskHandle_t xCreatedTask;
\r
223 /* The parameters are not used in this function. */
\r
224 ( void ) pvParameters;
\r
226 /* Cycle for ever, delaying then checking all the other tasks are still
\r
227 operating without error. If an error is detected then the delay period
\r
228 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
\r
229 the on board LED flash rate will increase.
\r
231 In addition to the standard tests the memory allocator is tested through
\r
232 the dynamic creation and deletion of a task each cycle. Each time the
\r
233 task is created memory must be allocated for its stack. When the task is
\r
234 deleted this memory is returned to the heap. If the task cannot be created
\r
235 then it is likely that the memory allocation failed. */
\r
239 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
241 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
242 xCreatedTask = mainNO_TASK;
\r
244 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
246 /* Could not create the task - we have probably run out of heap. */
\r
247 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
250 /* Delay until it is time to execute again. */
\r
251 vTaskDelay( xDelayPeriod );
\r
253 /* Delete the dynamically created task. */
\r
254 if( xCreatedTask != mainNO_TASK )
\r
256 vTaskDelete( xCreatedTask );
\r
259 /* Check all the standard demo application tasks are executing without
\r
260 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
261 modified by the task just deleted. */
\r
262 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
264 /* An error has been detected in one of the tasks - flash faster. */
\r
265 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
268 prvToggleOnBoardLED();
\r
271 /*-----------------------------------------------------------*/
\r
273 static void prvSetupHardware( void )
\r
275 #ifdef RUN_FROM_RAM
\r
276 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
\r
280 /* Configure the RS2332 pins. All other pins remain at their default of 0. */
\r
281 PCB_PINSEL0 |= mainTX_ENABLE;
\r
282 PCB_PINSEL0 |= mainRX_ENABLE;
\r
284 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
\r
285 The JTAG pins are left as input as I'm not sure what will happen if the
\r
286 Wiggler is connected after powerup - not that it would be a good idea to
\r
288 GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );
\r
290 /* Setup the PLL to multiply the XTAL input by 4. */
\r
291 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
\r
293 /* Activate the PLL by turning it on then feeding the correct sequence of
\r
295 SCB_PLLCON = mainPLL_ENABLE;
\r
296 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
297 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
299 /* Wait for the PLL to lock... */
\r
300 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
\r
302 /* ...before connecting it using the feed sequence again. */
\r
303 SCB_PLLCON = mainPLL_CONNECT;
\r
304 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
305 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
307 /* Setup and turn on the MAM. Three cycle access is used due to the fast
\r
308 PLL used. It is possible faster overall performance could be obtained by
\r
309 tuning the MAM and PLL settings. */
\r
310 MAM_TIM = mainMAM_TIM_3;
\r
311 MAM_CR = mainMAM_MODE_FULL;
\r
313 /* Setup the peripheral bus to be the same as the PLL output. */
\r
314 SCB_VPBDIV = mainBUS_CLK_FULL;
\r
316 /* Initialise LED outputs. */
\r
317 vParTestInitialise();
\r
319 /*-----------------------------------------------------------*/
\r
321 void prvToggleOnBoardLED( void )
\r
323 unsigned long ulState;
\r
325 ulState = GPIO0_IOPIN;
\r
326 if( ulState & mainON_BOARD_LED_BIT )
\r
328 GPIO_IOCLR = mainON_BOARD_LED_BIT;
\r
332 GPIO_IOSET = mainON_BOARD_LED_BIT;
\r
335 /*-----------------------------------------------------------*/
\r
337 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
339 long lReturn = ( long ) pdPASS;
\r
341 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
342 that they are all still running, and that none of them have detected
\r
345 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
347 lReturn = ( long ) pdFAIL;
\r
350 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
352 lReturn = ( long ) pdFAIL;
\r
355 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
357 lReturn = ( long ) pdFAIL;
\r
360 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
362 lReturn = ( long ) pdFAIL;
\r
365 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
367 lReturn = ( long ) pdFAIL;
\r
370 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
372 lReturn = ( long ) pdFAIL;
\r
375 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
377 lReturn = ( long ) pdFAIL;
\r
380 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
382 /* The vMemCheckTask did not increment the counter - it must
\r
384 lReturn = ( long ) pdFAIL;
\r
389 /*-----------------------------------------------------------*/
\r
391 static void vMemCheckTask( void *pvParameters )
\r
393 unsigned long *pulMemCheckTaskRunningCounter;
\r
394 void *pvMem1, *pvMem2, *pvMem3;
\r
395 static long lErrorOccurred = pdFALSE;
\r
397 /* This task is dynamically created then deleted during each cycle of the
\r
398 vErrorChecks task to check the operation of the memory allocator. Each time
\r
399 the task is created memory is allocated for the stack and TCB. Each time
\r
400 the task is deleted this memory is returned to the heap. This task itself
\r
401 exercises the allocator by allocating and freeing blocks.
\r
403 The task executes at the idle priority so does not require a delay.
\r
405 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
406 vErrorChecks() task that this task is still executing without error. */
\r
408 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
412 if( lErrorOccurred == pdFALSE )
\r
414 /* We have never seen an error so increment the counter. */
\r
415 ( *pulMemCheckTaskRunningCounter )++;
\r
418 /* Allocate some memory - just to give the allocator some extra
\r
419 exercise. This has to be in a critical section to ensure the
\r
420 task does not get deleted while it has memory allocated. */
\r
423 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
424 if( pvMem1 == NULL )
\r
426 lErrorOccurred = pdTRUE;
\r
430 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
431 vPortFree( pvMem1 );
\r
436 /* Again - with a different size block. */
\r
439 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
440 if( pvMem2 == NULL )
\r
442 lErrorOccurred = pdTRUE;
\r
446 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
447 vPortFree( pvMem2 );
\r
452 /* Again - with a different size block. */
\r
455 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
456 if( pvMem3 == NULL )
\r
458 lErrorOccurred = pdTRUE;
\r
462 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
463 vPortFree( pvMem3 );
\r