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