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