]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
30         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
31         called.  The demo applications included in the FreeRTOS.org download switch\r
32         to supervisor mode prior to main being called.  If you are not using one of\r
33         these demo application projects then ensure Supervisor mode is used.\r
34 */\r
35 \r
36 \r
37 /*\r
38  * Creates all the application tasks, then starts the scheduler.\r
39  *\r
40  * A task defined by the function vBasicWEBServer is created.  This executes\r
41  * the lwIP stack and basic WEB server sample.  A task defined by the function\r
42  * vUSBCDCTask.  This executes the USB to serial CDC example.  All the other\r
43  * tasks are from the set of standard demo tasks.  The WEB documentation\r
44  * provides more details of the standard demo application tasks.\r
45  *\r
46  * Main.c also creates a task called "Check".  This only executes every three\r
47  * seconds but has the highest priority so is guaranteed to get processor time.\r
48  * Its main function is to check the status of all the other demo application\r
49  * tasks.  LED mainCHECK_LED is toggled every three seconds by the check task\r
50  * should no error conditions be detected in any of the standard demo tasks.\r
51  * The toggle rate increasing to 500ms indicates that at least one error has\r
52  * been detected.\r
53  *\r
54  * Main.c includes an idle hook function that simply periodically sends data\r
55  * to the USB task for transmission.\r
56  */\r
57 \r
58 /*\r
59         Changes from V3.2.2\r
60 \r
61         + Modified the stack sizes used by some tasks to permit use of the\r
62           command line GCC tools.\r
63 */\r
64 \r
65 /* Library includes. */\r
66 #include <string.h>\r
67 #include <stdio.h>\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* Demo application includes. */\r
74 #include "partest.h"\r
75 #include "PollQ.h"\r
76 #include "semtest.h"\r
77 #include "flash.h"\r
78 #include "integer.h"\r
79 #include "BlockQ.h"\r
80 #include "BasicWEB.h"\r
81 #include "USB-CDC.h"\r
82 \r
83 /* lwIP includes. */\r
84 #include "lwip/api.h"\r
85 \r
86 /* Hardware specific headers. */\r
87 #include "Board.h"\r
88 #include "AT91SAM7X256.h"\r
89 \r
90 /* Priorities/stacks for the various tasks within the demo application. */\r
91 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
92 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
93 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
94 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
95 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
96 #define mainWEBSERVER_PRIORITY      ( tskIDLE_PRIORITY + 2 )\r
97 #define mainUSB_PRIORITY                        ( tskIDLE_PRIORITY + 1 )\r
98 #define mainUSB_TASK_STACK                      ( 200 )\r
99 \r
100 /* The rate at which the on board LED will toggle when there is/is not an\r
101 error. */\r
102 #define mainNO_ERROR_FLASH_PERIOD       ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
103 #define mainERROR_FLASH_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS  )\r
104 \r
105 /* The rate at which the idle hook sends data to the USB port. */\r
106 #define mainUSB_TX_FREQUENCY            ( 100 / portTICK_PERIOD_MS )\r
107 \r
108 /* The string that is transmitted down the USB port. */\r
109 #define mainFIRST_TX_CHAR                       'a'\r
110 #define mainLAST_TX_CHAR                        'z'\r
111 \r
112 /* The LED used by the check task to indicate the system status. */\r
113 #define mainCHECK_LED                           ( 3 )\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * Checks that all the demo application tasks are still executing without error\r
118  * - as described at the top of the file.\r
119  */\r
120 static long prvCheckOtherTasksAreStillRunning( void );\r
121 \r
122 /*\r
123  * The task that executes at the highest priority and calls\r
124  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
125  * of the file.\r
126  */\r
127 static void vErrorChecks( void *pvParameters );\r
128 \r
129 /*\r
130  * Configure the processor for use with the Atmel demo board.  This is very\r
131  * minimal as most of the setup is performed in the startup code.\r
132  */\r
133 static void prvSetupHardware( void );\r
134 \r
135 /*\r
136  * The idle hook is just used to stream data to the USB port.\r
137  */\r
138 void vApplicationIdleHook( void );\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * Setup hardware then start all the demo application tasks.\r
143  */\r
144 int main( void )\r
145 {\r
146         /* Setup the ports. */\r
147         prvSetupHardware();\r
148 \r
149         /* Setup the IO required for the LED's. */\r
150         vParTestInitialise();\r
151 \r
152         /* Setup lwIP. */\r
153     vlwIPInit();\r
154 \r
155         /* Create the lwIP task.  This uses the lwIP RTOS abstraction layer.*/\r
156     sys_thread_new( vBasicWEBServer, ( void * ) NULL, mainWEBSERVER_PRIORITY );\r
157 \r
158         /* Create the demo USB CDC task. */\r
159         xTaskCreate( vUSBCDCTask, "USB", mainUSB_TASK_STACK, NULL, mainUSB_PRIORITY, NULL );\r
160 \r
161         /* Create the standard demo application tasks. */\r
162         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
163         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
164         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
165         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
166         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
167 \r
168         /* Start the check task - which is defined in this file. */\r
169     xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
170 \r
171         /* Finally, start the scheduler.\r
172 \r
173         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
174         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
175         called.  The demo applications included in the FreeRTOS.org download switch\r
176         to supervisor mode prior to main being called.  If you are not using one of\r
177         these demo application projects then ensure Supervisor mode is used here. */\r
178         vTaskStartScheduler();\r
179 \r
180         /* Should never get here! */\r
181         return 0;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 \r
186 static void prvSetupHardware( void )\r
187 {\r
188         /* When using the JTAG debugger the hardware is not always initialised to\r
189         the correct default state.  This line just ensures that this does not\r
190         cause all interrupts to be masked at the start. */\r
191         AT91C_BASE_AIC->AIC_EOICR = 0;\r
192 \r
193         /* Most setup is performed by the low level init function called from the\r
194         startup asm file.\r
195 \r
196         Configure the PIO Lines corresponding to LED1 to LED4 to be outputs as\r
197         well as the UART Tx line. */\r
198         AT91C_BASE_PIOB->PIO_PER = LED_MASK; // Set in PIO mode\r
199         AT91C_BASE_PIOB->PIO_OER = LED_MASK; // Configure in Output\r
200 \r
201 \r
202         /* Enable the peripheral clock. */\r
203     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;\r
204     AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;\r
205         AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void vErrorChecks( void *pvParameters )\r
210 {\r
211 TickType_t xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;\r
212 TickType_t xLastWakeTime;\r
213 \r
214         /* The parameters are not used. */\r
215         ( void ) pvParameters;\r
216 \r
217         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
218         functions correctly. */\r
219         xLastWakeTime = xTaskGetTickCount();\r
220 \r
221         /* Cycle for ever, delaying then checking all the other tasks are still\r
222         operating without error.  If an error is detected then the delay period\r
223         is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so\r
224         the Check LED flash rate will increase. */\r
225         for( ;; )\r
226         {\r
227                 /* Delay until it is time to execute again.  The delay period is\r
228                 shorter following an error. */\r
229                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
230 \r
231                 /* Check all the standard demo application tasks are executing without\r
232                 error.  */\r
233                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
234                 {\r
235                         /* An error has been detected in one of the tasks - flash faster. */\r
236                         xDelayPeriod = mainERROR_FLASH_PERIOD;\r
237                 }\r
238 \r
239                 vParTestToggleLED( mainCHECK_LED );\r
240         }\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static long prvCheckOtherTasksAreStillRunning( void )\r
245 {\r
246 long lReturn = ( long ) pdPASS;\r
247 \r
248         /* Check all the demo tasks (other than the flash tasks) to ensure\r
249         that they are all still running, and that none of them have detected\r
250         an error. */\r
251 \r
252         if( xArePollingQueuesStillRunning() != pdTRUE )\r
253         {\r
254                 lReturn = ( long ) pdFAIL;\r
255         }\r
256 \r
257         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
258         {\r
259                 lReturn = ( long ) pdFAIL;\r
260         }\r
261 \r
262         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
263         {\r
264                 lReturn = ( long ) pdFAIL;\r
265         }\r
266 \r
267         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
268         {\r
269                 lReturn = ( long ) pdFAIL;\r
270         }\r
271 \r
272         return lReturn;\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 void vApplicationIdleHook( void )\r
277 {\r
278 static TickType_t xLastTx = 0;\r
279 char cTxByte;\r
280 \r
281         /* The idle hook simply sends a string of characters to the USB port.\r
282         The characters will be buffered and sent once the port is connected. */\r
283         if( ( xTaskGetTickCount() - xLastTx ) > mainUSB_TX_FREQUENCY )\r
284         {\r
285                 xLastTx = xTaskGetTickCount();\r
286                 for( cTxByte = mainFIRST_TX_CHAR; cTxByte <= mainLAST_TX_CHAR; cTxByte++ )\r
287                 {\r
288                         vUSBSendByte( cTxByte );\r
289                 }\r
290         }\r
291 }\r
292 \r
293 \r