2 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
56 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
57 called. The demo applications included in the FreeRTOS.org download switch
\r
58 to supervisor mode prior to main being called. If you are not using one of
\r
59 these demo application projects then ensure Supervisor mode is used.
\r
64 * Creates all the application tasks, then starts the scheduler.
\r
66 * A task defined by the function vBasicWEBServer is created. This executes
\r
67 * the lwIP stack and basic WEB server sample. A task defined by the function
\r
68 * vUSBCDCTask. This executes the USB to serial CDC example. All the other
\r
69 * tasks are from the set of standard demo tasks. The WEB documentation
\r
70 * provides more details of the standard demo application tasks.
\r
72 * Main.c also creates a task called "Check". This only executes every three
\r
73 * seconds but has the highest priority so is guaranteed to get processor time.
\r
74 * Its main function is to check the status of all the other demo application
\r
75 * tasks. LED mainCHECK_LED is toggled every three seconds by the check task
\r
76 * should no error conditions be detected in any of the standard demo tasks.
\r
77 * The toggle rate increasing to 500ms indicates that at least one error has
\r
80 * Main.c includes an idle hook function that simply periodically sends data
\r
81 * to the USB task for transmission.
\r
87 + Modified the stack sizes used by some tasks to permit use of the
\r
88 command line GCC tools.
\r
91 /* Library includes. */
\r
95 /* Scheduler includes. */
\r
96 #include "FreeRTOS.h"
\r
99 /* Demo application includes. */
\r
100 #include "partest.h"
\r
102 #include "semtest.h"
\r
104 #include "integer.h"
\r
105 #include "BlockQ.h"
\r
106 #include "BasicWEB.h"
\r
107 #include "USB-CDC.h"
\r
109 /* lwIP includes. */
\r
110 #include "lwip/api.h"
\r
112 /* Hardware specific headers. */
\r
114 #include "AT91SAM7X256.h"
\r
116 /* Priorities/stacks for the various tasks within the demo application. */
\r
117 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
118 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
119 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
120 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
121 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
122 #define mainWEBSERVER_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
123 #define mainUSB_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
124 #define mainUSB_TASK_STACK ( 200 )
\r
126 /* The rate at which the on board LED will toggle when there is/is not an
\r
128 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
129 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
131 /* The rate at which the idle hook sends data to the USB port. */
\r
132 #define mainUSB_TX_FREQUENCY ( 100 / portTICK_RATE_MS )
\r
134 /* The string that is transmitted down the USB port. */
\r
135 #define mainFIRST_TX_CHAR 'a'
\r
136 #define mainLAST_TX_CHAR 'z'
\r
138 /* The LED used by the check task to indicate the system status. */
\r
139 #define mainCHECK_LED ( 3 )
\r
140 /*-----------------------------------------------------------*/
\r
143 * Checks that all the demo application tasks are still executing without error
\r
144 * - as described at the top of the file.
\r
146 static long prvCheckOtherTasksAreStillRunning( void );
\r
149 * The task that executes at the highest priority and calls
\r
150 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
153 static void vErrorChecks( void *pvParameters );
\r
156 * Configure the processor for use with the Atmel demo board. This is very
\r
157 * minimal as most of the setup is performed in the startup code.
\r
159 static void prvSetupHardware( void );
\r
162 * The idle hook is just used to stream data to the USB port.
\r
164 void vApplicationIdleHook( void );
\r
165 /*-----------------------------------------------------------*/
\r
168 * Setup hardware then start all the demo application tasks.
\r
172 /* Setup the ports. */
\r
173 prvSetupHardware();
\r
175 /* Setup the IO required for the LED's. */
\r
176 vParTestInitialise();
\r
181 /* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
\r
182 sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );
\r
184 /* Create the demo USB CDC task. */
\r
185 xTaskCreate( vUSBCDCTask, ( signed char * ) "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );
\r
187 /* Create the standard demo application tasks. */
\r
188 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
189 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
190 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
191 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
192 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
194 /* Start the check task - which is defined in this file. */
\r
195 xTaskCreate( vErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
197 /* Finally, start the scheduler.
\r
199 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
200 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
201 called. The demo applications included in the FreeRTOS.org download switch
\r
202 to supervisor mode prior to main being called. If you are not using one of
\r
203 these demo application projects then ensure Supervisor mode is used here. */
\r
204 vTaskStartScheduler();
\r
206 /* Should never get here! */
\r
209 /*-----------------------------------------------------------*/
\r
212 static void prvSetupHardware( void )
\r
214 /* When using the JTAG debugger the hardware is not always initialised to
\r
215 the correct default state. This line just ensures that this does not
\r
216 cause all interrupts to be masked at the start. */
\r
217 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
219 /* Most setup is performed by the low level init function called from the
\r
222 Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
\r
223 well as the UART Tx line. */
\r
224 AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode
\r
225 AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output
\r
228 /* Enable the peripheral clock. */
\r
229 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
\r
230 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
\r
231 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
\r
233 /*-----------------------------------------------------------*/
\r
235 static void vErrorChecks( void *pvParameters )
\r
237 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
\r
238 portTickType xLastWakeTime;
\r
240 /* The parameters are not used. */
\r
241 ( void ) pvParameters;
\r
243 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
244 functions correctly. */
\r
245 xLastWakeTime = xTaskGetTickCount();
\r
247 /* Cycle for ever, delaying then checking all the other tasks are still
\r
248 operating without error. If an error is detected then the delay period
\r
249 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
\r
250 the Check LED flash rate will increase. */
\r
253 /* Delay until it is time to execute again. The delay period is
\r
254 shorter following an error. */
\r
255 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
257 /* Check all the standard demo application tasks are executing without
\r
259 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
261 /* An error has been detected in one of the tasks - flash faster. */
\r
262 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
265 vParTestToggleLED( mainCHECK_LED );
\r
268 /*-----------------------------------------------------------*/
\r
270 static long prvCheckOtherTasksAreStillRunning( void )
\r
272 long lReturn = ( long ) pdPASS;
\r
274 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
275 that they are all still running, and that none of them have detected
\r
278 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
280 lReturn = ( long ) pdFAIL;
\r
283 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
285 lReturn = ( long ) pdFAIL;
\r
288 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
290 lReturn = ( long ) pdFAIL;
\r
293 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
295 lReturn = ( long ) pdFAIL;
\r
300 /*-----------------------------------------------------------*/
\r
302 void vApplicationIdleHook( void )
\r
304 static portTickType xLastTx = 0;
\r
307 /* The idle hook simply sends a string of characters to the USB port.
\r
308 The characters will be buffered and sent once the port is connected. */
\r
309 if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )
\r
311 xLastTx = xTaskGetTickCount();
\r
312 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )
\r
314 vUSBSendByte( cTxByte );
\r