2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
54 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
55 called. The demo applications included in the FreeRTOS.org download switch
\r
56 to supervisor mode prior to main being called. If you are not using one of
\r
57 these demo application projects then ensure Supervisor mode is used.
\r
62 * Creates all the application tasks, then starts the scheduler.
\r
64 * A task defined by the function vBasicWEBServer is created. This executes
\r
65 * the lwIP stack and basic WEB server sample. A task defined by the function
\r
66 * vUSBCDCTask. This executes the USB to serial CDC example. All the other
\r
67 * tasks are from the set of standard demo tasks. The WEB documentation
\r
68 * provides more details of the standard demo application tasks.
\r
70 * Main.c also creates a task called "Check". This only executes every three
\r
71 * seconds but has the highest priority so is guaranteed to get processor time.
\r
72 * Its main function is to check the status of all the other demo application
\r
73 * tasks. LED mainCHECK_LED is toggled every three seconds by the check task
\r
74 * should no error conditions be detected in any of the standard demo tasks.
\r
75 * The toggle rate increasing to 500ms indicates that at least one error has
\r
78 * Main.c includes an idle hook function that simply periodically sends data
\r
79 * to the USB task for transmission.
\r
85 + Modified the stack sizes used by some tasks to permit use of the
\r
86 command line GCC tools.
\r
89 /* Library includes. */
\r
93 /* Scheduler includes. */
\r
94 #include "FreeRTOS.h"
\r
97 /* Demo application includes. */
\r
98 #include "partest.h"
\r
100 #include "semtest.h"
\r
102 #include "integer.h"
\r
103 #include "BlockQ.h"
\r
104 #include "BasicWEB.h"
\r
105 #include "USB-CDC.h"
\r
107 /* lwIP includes. */
\r
108 #include "lwip/api.h"
\r
110 /* Hardware specific headers. */
\r
112 #include "AT91SAM7X256.h"
\r
114 /* Priorities/stacks for the various tasks within the demo application. */
\r
115 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
117 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
118 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
120 #define mainWEBSERVER_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
121 #define mainUSB_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
122 #define mainUSB_TASK_STACK ( 200 )
\r
124 /* The rate at which the on board LED will toggle when there is/is not an
\r
126 #define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
127 #define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
129 /* The rate at which the idle hook sends data to the USB port. */
\r
130 #define mainUSB_TX_FREQUENCY ( 100 / portTICK_RATE_MS )
\r
132 /* The string that is transmitted down the USB port. */
\r
133 #define mainFIRST_TX_CHAR 'a'
\r
134 #define mainLAST_TX_CHAR 'z'
\r
136 /* The LED used by the check task to indicate the system status. */
\r
137 #define mainCHECK_LED ( 3 )
\r
138 /*-----------------------------------------------------------*/
\r
141 * Checks that all the demo application tasks are still executing without error
\r
142 * - as described at the top of the file.
\r
144 static portLONG prvCheckOtherTasksAreStillRunning( void );
\r
147 * The task that executes at the highest priority and calls
\r
148 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
151 static void vErrorChecks( void *pvParameters );
\r
154 * Configure the processor for use with the Atmel demo board. This is very
\r
155 * minimal as most of the setup is performed in the startup code.
\r
157 static void prvSetupHardware( void );
\r
160 * The idle hook is just used to stream data to the USB port.
\r
162 void vApplicationIdleHook( void );
\r
163 /*-----------------------------------------------------------*/
\r
166 * Setup hardware then start all the demo application tasks.
\r
170 /* Setup the ports. */
\r
171 prvSetupHardware();
\r
173 /* Setup the IO required for the LED's. */
\r
174 vParTestInitialise();
\r
179 /* Create the lwIP task. This uses the lwIP RTOS abstraction layer.*/
\r
180 sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );
\r
182 /* Create the demo USB CDC task. */
\r
183 xTaskCreate( vUSBCDCTask, ( signed portCHAR * ) "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );
\r
185 /* Create the standard demo application tasks. */
\r
186 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
187 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
188 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
189 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
190 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
192 /* Start the check task - which is defined in this file. */
\r
193 xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
195 /* Finally, start the scheduler.
\r
197 NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
\r
198 The processor MUST be in supervisor mode when vTaskStartScheduler is
\r
199 called. The demo applications included in the FreeRTOS.org download switch
\r
200 to supervisor mode prior to main being called. If you are not using one of
\r
201 these demo application projects then ensure Supervisor mode is used here. */
\r
202 vTaskStartScheduler();
\r
204 /* Should never get here! */
\r
207 /*-----------------------------------------------------------*/
\r
210 static void prvSetupHardware( void )
\r
212 /* When using the JTAG debugger the hardware is not always initialised to
\r
213 the correct default state. This line just ensures that this does not
\r
214 cause all interrupts to be masked at the start. */
\r
215 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
217 /* Most setup is performed by the low level init function called from the
\r
220 Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as
\r
221 well as the UART Tx line. */
\r
222 AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode
\r
223 AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output
\r
226 /* Enable the peripheral clock. */
\r
227 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
\r
228 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
\r
229 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
\r
231 /*-----------------------------------------------------------*/
\r
233 static void vErrorChecks( void *pvParameters )
\r
235 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
\r
236 portTickType xLastWakeTime;
\r
238 /* The parameters are not used. */
\r
239 ( void ) pvParameters;
\r
241 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
242 functions correctly. */
\r
243 xLastWakeTime = xTaskGetTickCount();
\r
245 /* Cycle for ever, delaying then checking all the other tasks are still
\r
246 operating without error. If an error is detected then the delay period
\r
247 is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
\r
248 the Check LED flash rate will increase. */
\r
251 /* Delay until it is time to execute again. The delay period is
\r
252 shorter following an error. */
\r
253 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
255 /* Check all the standard demo application tasks are executing without
\r
257 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
259 /* An error has been detected in one of the tasks - flash faster. */
\r
260 xDelayPeriod = mainERROR_FLASH_PERIOD;
\r
263 vParTestToggleLED( mainCHECK_LED );
\r
266 /*-----------------------------------------------------------*/
\r
268 static portLONG prvCheckOtherTasksAreStillRunning( void )
\r
270 portLONG lReturn = ( portLONG ) pdPASS;
\r
272 /* Check all the demo tasks (other than the flash tasks) to ensure
\r
273 that they are all still running, and that none of them have detected
\r
276 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
278 lReturn = ( portLONG ) pdFAIL;
\r
281 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
283 lReturn = ( portLONG ) pdFAIL;
\r
286 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
288 lReturn = ( portLONG ) pdFAIL;
\r
291 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
293 lReturn = ( portLONG ) pdFAIL;
\r
298 /*-----------------------------------------------------------*/
\r
300 void vApplicationIdleHook( void )
\r
302 static portTickType xLastTx = 0;
\r
305 /* The idle hook simply sends a string of characters to the USB port.
\r
306 The characters will be buffered and sent once the port is connected. */
\r
307 if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )
\r
309 xLastTx = xTaskGetTickCount();
\r
310 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )
\r
312 vUSBSendByte( cTxByte );
\r