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