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