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