2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
67 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
68 called. The demo applications included in the FreeRTOS.org download switch
\r
69 to supervisor mode prior to main being called. If you are not using one of
\r
70 these demo application projects then ensure Supervisor mode is used.
\r
75 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
76 * documentation provides more details of the demo application tasks.
\r
78 * Main.c also creates a task called "Check". This only executes every three
\r
79 * seconds but has the highest priority so is guaranteed to get processor time.
\r
80 * Its main function is to check that all the other tasks are still operational.
\r
81 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
82 * incremented each time the task successfully completes its function. Should
\r
83 * any error occur within such a task the count is permanently halted. The
\r
84 * check task inspects the count of each task to ensure it has changed since
\r
85 * the last time the check task executed. If all the count variables have
\r
86 * changed all the tasks are still executing error free, and the check task
\r
87 * toggles the onboard LED. Should any task contain an error at any time
\r
88 * the LED toggle rate will change from 3 seconds to 500ms.
\r
90 * To check the operation of the memory allocator the check task also
\r
91 * dynamically creates a task before delaying, and deletes it again when it
\r
92 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
93 * will fail and an error is signalled. The dynamically created task itself
\r
94 * allocates and frees memory just to give the allocator a bit more exercise.
\r
101 + The vErrorChecks() task now dynamically creates then deletes a task each
\r
102 cycle. This tests the operation of the memory allocator.
\r
104 Changes from V2.5.2
\r
106 + vParTestInitialise() is called during initialisation to ensure all the
\r
111 /* Standard includes. */
\r
112 #include <stdlib.h>
\r
113 #include <string.h>
\r
115 /* Scheduler includes. */
\r
116 #include "FreeRTOS.h"
\r
119 /* Demo application includes. */
\r
120 #include "partest.h"
\r
122 #include "integer.h"
\r
124 #include "comtest2.h"
\r
125 #include "semtest.h"
\r
127 #include "dynamic.h"
\r
128 #include "BlockQ.h"
\r
129 #include "serial.h"
\r
131 /*-----------------------------------------------------------*/
\r
133 /* Constants to setup I/O. */
\r
134 #define mainTX_ENABLE ( ( unsigned long ) 0x0001 )
\r
135 #define mainRX_ENABLE ( ( unsigned long ) 0x0004 )
\r
136 #define mainP0_14 ( ( unsigned long ) 0x4000 )
\r
137 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
\r
139 /* Constants to setup the PLL. */
\r
140 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
\r
141 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
\r
142 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
\r
143 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
\r
144 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
\r
145 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
\r
146 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
\r
148 /* Constants to setup the MAM. */
\r
149 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
\r
150 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
\r
152 /* Constants to setup the peripheral bus. */
\r
153 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
155 /* Constants for the ComTest tasks. */
\r
156 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
157 #define mainCOM_TEST_LED ( 3 )
\r
159 /* Priorities for the demo application tasks. */
\r
160 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
161 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
162 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
163 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
164 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
165 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
167 /* The rate at which the on board LED will toggle when there is/is not an
\r
169 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
170 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
171 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
\r
173 /* Constants used by the vMemCheckTask() task. */
\r
174 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
175 #define mainNO_TASK ( 0 )
\r
177 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
178 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
179 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
180 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
182 /*-----------------------------------------------------------*/
\r
185 * The Olimex demo board has a single built in LED. This function simply
\r
186 * toggles its state.
\r
188 void prvToggleOnBoardLED( void );
\r
191 * Checks that all the demo application tasks are still executing without error
\r
192 * - as described at the top of the file.
\r
194 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
197 * The task that executes at the highest priority and calls
\r
198 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
201 static void vErrorChecks( void *pvParameters );
\r
204 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
205 * task. This is done to check the operation of the memory allocator.
\r
206 * See the top of vErrorChecks for more details.
\r
208 static void vMemCheckTask( void *pvParameters );
\r
211 * Configure the processor for use with the Olimex demo board. This includes
\r
212 * setup for the I/O, system clock, and access timings.
\r
214 static void prvSetupHardware( void );
\r
216 /*-----------------------------------------------------------*/
\r
219 * Starts all the other tasks, then starts the scheduler.
\r
223 /* Setup the hardware for use with the Olimex demo board. */
\r
224 prvSetupHardware();
\r
226 /* Start the demo/test application tasks. */
\r
227 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
228 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
229 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
230 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
231 vStartMathTasks( tskIDLE_PRIORITY );
\r
232 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
233 vStartDynamicPriorityTasks();
\r
234 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
236 /* Start the check task - which is defined in this file. */
\r
237 xTaskCreate( vErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
239 /* Now all the tasks have been started - start the scheduler.
\r
241 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
242 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
243 called. The demo applications included in the FreeRTOS.org download switch
\r
244 to supervisor mode prior to main being called. If you are not using one of
\r
245 these demo application projects then ensure Supervisor mode is used here. */
\r
246 vTaskStartScheduler();
\r
248 /* Should never reach here! */
\r
251 /*-----------------------------------------------------------*/
\r
253 static void vErrorChecks( void *pvParameters )
\r
255 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
\r
256 unsigned long ulMemCheckTaskRunningCount;
\r
257 xTaskHandle xCreatedTask;
\r
259 /* The parameters are not used in this function. */
\r
260 ( void ) pvParameters;
\r
262 /* Cycle for ever, delaying then checking all the other tasks are still
\r
263 operating without error. If an error is detected then the delay period
\r
264 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
\r
265 the on board LED flash rate will increase.
\r
267 In addition to the standard tests the memory allocator is tested through
\r
268 the dynamic creation and deletion of a task each cycle. Each time the
\r
269 task is created memory must be allocated for its stack. When the task is
\r
270 deleted this memory is returned to the heap. If the task cannot be created
\r
271 then it is likely that the memory allocation failed. */
\r
275 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
277 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
278 xCreatedTask = mainNO_TASK;
\r
280 if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
282 /* Could not create the task - we have probably run out of heap. */
\r
283 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
286 /* Delay until it is time to execute again. */
\r
287 vTaskDelay( xDelayPeriod );
\r
289 /* Delete the dynamically created task. */
\r
290 if( xCreatedTask != mainNO_TASK )
\r
292 vTaskDelete( xCreatedTask );
\r
295 /* Check all the standard demo application tasks are executing without
\r
296 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
297 modified by the task just deleted. */
\r
298 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
300 /* An error has been detected in one of the tasks - flash faster. */
\r
301 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
304 prvToggleOnBoardLED();
\r
307 /*-----------------------------------------------------------*/
\r
309 static void prvSetupHardware( void )
\r
311 #ifdef RUN_FROM_RAM
\r
312 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
\r
316 /* Configure the RS2332 pins. All other pins remain at their default of 0. */
\r
317 PCB_PINSEL0 |= mainTX_ENABLE;
\r
318 PCB_PINSEL0 |= mainRX_ENABLE;
\r
320 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
\r
321 The JTAG pins are left as input as I'm not sure what will happen if the
\r
322 Wiggler is connected after powerup - not that it would be a good idea to
\r
324 GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );
\r
326 /* Setup the PLL to multiply the XTAL input by 4. */
\r
327 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
\r
329 /* Activate the PLL by turning it on then feeding the correct sequence of
\r
331 SCB_PLLCON = mainPLL_ENABLE;
\r
332 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
333 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
335 /* Wait for the PLL to lock... */
\r
336 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
\r
338 /* ...before connecting it using the feed sequence again. */
\r
339 SCB_PLLCON = mainPLL_CONNECT;
\r
340 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
341 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
343 /* Setup and turn on the MAM. Three cycle access is used due to the fast
\r
344 PLL used. It is possible faster overall performance could be obtained by
\r
345 tuning the MAM and PLL settings. */
\r
346 MAM_TIM = mainMAM_TIM_3;
\r
347 MAM_CR = mainMAM_MODE_FULL;
\r
349 /* Setup the peripheral bus to be the same as the PLL output. */
\r
350 SCB_VPBDIV = mainBUS_CLK_FULL;
\r
352 /* Initialise LED outputs. */
\r
353 vParTestInitialise();
\r
355 /*-----------------------------------------------------------*/
\r
357 void prvToggleOnBoardLED( void )
\r
359 unsigned long ulState;
\r
361 ulState = GPIO0_IOPIN;
\r
362 if( ulState & mainON_BOARD_LED_BIT )
\r
364 GPIO_IOCLR = mainON_BOARD_LED_BIT;
\r
368 GPIO_IOSET = mainON_BOARD_LED_BIT;
\r
371 /*-----------------------------------------------------------*/
\r
373 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
375 long lReturn = ( long ) pdPASS;
\r
377 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
378 that they are all still running, and that none of them have detected
\r
381 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
383 lReturn = ( long ) pdFAIL;
\r
386 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
388 lReturn = ( long ) pdFAIL;
\r
391 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
393 lReturn = ( long ) pdFAIL;
\r
396 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
398 lReturn = ( long ) pdFAIL;
\r
401 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
403 lReturn = ( long ) pdFAIL;
\r
406 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
408 lReturn = ( long ) pdFAIL;
\r
411 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
413 lReturn = ( long ) pdFAIL;
\r
416 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
418 /* The vMemCheckTask did not increment the counter - it must
\r
420 lReturn = ( long ) pdFAIL;
\r
425 /*-----------------------------------------------------------*/
\r
427 static void vMemCheckTask( void *pvParameters )
\r
429 unsigned long *pulMemCheckTaskRunningCounter;
\r
430 void *pvMem1, *pvMem2, *pvMem3;
\r
431 static long lErrorOccurred = pdFALSE;
\r
433 /* This task is dynamically created then deleted during each cycle of the
\r
434 vErrorChecks task to check the operation of the memory allocator. Each time
\r
435 the task is created memory is allocated for the stack and TCB. Each time
\r
436 the task is deleted this memory is returned to the heap. This task itself
\r
437 exercises the allocator by allocating and freeing blocks.
\r
439 The task executes at the idle priority so does not require a delay.
\r
441 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
442 vErrorChecks() task that this task is still executing without error. */
\r
444 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
448 if( lErrorOccurred == pdFALSE )
\r
450 /* We have never seen an error so increment the counter. */
\r
451 ( *pulMemCheckTaskRunningCounter )++;
\r
454 /* Allocate some memory - just to give the allocator some extra
\r
455 exercise. This has to be in a critical section to ensure the
\r
456 task does not get deleted while it has memory allocated. */
\r
459 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
460 if( pvMem1 == NULL )
\r
462 lErrorOccurred = pdTRUE;
\r
466 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
467 vPortFree( pvMem1 );
\r
472 /* Again - with a different size block. */
\r
475 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
476 if( pvMem2 == NULL )
\r
478 lErrorOccurred = pdTRUE;
\r
482 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
483 vPortFree( pvMem2 );
\r
488 /* Again - with a different size block. */
\r
491 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
492 if( pvMem3 == NULL )
\r
494 lErrorOccurred = pdTRUE;
\r
498 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
499 vPortFree( pvMem3 );
\r