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