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