]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_GCC_ARM7/main.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Demo / WizNET_DEMO_GCC_ARM7 / main.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* \r
66         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
67         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
68         called.  The demo applications included in the FreeRTOS.org download switch\r
69         to supervisor mode prior to main being called.  If you are not using one of\r
70         these demo application projects then ensure Supervisor mode is used.\r
71 */\r
72 \r
73 \r
74 /*\r
75  * Program entry point.\r
76  * \r
77  * main() is responsible for setting up the microcontroller peripherals, then\r
78  * starting the demo application tasks.  Once the tasks have been created the\r
79  * scheduler is started and main() should never complete.\r
80  *\r
81  * The demo creates the three standard 'flash' tasks to provide some visual\r
82  * feedback that the system and scheduler are still operating correctly.\r
83  *\r
84  * The HTTP server task operates at the highest priority so will always preempt\r
85  * the flash or idle task on TCP/IP events.\r
86  */\r
87 \r
88 /* Standard includes. */\r
89 #include <stdlib.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "semphr.h"\r
94 #include "task.h"\r
95 \r
96 /* Application includes. */\r
97 #include "i2c.h"\r
98 #include "HTTP_Serv.h"\r
99 #include "flash.h"\r
100 #include "partest.h"\r
101 #include "dynamic.h"\r
102 #include "semtest.h"\r
103 #include "PollQ.h"\r
104 #include "BlockQ.h"\r
105 #include "integer.h"\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* Constants to setup the PLL. */\r
110 #define mainPLL_MUL_4           ( ( unsigned char ) 0x0003 )\r
111 #define mainPLL_DIV_1           ( ( unsigned char ) 0x0000 )\r
112 #define mainPLL_ENABLE          ( ( unsigned char ) 0x0001 )\r
113 #define mainPLL_CONNECT         ( ( unsigned char ) 0x0003 )\r
114 #define mainPLL_FEED_BYTE1      ( ( unsigned char ) 0xaa )\r
115 #define mainPLL_FEED_BYTE2      ( ( unsigned char ) 0x55 )\r
116 #define mainPLL_LOCK            ( ( unsigned long ) 0x0400 )\r
117 \r
118 /* Constants to setup the MAM. */\r
119 #define mainMAM_TIM_3           ( ( unsigned char ) 0x03 )\r
120 #define mainMAM_MODE_FULL       ( ( unsigned char ) 0x02 )\r
121 \r
122 /* Constants to setup the peripheral bus. */\r
123 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
124 \r
125 /* Constants to setup I/O and processor. */\r
126 #define mainBUS_CLK_FULL        ( ( unsigned char ) 0x01 )\r
127 #define mainLED_TO_OUTPUT       ( ( unsigned long ) 0xff0000 )\r
128 #define mainJTAG_PORT           ( ( unsigned long ) 0x3E0000UL )\r
129 \r
130 /* Priorities for the demo application tasks. */\r
131 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
132 #define mainHTTP_TASK_PRIORITY          ( tskIDLE_PRIORITY + 2 )\r
133 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
134 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
135 #define mainERROR_CHECK_PRIORITY        ( tskIDLE_PRIORITY + 1 )\r
136 \r
137 /* Flash rates of the on board LED to indicate the health of the system. */\r
138 #define mainNO_ERROR_DELAY                      ( 3000 )\r
139 #define mainERROR_DELAY                         ( 500 )\r
140 #define mainON_BOARD_LED_BIT            ( ( unsigned long ) 0x80 )\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * The Olimex demo board has a single built in LED.  This function simply\r
146  * toggles its state. \r
147  */\r
148 void prvToggleOnBoardLED( void );\r
149 \r
150 /*\r
151  * Configure the processor for use with the Olimex demo board.\r
152  */\r
153 static void prvSetupHardware( void );\r
154 \r
155 /*\r
156  * Simply check for errors and toggle the onboard LED.\r
157  */\r
158 static void prvErrorChecks( void *pvParameters );\r
159 \r
160 /*\r
161  * Return true if the demo tasks are executing without error - otherwise \r
162  * return false.\r
163  */\r
164 static void prvMainCheckOtherTasksAreStillRunning( void );\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 /* Flag set by prvMainCheckOtherTasksAreStillExecuting(). */\r
168 long lErrorInTask = pdFALSE;\r
169 \r
170 /*\r
171  * Application entry point:\r
172  * Starts all the other tasks, then starts the scheduler. \r
173  */\r
174 int main( void )\r
175 {\r
176         /* Setup the hardware for use with the Olimex demo board. */\r
177         prvSetupHardware();\r
178 \r
179         /* Start the standard flash tasks so the WEB server is not the only thing \r
180         running. */\r
181         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
182         vStartSemaphoreTasks( tskIDLE_PRIORITY );\r
183         vStartDynamicPriorityTasks();\r
184         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
185         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
186         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
187 \r
188         /* Start the WEB server task and the error check task. */\r
189         xTaskCreate( vHTTPServerTask, ( signed char * ) "HTTP", configMINIMAL_STACK_SIZE, NULL, mainHTTP_TASK_PRIORITY, NULL );\r
190         xTaskCreate( prvErrorChecks, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainERROR_CHECK_PRIORITY, NULL );\r
191         \r
192         /* Now all the tasks have been started - start the scheduler.\r
193 \r
194         NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.\r
195         The processor MUST be in supervisor mode when vTaskStartScheduler is \r
196         called.  The demo applications included in the FreeRTOS.org download switch\r
197         to supervisor mode prior to main being called.  If you are not using one of\r
198         these demo application projects then ensure Supervisor mode is used. */\r
199         vTaskStartScheduler();\r
200 \r
201         /* Should never reach here! */\r
202         return 0;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvSetupHardware( void )\r
207 {\r
208         #ifdef RUN_FROM_RAM\r
209                 /* Remap the interrupt vectors to RAM if we are are running from RAM. */\r
210                 SCB_MEMMAP = 2;\r
211         #endif\r
212 \r
213         /* Set all GPIO to output other than the P0.14 (BSL), and the JTAG pins.  \r
214         The JTAG pins are left as input as I'm not sure what will happen if the \r
215         Wiggler is connected after powerup - not that it would be a good idea to\r
216         do that anyway. */\r
217         GPIO_IODIR = ~( mainJTAG_PORT );\r
218 \r
219         /* Setup the PLL to multiply the XTAL input by 4. */\r
220         SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );\r
221 \r
222         /* Activate the PLL by turning it on then feeding the correct sequence of\r
223         bytes. */\r
224         SCB_PLLCON = mainPLL_ENABLE;\r
225         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
226         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
227 \r
228         /* Wait for the PLL to lock... */\r
229         while( !( SCB_PLLSTAT & mainPLL_LOCK ) );\r
230 \r
231         /* ...before connecting it using the feed sequence again. */\r
232         SCB_PLLCON = mainPLL_CONNECT;\r
233         SCB_PLLFEED = mainPLL_FEED_BYTE1;\r
234         SCB_PLLFEED = mainPLL_FEED_BYTE2;\r
235 \r
236         /* Setup and turn on the MAM.  Three cycle access is used due to the fast\r
237         PLL used.  It is possible faster overall performance could be obtained by\r
238         tuning the MAM and PLL settings. */\r
239         MAM_TIM = mainMAM_TIM_3;\r
240         MAM_CR = mainMAM_MODE_FULL;\r
241 \r
242         /* Setup the peripheral bus to be the same as the PLL output. */\r
243         SCB_VPBDIV = mainBUS_CLK_FULL;\r
244 \r
245         /* Initialise the i2c peripheral. */\r
246         i2cInit();\r
247 \r
248         /* Initialise the LED's used by the flash tasks. */\r
249         vParTestInitialise();\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 static void prvMainCheckOtherTasksAreStillRunning( void )\r
254 {\r
255         /* Check all the demo tasks (other than the flash tasks) to ensure\r
256         that they are all still running, and that none of them have detected\r
257         an error. */\r
258 \r
259         /* This function is called from more than one task. */\r
260         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
261         {\r
262                 lErrorInTask = pdTRUE;\r
263         }\r
264 \r
265         if( xArePollingQueuesStillRunning() != pdTRUE )\r
266         {\r
267                 lErrorInTask = pdTRUE;\r
268         }\r
269 \r
270         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
271         {\r
272                 lErrorInTask = pdTRUE;\r
273         }\r
274 \r
275         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
276         {\r
277                 lErrorInTask = pdTRUE;\r
278         }\r
279 \r
280         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
281         {\r
282                 lErrorInTask = pdTRUE;\r
283         }\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 void prvToggleOnBoardLED( void )\r
288 {\r
289 unsigned long ulState;\r
290 \r
291         ulState = GPIO0_IOPIN;\r
292         if( ulState & mainON_BOARD_LED_BIT )\r
293         {\r
294                 GPIO_IOCLR = mainON_BOARD_LED_BIT;\r
295         }\r
296         else\r
297         {\r
298                 GPIO_IOSET = mainON_BOARD_LED_BIT;\r
299         }       \r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 static void prvErrorChecks( void *pvParameters )\r
304 {\r
305 portTickType xDelay = mainNO_ERROR_DELAY;\r
306 \r
307         /* The parameters are not used. */\r
308         ( void ) pvParameters;\r
309 \r
310         for( ;; )\r
311         {\r
312                 /* How long we delay depends on whether an error has been detected\r
313                 or not.  Therefore the flash rate of the on board LED indicates \r
314                 whether or not an error has occurred. */\r
315                 vTaskDelay( xDelay );\r
316 \r
317                 /* Update the lErrorInTask flag. */\r
318                 prvMainCheckOtherTasksAreStillRunning();\r
319 \r
320                 if( lErrorInTask )\r
321                 {\r
322                         /* An error has been found so reduce the delay period and in so\r
323                         doing speed up the flash rate of the on board LED. */\r
324                         xDelay = mainERROR_DELAY;\r
325                 }\r
326 \r
327                 prvToggleOnBoardLED();\r
328         }\r
329 }\r
330 \r