1 |
583 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
|
56 |
|
|
The processor MUST be in supervisor mode when vTaskStartScheduler is
|
57 |
|
|
called. The demo applications included in the FreeRTOS.org download switch
|
58 |
|
|
to supervisor mode prior to main being called. If you are not using one of
|
59 |
|
|
these demo application projects then ensure Supervisor mode is used.
|
60 |
|
|
*/
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/*
|
64 |
|
|
* Creates all the application tasks, then starts the scheduler.
|
65 |
|
|
*
|
66 |
|
|
* A task is created called "uIP". This executes the uIP stack and small
|
67 |
|
|
* WEB server sample. All the other tasks are from the set of standard
|
68 |
|
|
* demo tasks. The WEB documentation provides more details of the standard
|
69 |
|
|
* demo application tasks.
|
70 |
|
|
*
|
71 |
|
|
* Main.c also creates a task called "Check". This only executes every three
|
72 |
|
|
* seconds but has the highest priority so is guaranteed to get processor time.
|
73 |
|
|
* Its main function is to check that all the other tasks are still operational.
|
74 |
|
|
* Each standard demo task maintains a unique count that is incremented each
|
75 |
|
|
* time the task successfully completes its function. Should any error occur
|
76 |
|
|
* within such a task the count is permanently halted. The check task inspects
|
77 |
|
|
* the count of each task to ensure it has changed since the last time the
|
78 |
|
|
* check task executed. If all the count variables have changed all the tasks
|
79 |
|
|
* are still executing error free, and the check task toggles the yellow LED.
|
80 |
|
|
* Should any task contain an error at any time the LED toggle rate will change
|
81 |
|
|
* from 3 seconds to 500ms.
|
82 |
|
|
*
|
83 |
|
|
*/
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* Standard includes. */
|
87 |
|
|
#include <stdlib.h>
|
88 |
|
|
#include <string.h>
|
89 |
|
|
|
90 |
|
|
/* Scheduler includes. */
|
91 |
|
|
#include "FreeRTOS.h"
|
92 |
|
|
#include "task.h"
|
93 |
|
|
|
94 |
|
|
/* Demo application includes. */
|
95 |
|
|
#include "PollQ.h"
|
96 |
|
|
#include "dynamic.h"
|
97 |
|
|
#include "semtest.h"
|
98 |
|
|
|
99 |
|
|
/*-----------------------------------------------------------*/
|
100 |
|
|
|
101 |
|
|
/* Constants to setup the PLL. */
|
102 |
|
|
#define mainPLL_MUL_4 ( ( unsigned char ) 0x0003 )
|
103 |
|
|
#define mainPLL_DIV_1 ( ( unsigned char ) 0x0000 )
|
104 |
|
|
#define mainPLL_ENABLE ( ( unsigned char ) 0x0001 )
|
105 |
|
|
#define mainPLL_CONNECT ( ( unsigned char ) 0x0003 )
|
106 |
|
|
#define mainPLL_FEED_BYTE1 ( ( unsigned char ) 0xaa )
|
107 |
|
|
#define mainPLL_FEED_BYTE2 ( ( unsigned char ) 0x55 )
|
108 |
|
|
#define mainPLL_LOCK ( ( unsigned long ) 0x0400 )
|
109 |
|
|
|
110 |
|
|
/* Constants to setup the MAM. */
|
111 |
|
|
#define mainMAM_TIM_3 ( ( unsigned char ) 0x03 )
|
112 |
|
|
#define mainMAM_MODE_FULL ( ( unsigned char ) 0x02 )
|
113 |
|
|
|
114 |
|
|
/* Constants to setup the peripheral bus. */
|
115 |
|
|
#define mainBUS_CLK_FULL ( ( unsigned char ) 0x01 )
|
116 |
|
|
|
117 |
|
|
/* Priorities/stacks for the demo application tasks. */
|
118 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
119 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
|
120 |
|
|
#define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
121 |
|
|
#define mainUIP_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
122 |
|
|
#define mainUIP_TASK_STACK_SIZE ( 150 )
|
123 |
|
|
|
124 |
|
|
/* The rate at which the on board LED will toggle when there is/is not an
|
125 |
|
|
error. */
|
126 |
|
|
#define mainNO_ERROR_FLASH_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
|
127 |
|
|
#define mainERROR_FLASH_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
|
128 |
|
|
#define mainON_BOARD_LED_BIT ( ( unsigned long ) 0x80 )
|
129 |
|
|
#define mainYELLOW_LED ( 1 << 11 )
|
130 |
|
|
|
131 |
|
|
/*-----------------------------------------------------------*/
|
132 |
|
|
|
133 |
|
|
/*
|
134 |
|
|
* This is the uIP task which is defined within the uip.c file. This has not
|
135 |
|
|
* been placed into a header file in order to minimise the changes to the uip
|
136 |
|
|
* code.
|
137 |
|
|
*/
|
138 |
|
|
extern void ( vuIP_TASK ) ( void *pvParameters );
|
139 |
|
|
|
140 |
|
|
/*
|
141 |
|
|
* The Yellow LED is under the control of the Check task. All the other LED's
|
142 |
|
|
* are under the control of the uIP task.
|
143 |
|
|
*/
|
144 |
|
|
void prvToggleOnBoardLED( void );
|
145 |
|
|
|
146 |
|
|
/*
|
147 |
|
|
* Checks that all the demo application tasks are still executing without error
|
148 |
|
|
* - as described at the top of the file.
|
149 |
|
|
*/
|
150 |
|
|
static long prvCheckOtherTasksAreStillRunning( void );
|
151 |
|
|
|
152 |
|
|
/*
|
153 |
|
|
* The task that executes at the highest priority and calls
|
154 |
|
|
* prvCheckOtherTasksAreStillRunning(). See the description at the top
|
155 |
|
|
* of the file.
|
156 |
|
|
*/
|
157 |
|
|
static void vErrorChecks( void *pvParameters );
|
158 |
|
|
|
159 |
|
|
/*
|
160 |
|
|
* Configure the processor for use with the Olimex demo board. This includes
|
161 |
|
|
* setup for the I/O, system clock, and access timings.
|
162 |
|
|
*/
|
163 |
|
|
static void prvSetupHardware( void );
|
164 |
|
|
|
165 |
|
|
/*-----------------------------------------------------------*/
|
166 |
|
|
|
167 |
|
|
/*
|
168 |
|
|
* Starts all the other tasks, then starts the scheduler.
|
169 |
|
|
*/
|
170 |
|
|
int main( void )
|
171 |
|
|
{
|
172 |
|
|
/* Configure the processor. */
|
173 |
|
|
prvSetupHardware();
|
174 |
|
|
|
175 |
|
|
/* Start the task that handles the TCP/IP functionality. */
|
176 |
|
|
xTaskCreate( vuIP_TASK, "uIP", mainUIP_TASK_STACK_SIZE, NULL, mainUIP_PRIORITY, NULL );
|
177 |
|
|
|
178 |
|
|
/* Start the demo/test application tasks. These are created in addition
|
179 |
|
|
to the TCP/IP task for demonstration and test purposes. */
|
180 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
181 |
|
|
vStartDynamicPriorityTasks();
|
182 |
|
|
vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
|
183 |
|
|
|
184 |
|
|
/* Start the check task - which is defined in this file. */
|
185 |
|
|
xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
186 |
|
|
|
187 |
|
|
/* Now all the tasks have been started - start the scheduler.
|
188 |
|
|
|
189 |
|
|
NOTE : Tasks run in system mode and the scheduler runs in Supervisor mode.
|
190 |
|
|
The processor MUST be in supervisor mode when vTaskStartScheduler is
|
191 |
|
|
called. The demo applications included in the FreeRTOS.org download switch
|
192 |
|
|
to supervisor mode prior to main being called. If you are not using one of
|
193 |
|
|
these demo application projects then ensure Supervisor mode is used here. */
|
194 |
|
|
vTaskStartScheduler();
|
195 |
|
|
|
196 |
|
|
/* Should never reach here! */
|
197 |
|
|
return 0;
|
198 |
|
|
}
|
199 |
|
|
/*-----------------------------------------------------------*/
|
200 |
|
|
|
201 |
|
|
static void vErrorChecks( void *pvParameters )
|
202 |
|
|
{
|
203 |
|
|
portTickType xDelayPeriod = mainNO_ERROR_FLASH_PERIOD;
|
204 |
|
|
|
205 |
|
|
/* Cycle for ever, delaying then checking all the other tasks are still
|
206 |
|
|
operating without error. If an error is detected then the delay period
|
207 |
|
|
is decreased from mainNO_ERROR_FLASH_PERIOD to mainERROR_FLASH_PERIOD so
|
208 |
|
|
the on board LED flash rate will increase. */
|
209 |
|
|
for( ;; )
|
210 |
|
|
{
|
211 |
|
|
/* Delay until it is time to execute again. */
|
212 |
|
|
vTaskDelay( xDelayPeriod );
|
213 |
|
|
|
214 |
|
|
/* Check all the standard demo application tasks are executing without
|
215 |
|
|
error. */
|
216 |
|
|
if( prvCheckOtherTasksAreStillRunning() != pdPASS )
|
217 |
|
|
{
|
218 |
|
|
/* An error has been detected in one of the tasks - flash faster. */
|
219 |
|
|
xDelayPeriod = mainERROR_FLASH_PERIOD;
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
prvToggleOnBoardLED();
|
223 |
|
|
}
|
224 |
|
|
}
|
225 |
|
|
/*-----------------------------------------------------------*/
|
226 |
|
|
|
227 |
|
|
static void prvSetupHardware( void )
|
228 |
|
|
{
|
229 |
|
|
#ifdef RUN_FROM_RAM
|
230 |
|
|
/* Remap the interrupt vectors to RAM if we are are running from RAM. */
|
231 |
|
|
SCB_MEMMAP = 2;
|
232 |
|
|
#endif
|
233 |
|
|
|
234 |
|
|
/* Setup the PLL to multiply the XTAL input by 4. */
|
235 |
|
|
SCB_PLLCFG = ( mainPLL_MUL_4 | mainPLL_DIV_1 );
|
236 |
|
|
|
237 |
|
|
/* Activate the PLL by turning it on then feeding the correct sequence of
|
238 |
|
|
bytes. */
|
239 |
|
|
SCB_PLLCON = mainPLL_ENABLE;
|
240 |
|
|
SCB_PLLFEED = mainPLL_FEED_BYTE1;
|
241 |
|
|
SCB_PLLFEED = mainPLL_FEED_BYTE2;
|
242 |
|
|
|
243 |
|
|
/* Wait for the PLL to lock... */
|
244 |
|
|
while( !( SCB_PLLSTAT & mainPLL_LOCK ) );
|
245 |
|
|
|
246 |
|
|
/* ...before connecting it using the feed sequence again. */
|
247 |
|
|
SCB_PLLCON = mainPLL_CONNECT;
|
248 |
|
|
SCB_PLLFEED = mainPLL_FEED_BYTE1;
|
249 |
|
|
SCB_PLLFEED = mainPLL_FEED_BYTE2;
|
250 |
|
|
|
251 |
|
|
/* Setup and turn on the MAM. Three cycle access is used due to the fast
|
252 |
|
|
PLL used. It is possible faster overall performance could be obtained by
|
253 |
|
|
tuning the MAM and PLL settings. */
|
254 |
|
|
MAM_TIM = mainMAM_TIM_3;
|
255 |
|
|
MAM_CR = mainMAM_MODE_FULL;
|
256 |
|
|
|
257 |
|
|
/* Setup the peripheral bus to be the same as the PLL output. */
|
258 |
|
|
SCB_VPBDIV = mainBUS_CLK_FULL;
|
259 |
|
|
}
|
260 |
|
|
/*-----------------------------------------------------------*/
|
261 |
|
|
|
262 |
|
|
void prvToggleOnBoardLED( void )
|
263 |
|
|
{
|
264 |
|
|
unsigned long ulState;
|
265 |
|
|
|
266 |
|
|
ulState = GPIO0_IOPIN;
|
267 |
|
|
if( ulState & mainYELLOW_LED )
|
268 |
|
|
{
|
269 |
|
|
GPIO_IOCLR = mainYELLOW_LED;
|
270 |
|
|
}
|
271 |
|
|
else
|
272 |
|
|
{
|
273 |
|
|
GPIO_IOSET = mainYELLOW_LED;
|
274 |
|
|
}
|
275 |
|
|
}
|
276 |
|
|
/*-----------------------------------------------------------*/
|
277 |
|
|
|
278 |
|
|
static long prvCheckOtherTasksAreStillRunning( void )
|
279 |
|
|
{
|
280 |
|
|
long lReturn = ( long ) pdPASS;
|
281 |
|
|
|
282 |
|
|
/* Check all the demo tasks (other than the flash tasks) to ensure
|
283 |
|
|
that they are all still running, and that none of them have detected
|
284 |
|
|
an error. */
|
285 |
|
|
|
286 |
|
|
if( xArePollingQueuesStillRunning() != pdTRUE )
|
287 |
|
|
{
|
288 |
|
|
lReturn = ( long ) pdFAIL;
|
289 |
|
|
}
|
290 |
|
|
|
291 |
|
|
if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
|
292 |
|
|
{
|
293 |
|
|
lReturn = ( long ) pdFAIL;
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
if( xAreSemaphoreTasksStillRunning() != pdTRUE )
|
297 |
|
|
{
|
298 |
|
|
lReturn = ( long ) pdFAIL;
|
299 |
|
|
}
|
300 |
|
|
|
301 |
|
|
return lReturn;
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
|
305 |
|
|
|