]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/main.c
Ready for V5.2.0 release.
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2         FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \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
9 \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
13         more details.\r
14 \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
18 \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
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\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
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* \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
58 */\r
59 \r
60 \r
61 /*\r
62  * Creates all the application tasks, then starts the scheduler.\r
63  *\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
69  *\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
76  * been detected.\r
77  *\r
78  * Main.c includes an idle hook function that simply periodically sends data\r
79  * to the USB task for transmission.\r
80  */\r
81 \r
82 /*\r
83         Changes from V3.2.2\r
84 \r
85         + Modified the stack sizes used by some tasks to permit use of the \r
86           command line GCC tools.\r
87 */\r
88 \r
89 /* Library includes. */\r
90 #include <string.h>\r
91 #include <stdio.h>\r
92 \r
93 /* Scheduler includes. */\r
94 #include "FreeRTOS.h"\r
95 #include "task.h"\r
96 \r
97 /* Demo application includes. */\r
98 #include "partest.h"\r
99 #include "PollQ.h"\r
100 #include "semtest.h"\r
101 #include "flash.h"\r
102 #include "integer.h"\r
103 #include "BlockQ.h"\r
104 #include "BasicWEB.h"\r
105 #include "USB-CDC.h"\r
106 \r
107 /* lwIP includes. */\r
108 #include "lwip/api.h" \r
109 \r
110 /* Hardware specific headers. */\r
111 #include "Board.h"\r
112 #include "AT91SAM7X256.h"\r
113 \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
123 \r
124 /* The rate at which the on board LED will toggle when there is/is not an\r
125 error. */\r
126 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
127 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
128 \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
131 \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
135 \r
136 /* The LED used by the check task to indicate the system status. */\r
137 #define mainCHECK_LED                           ( 3 )\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 /*\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
143  */\r
144 static portLONG prvCheckOtherTasksAreStillRunning( void );\r
145 \r
146 /*\r
147  * The task that executes at the highest priority and calls\r
148  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
149  * of the file.\r
150  */\r
151 static void vErrorChecks( void *pvParameters );\r
152 \r
153 /*\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
156  */\r
157 static void prvSetupHardware( void );\r
158 \r
159 /*\r
160  * The idle hook is just used to stream data to the USB port.\r
161  */\r
162 void vApplicationIdleHook( void );\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 /*\r
166  * Setup hardware then start all the demo application tasks.\r
167  */\r
168 int main( void )\r
169 {\r
170         /* Setup the ports. */\r
171         prvSetupHardware();\r
172 \r
173         /* Setup the IO required for the LED's. */\r
174         vParTestInitialise();\r
175 \r
176         /* Setup lwIP. */\r
177     vlwIPInit();\r
178 \r
179         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
180     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );\r
181 \r
182         /* Create the demo USB CDC task. */\r
183         xTaskCreate( vUSBCDCTask, ( signed portCHAR * ) "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );\r
184 \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
191 \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
194 \r
195         /* Finally, start the scheduler. \r
196 \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
203 \r
204         /* Should never get here! */\r
205         return 0;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 \r
210 static void prvSetupHardware( void )\r
211 {\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
216         \r
217         /* Most setup is performed by the low level init function called from the\r
218         startup asm file.\r
219 \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
224 \r
225 \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
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void vErrorChecks( void *pvParameters )\r
234 {\r
235 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
236 portTickType xLastWakeTime;\r
237 \r
238         /* The parameters are not used. */\r
239         ( void ) pvParameters;\r
240 \r
241         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
242         functions correctly. */\r
243         xLastWakeTime = xTaskGetTickCount();\r
244 \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
249         for( ;; )\r
250         {\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
254         \r
255                 /* Check all the standard demo application tasks are executing without\r
256                 error.  */\r
257                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
258                 {\r
259                         /* An error has been detected in one of the tasks - flash faster. */\r
260                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
261                 }\r
262 \r
263                 vParTestToggleLED( mainCHECK_LED );\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 static portLONG prvCheckOtherTasksAreStillRunning( void )\r
269 {\r
270 portLONG lReturn = ( portLONG ) pdPASS;\r
271 \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
274         an error. */\r
275 \r
276         if( xArePollingQueuesStillRunning() != pdTRUE )\r
277         {\r
278                 lReturn = ( portLONG ) pdFAIL;\r
279         }\r
280 \r
281         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
282         {\r
283                 lReturn = ( portLONG ) pdFAIL;\r
284         }\r
285 \r
286         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
287         {\r
288                 lReturn = ( portLONG ) pdFAIL;\r
289         }\r
290 \r
291         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
292         {\r
293                 lReturn = ( portLONG ) pdFAIL;\r
294         }\r
295 \r
296         return lReturn;\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 void vApplicationIdleHook( void )\r
301 {\r
302 static portTickType xLastTx = 0;\r
303 portCHAR cTxByte;\r
304 \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
308         {\r
309                 xLastTx = xTaskGetTickCount();\r
310                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )\r
311                 {\r
312                         vUSBSendByte( cTxByte );\r
313                 }               \r
314         }\r
315 }\r
316 \r
317 \r