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