]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_GCC_ARM7/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / WizNET_DEMO_GCC_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  * Program entry point.\r
39  *\r
40  * main() is responsible for setting up the microcontroller peripherals, then\r
41  * starting the demo application tasks.  Once the tasks have been created the\r
42  * scheduler is started and main() should never complete.\r
43  *\r
44  * The demo creates the three standard 'flash' tasks to provide some visual\r
45  * feedback that the system and scheduler are still operating correctly.\r
46  *\r
47  * The HTTP server task operates at the highest priority so will always preempt\r
48  * the flash or idle task on TCP/IP events.\r
49  */\r
50 \r
51 /* Standard includes. */\r
52 #include <stdlib.h>\r
53 \r
54 /* Scheduler include files. */\r
55 #include "FreeRTOS.h"\r
56 #include "semphr.h"\r
57 #include "task.h"\r
58 \r
59 /* Application includes. */\r
60 #include "i2c.h"\r
61 #include "HTTP_Serv.h"\r
62 #include "flash.h"\r
63 #include "partest.h"\r
64 #include "dynamic.h"\r
65 #include "semtest.h"\r
66 #include "PollQ.h"\r
67 #include "BlockQ.h"\r
68 #include "integer.h"\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /* Constants to setup the PLL. */\r
73 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
74 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
75 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
76 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
77 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
78 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
79 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
80 \r
81 /* Constants to setup the MAM. */\r
82 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
83 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
84 \r
85 /* Constants to setup the peripheral bus. */\r
86 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
87 \r
88 /* Constants to setup I/O and processor. */\r
89 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
90 #define mainLED_TO_OUTPUT       ( ( unsigned long ) 0xff0000 )\r
91 #define mainJTAG_PORT           ( ( unsigned long ) 0x3E0000UL )\r
92 \r
93 /* Priorities for the demo application tasks. */\r
94 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
95 #define mainHTTP_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
96 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
97 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
98 #define mainERROR_CHECK_PRIORITY        ( tskIDLE_PRIORITY + 1 )\r
99 \r
100 /* Flash rates of the on board LED to indicate the health of the system. */\r
101 #define mainNO_ERROR_DELAY                      ( 3000 )\r
102 #define mainERROR_DELAY                         ( 500 )\r
103 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /*\r
108  * The Olimex demo board has a single built in LED.  This function simply\r
109  * toggles its state.\r
110  */\r
111 void prvToggleOnBoardLED( void );\r
112 \r
113 /*\r
114  * Configure the processor for use with the Olimex demo board.\r
115  */\r
116 static void prvSetupHardware( void );\r
117 \r
118 /*\r
119  * Simply check for errors and toggle the onboard LED.\r
120  */\r
121 static void prvErrorChecks( void *pvParameters );\r
122 \r
123 /*\r
124  * Return true if the demo tasks are executing without error - otherwise\r
125  * return false.\r
126  */\r
127 static void prvMainCheckOtherTasksAreStillRunning( void );\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* Flag set by prvMainCheckOtherTasksAreStillExecuting(). */\r
131 long lErrorInTask = pdFALSE;\r
132 \r
133 /*\r
134  * Application entry point:\r
135  * Starts all the other tasks, then starts the scheduler.\r
136  */\r
137 int main( void )\r
138 {\r
139         /* Setup the hardware for use with the Olimex demo board. */\r
140         prvSetupHardware();\r
141 \r
142         /* Start the standard flash tasks so the WEB server is not the only thing\r
143         running. */\r
144         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
145         vStartSemaphoreTasks( tskIDLE_PRIORITY );\r
146         vStartDynamicPriorityTasks();\r
147         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
148         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
149         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
150 \r
151         /* Start the WEB server task and the error check task. */\r
152         xTaskCreate( vHTTPServerTask, "HTTP", configMINIMAL_STACK_SIZE, NULL, mainHTTP_TASK_PRIORITY, NULL );\r
153         xTaskCreate( prvErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainERROR_CHECK_PRIORITY, NULL );\r
154 \r
155         /* Now all the tasks have been started - start the scheduler.\r
156 \r
157         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
158         The processor MUST be in supervisor mode when vTaskStartScheduler is\r
159         called.  The demo applications included in the FreeRTOS.org download switch\r
160         to supervisor mode prior to main being called.  If you are not using one of\r
161         these demo application projects then ensure Supervisor mode is used. */\r
162         vTaskStartScheduler();\r
163 \r
164         /* Should never reach here! */\r
165         return 0;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 static void prvSetupHardware( void )\r
170 {\r
171         #ifdef RUN_FROM_RAM\r
172                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
173                 SCB_MEMMAP = 2;\r
174         #endif\r
175 \r
176         /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.\r
177         The JTAG pins are left as input as I'm not sure what will happen if the\r
178         Wiggler is connected after powerup - not that it would be a good idea to\r
179         do that anyway. */\r
180         GPIO_IODIR = ~( mainJTAG_PORT );\r
181 \r
182         /* Setup the PLL to multiply the XTAL input by 4. */\r
183         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
184 \r
185         /* Activate the PLL by turning it on then feeding the correct sequence of\r
186         bytes. */\r
187         SCB_PLLCON = mainPLL_ENABLE;\r
188         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
189         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
190 \r
191         /* Wait for the PLL to lock... */\r
192         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
193 \r
194         /* ...before connecting it using the feed sequence again. */\r
195         SCB_PLLCON = mainPLL_CONNECT;\r
196         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
197         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
198 \r
199         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
200         PLL used.  It is possible faster overall performance could be obtained by\r
201         tuning the MAM and PLL settings. */\r
202         MAM_TIM = mainMAM_TIM_3;\r
203         MAM_CR = mainMAM_MODE_FULL;\r
204 \r
205         /* Setup the peripheral bus to be the same as the PLL output. */\r
206         SCB_VPBDIV = mainBUS_CLK_FULL;\r
207 \r
208         /* Initialise the i2c peripheral. */\r
209         i2cInit();\r
210 \r
211         /* Initialise the LED's used by the flash tasks. */\r
212         vParTestInitialise();\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvMainCheckOtherTasksAreStillRunning( void )\r
217 {\r
218         /* Check all the demo tasks (other than the flash tasks) to ensure\r
219         that they are all still running, and that none of them have detected\r
220         an error. */\r
221 \r
222         /* This function is called from more than one task. */\r
223         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
224         {\r
225                 lErrorInTask = pdTRUE;\r
226         }\r
227 \r
228         if( xArePollingQueuesStillRunning() != pdTRUE )\r
229         {\r
230                 lErrorInTask = pdTRUE;\r
231         }\r
232 \r
233         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
234         {\r
235                 lErrorInTask = pdTRUE;\r
236         }\r
237 \r
238         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
239         {\r
240                 lErrorInTask = pdTRUE;\r
241         }\r
242 \r
243         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
244         {\r
245                 lErrorInTask = pdTRUE;\r
246         }\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 void prvToggleOnBoardLED( void )\r
251 {\r
252 unsigned long ulState;\r
253 \r
254         ulState = GPIO0_IOPIN;\r
255         if( ulState & mainON_BOARD_LED_BIT )\r
256         {\r
257                 GPIO_IOCLR = mainON_BOARD_LED_BIT;\r
258         }\r
259         else\r
260         {\r
261                 GPIO_IOSET = mainON_BOARD_LED_BIT;\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void prvErrorChecks( void *pvParameters )\r
267 {\r
268 TickType_t xDelay = mainNO_ERROR_DELAY;\r
269 \r
270         /* The parameters are not used. */\r
271         ( void ) pvParameters;\r
272 \r
273         for( ;; )\r
274         {\r
275                 /* How long we delay depends on whether an error has been detected\r
276                 or not.  Therefore the flash rate of the on board LED indicates\r
277                 whether or not an error has occurred. */\r
278                 vTaskDelay( xDelay );\r
279 \r
280                 /* Update the lErrorInTask flag. */\r
281                 prvMainCheckOtherTasksAreStillRunning();\r
282 \r
283                 if( lErrorInTask )\r
284                 {\r
285                         /* An error has been found so reduce the delay period and in so\r
286                         doing speed up the flash rate of the on board LED. */\r
287                         xDelay = mainERROR_DELAY;\r
288                 }\r
289 \r
290                 prvToggleOnBoardLED();\r
291         }\r
292 }\r
293 \r