]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/main.c
94564829d369b5ad1945e81e111a032dbef20d9c
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2     FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\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
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\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
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* \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
60 */\r
61 \r
62 \r
63 /*\r
64  * Creates all the application tasks, then starts the scheduler.\r
65  *\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
71  *\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
78  * been detected.\r
79  *\r
80  * Main.c includes an idle hook function that simply periodically sends data\r
81  * to the USB task for transmission.\r
82  */\r
83 \r
84 /*\r
85         Changes from V3.2.2\r
86 \r
87         + Modified the stack sizes used by some tasks to permit use of the \r
88           command line GCC tools.\r
89 */\r
90 \r
91 /* Library includes. */\r
92 #include <string.h>\r
93 #include <stdio.h>\r
94 \r
95 /* Scheduler includes. */\r
96 #include "FreeRTOS.h"\r
97 #include "task.h"\r
98 \r
99 /* Demo application includes. */\r
100 #include "partest.h"\r
101 #include "PollQ.h"\r
102 #include "semtest.h"\r
103 #include "flash.h"\r
104 #include "integer.h"\r
105 #include "BlockQ.h"\r
106 #include "BasicWEB.h"\r
107 #include "USB-CDC.h"\r
108 \r
109 /* lwIP includes. */\r
110 #include "lwip/api.h" \r
111 \r
112 /* Hardware specific headers. */\r
113 #include "Board.h"\r
114 #include "AT91SAM7X256.h"\r
115 \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
125 \r
126 /* The rate at which the on board LED will toggle when there is/is not an\r
127 error. */\r
128 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
129 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
130 \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
133 \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
137 \r
138 /* The LED used by the check task to indicate the system status. */\r
139 #define mainCHECK_LED                           ( 3 )\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /*\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
145  */\r
146 static long prvCheckOtherTasksAreStillRunning( void );\r
147 \r
148 /*\r
149  * The task that executes at the highest priority and calls\r
150  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
151  * of the file.\r
152  */\r
153 static void vErrorChecks( void *pvParameters );\r
154 \r
155 /*\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
158  */\r
159 static void prvSetupHardware( void );\r
160 \r
161 /*\r
162  * The idle hook is just used to stream data to the USB port.\r
163  */\r
164 void vApplicationIdleHook( void );\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /*\r
168  * Setup hardware then start all the demo application tasks.\r
169  */\r
170 int main( void )\r
171 {\r
172         /* Setup the ports. */\r
173         prvSetupHardware();\r
174 \r
175         /* Setup the IO required for the LED's. */\r
176         vParTestInitialise();\r
177 \r
178         /* Setup lwIP. */\r
179     vlwIPInit();\r
180 \r
181         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
182     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );\r
183 \r
184         /* Create the demo USB CDC task. */\r
185         xTaskCreate( vUSBCDCTask, ( signed char * ) "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );\r
186 \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
193 \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
196 \r
197         /* Finally, start the scheduler. \r
198 \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
205 \r
206         /* Should never get here! */\r
207         return 0;\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 \r
212 static void prvSetupHardware( void )\r
213 {\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
218         \r
219         /* Most setup is performed by the low level init function called from the\r
220         startup asm file.\r
221 \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
226 \r
227 \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
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 static void vErrorChecks( void *pvParameters )\r
236 {\r
237 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
238 portTickType xLastWakeTime;\r
239 \r
240         /* The parameters are not used. */\r
241         ( void ) pvParameters;\r
242 \r
243         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
244         functions correctly. */\r
245         xLastWakeTime = xTaskGetTickCount();\r
246 \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
251         for( ;; )\r
252         {\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
256         \r
257                 /* Check all the standard demo application tasks are executing without\r
258                 error.  */\r
259                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
260                 {\r
261                         /* An error has been detected in one of the tasks - flash faster. */\r
262                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
263                 }\r
264 \r
265                 vParTestToggleLED( mainCHECK_LED );\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 static long prvCheckOtherTasksAreStillRunning( void )\r
271 {\r
272 long lReturn = ( long ) pdPASS;\r
273 \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
276         an error. */\r
277 \r
278         if( xArePollingQueuesStillRunning() != pdTRUE )\r
279         {\r
280                 lReturn = ( long ) pdFAIL;\r
281         }\r
282 \r
283         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
284         {\r
285                 lReturn = ( long ) pdFAIL;\r
286         }\r
287 \r
288         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
289         {\r
290                 lReturn = ( long ) pdFAIL;\r
291         }\r
292 \r
293         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
294         {\r
295                 lReturn = ( long ) pdFAIL;\r
296         }\r
297 \r
298         return lReturn;\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 void vApplicationIdleHook( void )\r
303 {\r
304 static portTickType xLastTx = 0;\r
305 char cTxByte;\r
306 \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
310         {\r
311                 xLastTx = xTaskGetTickCount();\r
312                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )\r
313                 {\r
314                         vUSBSendByte( cTxByte );\r
315                 }               \r
316         }\r
317 }\r
318 \r
319 \r