]> git.sur5r.net Git - freertos/blob - Demo/lwIP_Demo_Rowley_ARM7/main.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@261 1d2547de-c912-0410...
[freertos] / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2         FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 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\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27 \r
28         Please ensure to read the configuration and relevant port sections of the \r
29         online documentation.\r
30 \r
31         +++ http://www.FreeRTOS.org +++\r
32         Documentation, latest information, license and contact details.  \r
33 \r
34         +++ http://www.SafeRTOS.com +++\r
35         A version that is certified for use in safety critical systems.\r
36 \r
37         +++ http://www.OpenRTOS.com +++\r
38         Commercial support, development, porting, licensing and training services.\r
39 \r
40         ***************************************************************************\r
41 */\r
42 \r
43 /* \r
44         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
45         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
46         called.  The demo applications included in the FreeRTOS.org download switch\r
47         to supervisor mode prior to main being called.  If you are not using one of\r
48         these demo application projects then ensure Supervisor mode is used.\r
49 */\r
50 \r
51 \r
52 /*\r
53  * Creates all the application tasks, then starts the scheduler.\r
54  *\r
55  * A task defined by the function vBasicWEBServer is created.  This executes \r
56  * the lwIP stack and basic WEB server sample.  A task defined by the function\r
57  * vUSBCDCTask.  This executes the USB to serial CDC example.  All the other \r
58  * tasks are from the set of standard demo tasks.  The WEB documentation \r
59  * provides more details of the standard demo application tasks.\r
60  *\r
61  * Main.c also creates a task called "Check".  This only executes every three\r
62  * seconds but has the highest priority so is guaranteed to get processor time.\r
63  * Its main function is to check the status of all the other demo application\r
64  * tasks.  LED mainCHECK_LED is toggled every three seconds by the check task\r
65  * should no error conditions be detected in any of the standard demo tasks.\r
66  * The toggle rate increasing to 500ms indicates that at least one error has\r
67  * been detected.\r
68  *\r
69  * Main.c includes an idle hook function that simply periodically sends data\r
70  * to the USB task for transmission.\r
71  */\r
72 \r
73 /*\r
74         Changes from V3.2.2\r
75 \r
76         + Modified the stack sizes used by some tasks to permit use of the \r
77           command line GCC tools.\r
78 */\r
79 \r
80 /* Library includes. */\r
81 #include <string.h>\r
82 #include <stdio.h>\r
83 \r
84 /* Scheduler includes. */\r
85 #include "FreeRTOS.h"\r
86 #include "task.h"\r
87 \r
88 /* Demo application includes. */\r
89 #include "partest.h"\r
90 #include "PollQ.h"\r
91 #include "semtest.h"\r
92 #include "flash.h"\r
93 #include "integer.h"\r
94 #include "BlockQ.h"\r
95 #include "BasicWEB.h"\r
96 #include "USB-CDC.h"\r
97 \r
98 /* lwIP includes. */\r
99 #include "lwip/api.h" \r
100 \r
101 /* Hardware specific headers. */\r
102 #include "Board.h"\r
103 #include "AT91SAM7X256.h"\r
104 \r
105 /* Priorities/stacks for the various tasks within the demo application. */\r
106 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
107 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
108 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
109 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
110 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
111 #define mainWEBSERVER_PRIORITY      ( tskIDLE_PRIORITY + 2 )\r
112 #define mainUSB_PRIORITY                        ( tskIDLE_PRIORITY + 1 )\r
113 #define mainUSB_TASK_STACK                      ( 200 )\r
114 \r
115 /* The rate at which the on board LED will toggle when there is/is not an\r
116 error. */\r
117 #define mainNO_ERROR_FLASH_PERIOD       ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
118 #define mainERROR_FLASH_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
119 \r
120 /* The rate at which the idle hook sends data to the USB port. */\r
121 #define mainUSB_TX_FREQUENCY            ( 100 / portTICK_RATE_MS )\r
122 \r
123 /* The string that is transmitted down the USB port. */\r
124 #define mainFIRST_TX_CHAR                       'a'\r
125 #define mainLAST_TX_CHAR                        'z'\r
126 \r
127 /* The LED used by the check task to indicate the system status. */\r
128 #define mainCHECK_LED                           ( 3 )\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * Checks that all the demo application tasks are still executing without error\r
133  * - as described at the top of the file.\r
134  */\r
135 static portLONG prvCheckOtherTasksAreStillRunning( void );\r
136 \r
137 /*\r
138  * The task that executes at the highest priority and calls\r
139  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
140  * of the file.\r
141  */\r
142 static void vErrorChecks( void *pvParameters );\r
143 \r
144 /*\r
145  * Configure the processor for use with the Atmel demo board.  This is very\r
146  * minimal as most of the setup is performed in the startup code.\r
147  */\r
148 static void prvSetupHardware( void );\r
149 \r
150 /*\r
151  * The idle hook is just used to stream data to the USB port.\r
152  */\r
153 void vApplicationIdleHook( void );\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * Setup hardware then start all the demo application tasks.\r
158  */\r
159 int main( void )\r
160 {\r
161         /* Setup the ports. */\r
162         prvSetupHardware();\r
163 \r
164         /* Setup the IO required for the LED's. */\r
165         vParTestInitialise();\r
166 \r
167         /* Setup lwIP. */\r
168     vlwIPInit();\r
169 \r
170         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
171     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );\r
172 \r
173         /* Create the demo USB CDC task. */\r
174         xTaskCreate( vUSBCDCTask, ( signed portCHAR * ) "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );\r
175 \r
176         /* Create the standard demo application tasks. */\r
177         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
178         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
179         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
180         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
181         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
182 \r
183         /* Start the check task - which is defined in this file. */     \r
184     xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
185 \r
186         /* Finally, start the scheduler. \r
187 \r
188         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
189         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
190         called.  The demo applications included in the FreeRTOS.org download switch\r
191         to supervisor mode prior to main being called.  If you are not using one of\r
192         these demo application projects then ensure Supervisor mode is used here. */\r
193         vTaskStartScheduler();\r
194 \r
195         /* Should never get here! */\r
196         return 0;\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 \r
201 static void prvSetupHardware( void )\r
202 {\r
203         /* When using the JTAG debugger the hardware is not always initialised to\r
204         the correct default state.  This line just ensures that this does not\r
205         cause all interrupts to be masked at the start. */\r
206         AT91C_BASE_AIC->AIC_EOICR = 0;\r
207         \r
208         /* Most setup is performed by the low level init function called from the\r
209         startup asm file.\r
210 \r
211         Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as\r
212         well as the UART Tx line. */\r
213         AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode\r
214         AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output\r
215 \r
216 \r
217         /* Enable the peripheral clock. */\r
218     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;\r
219     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;\r
220         AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void vErrorChecks( void *pvParameters )\r
225 {\r
226 portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
227 portTickType xLastWakeTime;\r
228 \r
229         /* The parameters are not used. */\r
230         ( void ) pvParameters;\r
231 \r
232         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
233         functions correctly. */\r
234         xLastWakeTime = xTaskGetTickCount();\r
235 \r
236         /* Cycle for ever, delaying then checking all the other tasks are still\r
237         operating without error.  If an error is detected then the delay period\r
238         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
239         the Check LED flash rate will increase. */\r
240         for( ;; )\r
241         {\r
242                 /* Delay until it is time to execute again.  The delay period is\r
243                 shorter following an error. */\r
244                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
245         \r
246                 /* Check all the standard demo application tasks are executing without\r
247                 error.  */\r
248                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
249                 {\r
250                         /* An error has been detected in one of the tasks - flash faster. */\r
251                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
252                 }\r
253 \r
254                 vParTestToggleLED( mainCHECK_LED );\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static portLONG prvCheckOtherTasksAreStillRunning( void )\r
260 {\r
261 portLONG lReturn = ( portLONG ) pdPASS;\r
262 \r
263         /* Check all the demo tasks (other than the flash tasks) to ensure\r
264         that they are all still running, and that none of them have detected\r
265         an error. */\r
266 \r
267         if( xArePollingQueuesStillRunning() != pdTRUE )\r
268         {\r
269                 lReturn = ( portLONG ) pdFAIL;\r
270         }\r
271 \r
272         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
273         {\r
274                 lReturn = ( portLONG ) pdFAIL;\r
275         }\r
276 \r
277         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
278         {\r
279                 lReturn = ( portLONG ) pdFAIL;\r
280         }\r
281 \r
282         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
283         {\r
284                 lReturn = ( portLONG ) pdFAIL;\r
285         }\r
286 \r
287         return lReturn;\r
288 }\r
289 /*-----------------------------------------------------------*/\r
290 \r
291 void vApplicationIdleHook( void )\r
292 {\r
293 static portTickType xLastTx = 0;\r
294 portCHAR cTxByte;\r
295 \r
296         /* The idle hook simply sends a string of characters to the USB port.\r
297         The characters will be buffered and sent once the port is connected. */\r
298         if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )\r
299         {\r
300                 xLastTx = xTaskGetTickCount();\r
301                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )\r
302                 {\r
303                         vUSBSendByte( cTxByte );\r
304                 }               \r
305         }\r
306 }\r
307 \r
308 \r