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