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