simple-FreeRTOS-demo application is created
[armadillo_firmware.git] / simple-FreeRTOS-demo / main.c
blobfb2444734a9b18c84062c97e68ac1a127c035497
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
54 /*
55 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
56 The processor MUST be in supervisor mode when vTaskStartScheduler is
57 called. The demo applications included in the FreeRTOS.org download switch
58 to supervisor mode prior to main being called. If you are not using one of
59 these demo application projects then ensure Supervisor mode is used.
64 * Creates all the demo application tasks, then starts the scheduler. The WEB
65 * documentation provides more details of the demo application tasks.
67 * Main.c also creates a task called "Check". This only executes every three
68 * seconds but has the highest priority so is guaranteed to get processor time.
69 * Its main function is to check that all the other tasks are still operational.
70 * Each task (other than the "flash" tasks) maintains a unique count that is
71 * incremented each time the task successfully completes its function. Should
72 * any error occur within such a task the count is permanently halted. The
73 * check task inspects the count of each task to ensure it has changed since
74 * the last time the check task executed. If all the count variables have
75 * changed all the tasks are still executing error free, and the check task
76 * toggles the onboard LED. Should any task contain an error at any time
77 * the LED toggle rate will change from 3 seconds to 500ms.
79 * To check the operation of the memory allocator the check task also
80 * dynamically creates a task before delaying, and deletes it again when it
81 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
82 * will fail and an error is signalled. The dynamically created task itself
83 * allocates and frees memory just to give the allocator a bit more exercise.
87 /*
88 Changes from V2.4.2
90 + The vErrorChecks() task now dynamically creates then deletes a task each
91 cycle. This tests the operation of the memory allocator.
93 Changes from V2.5.2
95 + vParTestInitialise() is called during initialisation to ensure all the
96 LED's start off.
100 /* Standard includes. */
101 #include <stdlib.h>
102 #include <string.h>
104 /* Scheduler includes. */
105 #include "FreeRTOS.h"
106 #include "task.h"
108 /* Demo application includes. */
109 #include "partest.h"
110 #include "flash.h"
111 #include "integer.h"
112 #include "PollQ.h"
113 #include "comtest2.h"
114 #include "semtest.h"
115 #include "flop.h"
116 #include "dynamic.h"
117 #include "BlockQ.h"
118 #include "serial.h"
120 /*-----------------------------------------------------------*/
122 /* Constants to setup I/O. */
123 #define mainTX_ENABLE ( ( unsigned long ) 0x0001 )
124 #define mainRX_ENABLE ( ( unsigned long ) 0x0004 )
125 #define mainP0_14 ( ( unsigned long ) 0x4000 )
126 #define mainJTAG_PORT ( ( unsigned long ) 0x3E0000UL )
128 /* Constants to setup the PLL. */
129 #define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
130 #define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
131 #define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
132 #define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
133 #define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
134 #define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
135 #define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
137 /* Constants to setup the MAM. */
138 #define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
139 #define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
141 /* Constants to setup the peripheral bus. */
142 #define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
144 /* Constants for the ComTest tasks. */
145 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
146 #define mainCOM_TEST_LED ( 3 )
148 /* Priorities for the demo application tasks. */
149 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
150 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
151 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 0 )
152 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
153 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 0 )
154 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
156 /* The rate at which the on board LED will toggle when there is/is not an
157 error. */
158 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
159 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
160 #define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
162 /* Constants used by the vMemCheckTask() task. */
163 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
164 #define mainNO_TASK ( 0 )
166 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
167 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
168 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
169 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
171 /*-----------------------------------------------------------*/
174 * The Olimex demo board has a single built in LED. This function simply
175 * toggles its state.
177 void prvToggleOnBoardLED( void );
180 * Checks that all the demo application tasks are still executing without error
181 * - as described at the top of the file.
183 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
186 * The task that executes at the highest priority and calls
187 * prvCheckOtherTasksAreStillRunning(). See the description at the top
188 * of the file.
190 static void vErrorChecks( void *pvParameters );
193 * Dynamically created and deleted during each cycle of the vErrorChecks()
194 * task. This is done to check the operation of the memory allocator.
195 * See the top of vErrorChecks for more details.
197 static void vMemCheckTask( void *pvParameters );
200 * Configure the processor for use with the Olimex demo board. This includes
201 * setup for the I/O, system clock, and access timings.
203 static void prvSetupHardware( void );
205 /*-----------------------------------------------------------*/
208 * Starts all the other tasks, then starts the scheduler.
210 int main( void )
212 /* Setup the hardware for use with the Olimex demo board. */
213 prvSetupHardware();
215 /* Start the demo/test application tasks. */
216 vStartIntegerMathTasks( tskIDLE_PRIORITY );
217 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
218 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
219 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
220 vStartMathTasks( tskIDLE_PRIORITY );
221 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
222 vStartDynamicPriorityTasks();
223 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
225 /* Start the check task - which is defined in this file. */
226 xTaskCreate( vErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
228 /* Now all the tasks have been started - start the scheduler.
230 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
231 The processor MUST be in supervisor mode when vTaskStartScheduler is
232 called. The demo applications included in the FreeRTOS.org download switch
233 to supervisor mode prior to main being called. If you are not using one of
234 these demo application projects then ensure Supervisor mode is used here. */
235 vTaskStartScheduler();
237 /* Should never reach here! */
238 return 0;
240 /*-----------------------------------------------------------*/
242 static void vErrorChecks( void *pvParameters )
244 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
245 unsigned long ulMemCheckTaskRunningCount;
246 xTaskHandle xCreatedTask;
248 /* The parameters are not used in this function. */
249 ( void ) pvParameters;
251 /* Cycle for ever, delaying then checking all the other tasks are still
252 operating without error. If an error is detected then the delay period
253 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
254 the on board LED flash rate will increase.
256 In addition to the standard tests the memory allocator is tested through
257 the dynamic creation and deletion of a task each cycle. Each time the
258 task is created memory must be allocated for its stack. When the task is
259 deleted this memory is returned to the heap. If the task cannot be created
260 then it is likely that the memory allocation failed. */
262 for( ;; )
264 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
265 parameter. */
266 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
267 xCreatedTask = mainNO_TASK;
269 if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
271 /* Could not create the task - we have probably run out of heap. */
272 xDelayPeriod = mainERROR_FLASH_PERIOD;
275 /* Delay until it is time to execute again. */
276 vTaskDelay( xDelayPeriod );
278 /* Delete the dynamically created task. */
279 if( xCreatedTask != mainNO_TASK )
281 vTaskDelete( xCreatedTask );
284 /* Check all the standard demo application tasks are executing without
285 error. ulMemCheckTaskRunningCount is checked to ensure it was
286 modified by the task just deleted. */
287 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
289 /* An error has been detected in one of the tasks - flash faster. */
290 xDelayPeriod = mainERROR_FLASH_PERIOD;
293 prvToggleOnBoardLED();
296 /*-----------------------------------------------------------*/
298 static void prvSetupHardware( void )
300 #ifdef RUN_FROM_RAM
301 /* Remap the interrupt vectors to RAM if we are are running from RAM. */
302 SCB_MEMMAP = 2;
303 #endif
305 /* Configure the RS2332 pins. All other pins remain at their default of 0. */
306 PCB_PINSEL0 |= mainTX_ENABLE;
307 PCB_PINSEL0 |= mainRX_ENABLE;
309 /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.
310 The JTAG pins are left as input as I'm not sure what will happen if the
311 Wiggler is connected after powerup - not that it would be a good idea to
312 do that anyway. */
313 GPIO_IODIR = ~( mainP0_14 + mainJTAG_PORT );
315 /* Setup the PLL to multiply the XTAL input by 4. */
316 SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
318 /* Activate the PLL by turning it on then feeding the correct sequence of
319 bytes. */
320 SCB_PLLCON = mainPLL_ENABLE;
321 SCB_PLLFEED = mainPLL_FEED_BYTE1;
322 SCB_PLLFEED = mainPLL_FEED_BYTE2;
324 /* Wait for the PLL to lock... */
325 while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
327 /* ...before connecting it using the feed sequence again. */
328 SCB_PLLCON = mainPLL_CONNECT;
329 SCB_PLLFEED = mainPLL_FEED_BYTE1;
330 SCB_PLLFEED = mainPLL_FEED_BYTE2;
332 /* Setup and turn on the MAM. Three cycle access is used due to the fast
333 PLL used. It is possible faster overall performance could be obtained by
334 tuning the MAM and PLL settings. */
335 MAM_TIM = mainMAM_TIM_3;
336 MAM_CR = mainMAM_MODE_FULL;
338 /* Setup the peripheral bus to be the same as the PLL output. */
339 SCB_VPBDIV = mainBUS_CLK_FULL;
341 /* Initialise LED outputs. */
342 vParTestInitialise();
344 /*-----------------------------------------------------------*/
346 void prvToggleOnBoardLED( void )
348 unsigned long ulState;
350 ulState = GPIO0_IOPIN;
351 if( ulState & mainON_BOARD_LED_BIT )
353 GPIO_IOCLR = mainON_BOARD_LED_BIT;
355 else
357 GPIO_IOSET = mainON_BOARD_LED_BIT;
360 /*-----------------------------------------------------------*/
362 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
364 long lReturn = ( long ) pdPASS;
366 /* Check all the demo tasks (other than the flash tasks) to ensure
367 that they are all still running, and that none of them have detected
368 an error. */
370 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
372 lReturn = ( long ) pdFAIL;
375 if( xAreComTestTasksStillRunning() != pdTRUE )
377 lReturn = ( long ) pdFAIL;
380 if( xArePollingQueuesStillRunning() != pdTRUE )
382 lReturn = ( long ) pdFAIL;
385 if( xAreMathsTaskStillRunning() != pdTRUE )
387 lReturn = ( long ) pdFAIL;
390 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
392 lReturn = ( long ) pdFAIL;
395 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
397 lReturn = ( long ) pdFAIL;
400 if( xAreBlockingQueuesStillRunning() != pdTRUE )
402 lReturn = ( long ) pdFAIL;
405 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
407 /* The vMemCheckTask did not increment the counter - it must
408 have failed. */
409 lReturn = ( long ) pdFAIL;
412 return lReturn;
414 /*-----------------------------------------------------------*/
416 static void vMemCheckTask( void *pvParameters )
418 unsigned long *pulMemCheckTaskRunningCounter;
419 void *pvMem1, *pvMem2, *pvMem3;
420 static long lErrorOccurred = pdFALSE;
422 /* This task is dynamically created then deleted during each cycle of the
423 vErrorChecks task to check the operation of the memory allocator. Each time
424 the task is created memory is allocated for the stack and TCB. Each time
425 the task is deleted this memory is returned to the heap. This task itself
426 exercises the allocator by allocating and freeing blocks.
428 The task executes at the idle priority so does not require a delay.
430 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
431 vErrorChecks() task that this task is still executing without error. */
433 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
435 for( ;; )
437 if( lErrorOccurred == pdFALSE )
439 /* We have never seen an error so increment the counter. */
440 ( *pulMemCheckTaskRunningCounter )++;
443 /* Allocate some memory - just to give the allocator some extra
444 exercise. This has to be in a critical section to ensure the
445 task does not get deleted while it has memory allocated. */
446 vTaskSuspendAll();
448 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
449 if( pvMem1 == NULL )
451 lErrorOccurred = pdTRUE;
453 else
455 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
456 vPortFree( pvMem1 );
459 xTaskResumeAll();
461 /* Again - with a different size block. */
462 vTaskSuspendAll();
464 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
465 if( pvMem2 == NULL )
467 lErrorOccurred = pdTRUE;
469 else
471 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
472 vPortFree( pvMem2 );
475 xTaskResumeAll();
477 /* Again - with a different size block. */
478 vTaskSuspendAll();
480 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
481 if( pvMem3 == NULL )
483 lErrorOccurred = pdTRUE;
485 else
487 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
488 vPortFree( pvMem3 );
491 xTaskResumeAll();