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