2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
68 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
69 called. The demo applications included in the FreeRTOS.org download switch
\r
70 to supervisor mode prior to main being called. If you are not using one of
\r
71 these demo application projects then ensure Supervisor mode is used.
\r
76 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
77 * documentation provides more details of the demo application tasks.
\r
79 * Main.c also creates a task called "Check". This only executes every three
\r
80 * seconds but has the highest priority so is guaranteed to get processor time.
\r
81 * Its main function is to check that all the other tasks are still operational.
\r
82 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
83 * incremented each time the task successfully completes its function. Should
\r
84 * any error occur within such a task the count is permanently halted. The
\r
85 * check task inspects the count of each task to ensure it has changed since
\r
86 * the last time the check task executed. If all the count variables have
\r
87 * changed all the tasks are still executing error free, and the check task
\r
88 * toggles the onboard LED. Should any task contain an error at any time
\r
89 * the LED toggle rate will change from 3 seconds to 500ms.
\r
91 * To check the operation of the memory allocator the check task also
\r
92 * dynamically creates a task before delaying, and deletes it again when it
\r
93 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
94 * will fail and an error is signalled. The dynamically created task itself
\r
95 * allocates and frees memory just to give the allocator a bit more exercise.
\r
100 Changes from V2.4.2
\r
102 + The vErrorChecks() task now dynamically creates then deletes a task each
\r
103 cycle. This tests the operation of the memory allocator.
\r
105 Changes from V2.5.2
\r
107 + vParTestInitialise() is called during initialisation to ensure all the
\r
112 /* Standard includes. */
\r
113 #include <stdlib.h>
\r
114 #include <string.h>
\r
116 /* Scheduler includes. */
\r
117 #include "FreeRTOS.h"
\r
120 /* Demo application includes. */
\r
121 #include "partest.h"
\r
123 #include "integer.h"
\r
125 #include "comtest2.h"
\r
126 #include "semtest.h"
\r
128 #include "dynamic.h"
\r
129 #include "BlockQ.h"
\r
130 #include "serial.h"
\r
132 /*-----------------------------------------------------------*/
\r
134 /* Constants to setup I/O. */
\r
135 #define mainTX_ENABLE ( ( unsigned long ) 0x0001 )
\r
136 #define mainRX_ENABLE ( ( unsigned long ) 0x0004 )
\r
137 #define mainP0_14 ( ( unsigned long ) 0x4000 )
\r
138 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
\r
140 /* Constants to setup the PLL. */
\r
141 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
\r
142 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
\r
143 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
\r
144 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
\r
145 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
\r
146 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
\r
147 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
\r
149 /* Constants to setup the MAM. */
\r
150 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
\r
151 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
\r
153 /* Constants to setup the peripheral bus. */
\r
154 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
\r
156 /* Constants for the ComTest tasks. */
\r
157 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
158 #define mainCOM_TEST_LED ( 3 )
\r
160 /* Priorities for the demo application tasks. */
\r
161 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
162 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
163 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
164 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
165 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
166 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
168 /* The rate at which the on board LED will toggle when there is/is not an
\r
170 #define mainNO_ERROR_FLASH_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
171 #define mainERROR_FLASH_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
172 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
\r
174 /* Constants used by the vMemCheckTask() task. */
\r
175 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
176 #define mainNO_TASK ( 0 )
\r
178 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
179 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
180 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
181 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
183 /*-----------------------------------------------------------*/
\r
186 * The Olimex demo board has a single built in LED. This function simply
\r
187 * toggles its state.
\r
189 void prvToggleOnBoardLED( void );
\r
192 * Checks that all the demo application tasks are still executing without error
\r
193 * - as described at the top of the file.
\r
195 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
198 * The task that executes at the highest priority and calls
\r
199 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
202 static void vErrorChecks( void *pvParameters );
\r
205 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
206 * task. This is done to check the operation of the memory allocator.
\r
207 * See the top of vErrorChecks for more details.
\r
209 static void vMemCheckTask( void *pvParameters );
\r
212 * Configure the processor for use with the Olimex demo board. This includes
\r
213 * setup for the I/O, system clock, and access timings.
\r
215 static void prvSetupHardware( void );
\r
217 /*-----------------------------------------------------------*/
\r
220 * Starts all the other tasks, then starts the scheduler.
\r
224 /* Setup the hardware for use with the Olimex demo board. */
\r
225 prvSetupHardware();
\r
227 /* Start the demo/test application tasks. */
\r
228 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
229 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
230 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
231 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
232 vStartMathTasks( tskIDLE_PRIORITY );
\r
233 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
234 vStartDynamicPriorityTasks();
\r
235 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
237 /* Start the check task - which is defined in this file. */
\r
238 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
240 /* Now all the tasks have been started - start the scheduler.
\r
242 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
243 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
244 called. The demo applications included in the FreeRTOS.org download switch
\r
245 to supervisor mode prior to main being called. If you are not using one of
\r
246 these demo application projects then ensure Supervisor mode is used here. */
\r
247 vTaskStartScheduler();
\r
249 /* Should never reach here! */
\r
252 /*-----------------------------------------------------------*/
\r
254 static void vErrorChecks( void *pvParameters )
\r
256 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
\r
257 unsigned long ulMemCheckTaskRunningCount;
\r
258 TaskHandle_t xCreatedTask;
\r
260 /* The parameters are not used in this function. */
\r
261 ( void ) pvParameters;
\r
263 /* Cycle for ever, delaying then checking all the other tasks are still
\r
264 operating without error. If an error is detected then the delay period
\r
265 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
\r
266 the on board LED flash rate will increase.
\r
268 In addition to the standard tests the memory allocator is tested through
\r
269 the dynamic creation and deletion of a task each cycle. Each time the
\r
270 task is created memory must be allocated for its stack. When the task is
\r
271 deleted this memory is returned to the heap. If the task cannot be created
\r
272 then it is likely that the memory allocation failed. */
\r
276 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
278 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
279 xCreatedTask = mainNO_TASK;
\r
281 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
283 /* Could not create the task - we have probably run out of heap. */
\r
284 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
287 /* Delay until it is time to execute again. */
\r
288 vTaskDelay( xDelayPeriod );
\r
290 /* Delete the dynamically created task. */
\r
291 if( xCreatedTask != mainNO_TASK )
\r
293 vTaskDelete( xCreatedTask );
\r
296 /* Check all the standard demo application tasks are executing without
\r
297 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
298 modified by the task just deleted. */
\r
299 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
301 /* An error has been detected in one of the tasks - flash faster. */
\r
302 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
305 prvToggleOnBoardLED();
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvSetupHardware( void )
\r
312 #ifdef RUN_FROM_RAM
\r
313 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
\r
317 /* Configure the RS2332 pins. All other pins remain at their default of 0. */
\r
318 PCB_PINSEL0 |= mainTX_ENABLE;
\r
319 PCB_PINSEL0 |= mainRX_ENABLE;
\r
321 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
\r
322 The JTAG pins are left as input as I'm not sure what will happen if the
\r
323 Wiggler is connected after powerup - not that it would be a good idea to
\r
325 GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );
\r
327 /* Setup the PLL to multiply the XTAL input by 4. */
\r
328 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
\r
330 /* Activate the PLL by turning it on then feeding the correct sequence of
\r
332 SCB_PLLCON = mainPLL_ENABLE;
\r
333 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
334 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
336 /* Wait for the PLL to lock... */
\r
337 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
\r
339 /* ...before connecting it using the feed sequence again. */
\r
340 SCB_PLLCON = mainPLL_CONNECT;
\r
341 SCB_PLLFEED = mainPLL_FEED_BYTE1;
\r
342 SCB_PLLFEED = mainPLL_FEED_BYTE2;
\r
344 /* Setup and turn on the MAM. Three cycle access is used due to the fast
\r
345 PLL used. It is possible faster overall performance could be obtained by
\r
346 tuning the MAM and PLL settings. */
\r
347 MAM_TIM = mainMAM_TIM_3;
\r
348 MAM_CR = mainMAM_MODE_FULL;
\r
350 /* Setup the peripheral bus to be the same as the PLL output. */
\r
351 SCB_VPBDIV = mainBUS_CLK_FULL;
\r
353 /* Initialise LED outputs. */
\r
354 vParTestInitialise();
\r
356 /*-----------------------------------------------------------*/
\r
358 void prvToggleOnBoardLED( void )
\r
360 unsigned long ulState;
\r
362 ulState = GPIO0_IOPIN;
\r
363 if( ulState & mainON_BOARD_LED_BIT )
\r
365 GPIO_IOCLR = mainON_BOARD_LED_BIT;
\r
369 GPIO_IOSET = mainON_BOARD_LED_BIT;
\r
372 /*-----------------------------------------------------------*/
\r
374 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
376 long lReturn = ( long ) pdPASS;
\r
378 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
379 that they are all still running, and that none of them have detected
\r
382 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
384 lReturn = ( long ) pdFAIL;
\r
387 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
389 lReturn = ( long ) pdFAIL;
\r
392 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
394 lReturn = ( long ) pdFAIL;
\r
397 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
399 lReturn = ( long ) pdFAIL;
\r
402 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
404 lReturn = ( long ) pdFAIL;
\r
407 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
409 lReturn = ( long ) pdFAIL;
\r
412 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
414 lReturn = ( long ) pdFAIL;
\r
417 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
419 /* The vMemCheckTask did not increment the counter - it must
\r
421 lReturn = ( long ) pdFAIL;
\r
426 /*-----------------------------------------------------------*/
\r
428 static void vMemCheckTask( void *pvParameters )
\r
430 unsigned long *pulMemCheckTaskRunningCounter;
\r
431 void *pvMem1, *pvMem2, *pvMem3;
\r
432 static long lErrorOccurred = pdFALSE;
\r
434 /* This task is dynamically created then deleted during each cycle of the
\r
435 vErrorChecks task to check the operation of the memory allocator. Each time
\r
436 the task is created memory is allocated for the stack and TCB. Each time
\r
437 the task is deleted this memory is returned to the heap. This task itself
\r
438 exercises the allocator by allocating and freeing blocks.
\r
440 The task executes at the idle priority so does not require a delay.
\r
442 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
443 vErrorChecks() task that this task is still executing without error. */
\r
445 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
449 if( lErrorOccurred == pdFALSE )
\r
451 /* We have never seen an error so increment the counter. */
\r
452 ( *pulMemCheckTaskRunningCounter )++;
\r
455 /* Allocate some memory - just to give the allocator some extra
\r
456 exercise. This has to be in a critical section to ensure the
\r
457 task does not get deleted while it has memory allocated. */
\r
460 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
461 if( pvMem1 == NULL )
\r
463 lErrorOccurred = pdTRUE;
\r
467 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
468 vPortFree( pvMem1 );
\r
473 /* Again - with a different size block. */
\r
476 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
477 if( pvMem2 == NULL )
\r
479 lErrorOccurred = pdTRUE;
\r
483 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
484 vPortFree( pvMem2 );
\r
489 /* Again - with a different size block. */
\r
492 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
493 if( pvMem3 == NULL )
\r
495 lErrorOccurred = pdTRUE;
\r
499 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
500 vPortFree( pvMem3 );
\r