1 |
589 |
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 |
|
|
* Creates all the demo application tasks, then starts the scheduler. The WEB
|
56 |
|
|
* documentation provides more details of the demo application tasks.
|
57 |
|
|
*
|
58 |
|
|
* Main. c also creates a task called "Check". This only executes every three
|
59 |
|
|
* seconds but has the highest priority so is guaranteed to get processor time.
|
60 |
|
|
* Its main function is to check that all the other tasks are still operational.
|
61 |
|
|
* Each task that does not flash an LED maintains a unique count that is
|
62 |
|
|
* incremented each time the task successfully completes its function. Should
|
63 |
|
|
* any error occur within such a task the count is permanently halted. The
|
64 |
|
|
* check task inspects the count of each task to ensure it has changed since
|
65 |
|
|
* the last time the check task executed. If all the count variables have
|
66 |
|
|
* changed all the tasks are still executing error free, and the check task
|
67 |
|
|
* toggles an LED. Should any task contain an error at any time the LED toggle
|
68 |
|
|
* will stop.
|
69 |
|
|
*
|
70 |
|
|
* The LED flash and communications test tasks do not maintain a count.
|
71 |
|
|
*/
|
72 |
|
|
|
73 |
|
|
/*
|
74 |
|
|
Changes from V1.2.0
|
75 |
|
|
|
76 |
|
|
+ Changed the baud rate for the serial test from 19200 to 57600.
|
77 |
|
|
|
78 |
|
|
Changes from V1.2.3
|
79 |
|
|
|
80 |
|
|
+ The integer and comtest tasks are now used when the cooperative scheduler
|
81 |
|
|
is being used. Previously they were only used with the preemptive
|
82 |
|
|
scheduler.
|
83 |
|
|
|
84 |
|
|
Changes from V1.2.5
|
85 |
|
|
|
86 |
|
|
+ Set the baud rate to 38400. This has a smaller error percentage with an
|
87 |
|
|
8MHz clock (according to the manual).
|
88 |
|
|
|
89 |
|
|
Changes from V2.0.0
|
90 |
|
|
|
91 |
|
|
+ Delay periods are now specified using variables and constants of
|
92 |
|
|
portTickType rather than unsigned long.
|
93 |
|
|
|
94 |
|
|
Changes from V2.2.0
|
95 |
|
|
|
96 |
|
|
+ File can now be built using either the IAR or WinAVR compiler.
|
97 |
|
|
|
98 |
|
|
Changes from V2.6.1
|
99 |
|
|
|
100 |
|
|
+ The IAR and WinAVR AVR ports are now maintained separately.
|
101 |
|
|
|
102 |
|
|
Changes from V4.0.5
|
103 |
|
|
|
104 |
|
|
+ Modified to demonstrate the use of co-routines.
|
105 |
|
|
*/
|
106 |
|
|
|
107 |
|
|
#include <stdlib.h>
|
108 |
|
|
#include <string.h>
|
109 |
|
|
|
110 |
|
|
#ifdef GCC_MEGA_AVR
|
111 |
|
|
/* EEPROM routines used only with the WinAVR compiler. */
|
112 |
|
|
#include <avr/eeprom.h>
|
113 |
|
|
#endif
|
114 |
|
|
|
115 |
|
|
/* Scheduler include files. */
|
116 |
|
|
#include "FreeRTOS.h"
|
117 |
|
|
#include "task.h"
|
118 |
|
|
#include "croutine.h"
|
119 |
|
|
|
120 |
|
|
/* Demo file headers. */
|
121 |
|
|
#include "PollQ.h"
|
122 |
|
|
#include "integer.h"
|
123 |
|
|
#include "serial.h"
|
124 |
|
|
#include "comtest.h"
|
125 |
|
|
#include "crflash.h"
|
126 |
|
|
#include "print.h"
|
127 |
|
|
#include "partest.h"
|
128 |
|
|
#include "regtest.h"
|
129 |
|
|
|
130 |
|
|
/* Priority definitions for most of the tasks in the demo application. Some
|
131 |
|
|
tasks just use the idle priority. */
|
132 |
|
|
#define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
133 |
|
|
#define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
134 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
135 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
|
136 |
|
|
|
137 |
|
|
/* Baud rate used by the serial port tasks. */
|
138 |
|
|
#define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 38400 )
|
139 |
|
|
|
140 |
|
|
/* LED used by the serial port tasks. This is toggled on each character Tx,
|
141 |
|
|
and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
|
142 |
|
|
#define mainCOM_TEST_LED ( 4 )
|
143 |
|
|
|
144 |
|
|
/* LED that is toggled by the check task. The check task periodically checks
|
145 |
|
|
that all the other tasks are operating without error. If no errors are found
|
146 |
|
|
the LED is toggled. If an error is found at any time the LED is never toggles
|
147 |
|
|
again. */
|
148 |
|
|
#define mainCHECK_TASK_LED ( 7 )
|
149 |
|
|
|
150 |
|
|
/* The period between executions of the check task. */
|
151 |
|
|
#define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
|
152 |
|
|
|
153 |
|
|
/* An address in the EEPROM used to count resets. This is used to check that
|
154 |
|
|
the demo application is not unexpectedly resetting. */
|
155 |
|
|
#define mainRESET_COUNT_ADDRESS ( ( void * ) 0x50 )
|
156 |
|
|
|
157 |
|
|
/* The number of coroutines to create. */
|
158 |
|
|
#define mainNUM_FLASH_COROUTINES ( 3 )
|
159 |
|
|
|
160 |
|
|
/*
|
161 |
|
|
* The task function for the "Check" task.
|
162 |
|
|
*/
|
163 |
|
|
static void vErrorChecks( void *pvParameters );
|
164 |
|
|
|
165 |
|
|
/*
|
166 |
|
|
* Checks the unique counts of other tasks to ensure they are still operational.
|
167 |
|
|
* Flashes an LED if everything is okay.
|
168 |
|
|
*/
|
169 |
|
|
static void prvCheckOtherTasksAreStillRunning( void );
|
170 |
|
|
|
171 |
|
|
/*
|
172 |
|
|
* Called on boot to increment a count stored in the EEPROM. This is used to
|
173 |
|
|
* ensure the CPU does not reset unexpectedly.
|
174 |
|
|
*/
|
175 |
|
|
static void prvIncrementResetCount( void );
|
176 |
|
|
|
177 |
|
|
/*
|
178 |
|
|
* Idle hook is used to scheduler co-routines.
|
179 |
|
|
*/
|
180 |
|
|
void vApplicationIdleHook( void );
|
181 |
|
|
|
182 |
|
|
short main( void )
|
183 |
|
|
{
|
184 |
|
|
prvIncrementResetCount();
|
185 |
|
|
|
186 |
|
|
/* Setup the LED's for output. */
|
187 |
|
|
vParTestInitialise();
|
188 |
|
|
|
189 |
|
|
/* Create the standard demo tasks. */
|
190 |
|
|
vStartIntegerMathTasks( tskIDLE_PRIORITY );
|
191 |
|
|
vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
|
192 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
193 |
|
|
vStartRegTestTasks();
|
194 |
|
|
|
195 |
|
|
/* Create the tasks defined within this file. */
|
196 |
|
|
xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
|
197 |
|
|
|
198 |
|
|
/* Create the co-routines that flash the LED's. */
|
199 |
|
|
vStartFlashCoRoutines( mainNUM_FLASH_COROUTINES );
|
200 |
|
|
|
201 |
|
|
/* In this port, to use preemptive scheduler define configUSE_PREEMPTION
|
202 |
|
|
as 1 in portmacro.h. To use the cooperative scheduler define
|
203 |
|
|
configUSE_PREEMPTION as 0. */
|
204 |
|
|
vTaskStartScheduler();
|
205 |
|
|
|
206 |
|
|
return 0;
|
207 |
|
|
}
|
208 |
|
|
/*-----------------------------------------------------------*/
|
209 |
|
|
|
210 |
|
|
static void vErrorChecks( void *pvParameters )
|
211 |
|
|
{
|
212 |
|
|
static volatile unsigned long ulDummyVariable = 3UL;
|
213 |
|
|
|
214 |
|
|
/* The parameters are not used. */
|
215 |
|
|
( void ) pvParameters;
|
216 |
|
|
|
217 |
|
|
/* Cycle for ever, delaying then checking all the other tasks are still
|
218 |
|
|
operating without error. */
|
219 |
|
|
for( ;; )
|
220 |
|
|
{
|
221 |
|
|
vTaskDelay( mainCHECK_PERIOD );
|
222 |
|
|
|
223 |
|
|
/* Perform a bit of 32bit maths to ensure the registers used by the
|
224 |
|
|
integer tasks get some exercise. The result here is not important -
|
225 |
|
|
see the demo application documentation for more info. */
|
226 |
|
|
ulDummyVariable *= 3;
|
227 |
|
|
|
228 |
|
|
prvCheckOtherTasksAreStillRunning();
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
/*-----------------------------------------------------------*/
|
232 |
|
|
|
233 |
|
|
static void prvCheckOtherTasksAreStillRunning( void )
|
234 |
|
|
{
|
235 |
|
|
static portBASE_TYPE xErrorHasOccurred = pdFALSE;
|
236 |
|
|
|
237 |
|
|
if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
|
238 |
|
|
{
|
239 |
|
|
xErrorHasOccurred = pdTRUE;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
if( xAreComTestTasksStillRunning() != pdTRUE )
|
243 |
|
|
{
|
244 |
|
|
xErrorHasOccurred = pdTRUE;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
if( xArePollingQueuesStillRunning() != pdTRUE )
|
248 |
|
|
{
|
249 |
|
|
xErrorHasOccurred = pdTRUE;
|
250 |
|
|
}
|
251 |
|
|
|
252 |
|
|
if( xAreRegTestTasksStillRunning() != pdTRUE )
|
253 |
|
|
{
|
254 |
|
|
xErrorHasOccurred = pdTRUE;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
if( xErrorHasOccurred == pdFALSE )
|
258 |
|
|
{
|
259 |
|
|
/* Toggle the LED if everything is okay so we know if an error occurs even if not
|
260 |
|
|
using console IO. */
|
261 |
|
|
vParTestToggleLED( mainCHECK_TASK_LED );
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
/*-----------------------------------------------------------*/
|
265 |
|
|
|
266 |
|
|
static void prvIncrementResetCount( void )
|
267 |
|
|
{
|
268 |
|
|
unsigned char ucCount;
|
269 |
|
|
const unsigned char ucReadBit = ( unsigned char ) 0x01;
|
270 |
|
|
const unsigned char ucWrite1 = ( unsigned char ) 0x04;
|
271 |
|
|
const unsigned char ucWrite2 = ( unsigned char ) 0x02;
|
272 |
|
|
|
273 |
|
|
/* Increment the EEPROM value at 0x00.
|
274 |
|
|
|
275 |
|
|
Setup the EEPROM address. */
|
276 |
|
|
EEARH = 0x00;
|
277 |
|
|
EEARL = 0x00;
|
278 |
|
|
|
279 |
|
|
/* Set the read enable bit. */
|
280 |
|
|
EECR |= ucReadBit;
|
281 |
|
|
|
282 |
|
|
/* Wait for the read. */
|
283 |
|
|
while( EECR & ucReadBit );
|
284 |
|
|
|
285 |
|
|
/* The byte is ready. */
|
286 |
|
|
ucCount = EEDR;
|
287 |
|
|
|
288 |
|
|
/* Increment the reset count, then write the byte back. */
|
289 |
|
|
ucCount++;
|
290 |
|
|
EEDR = ucCount;
|
291 |
|
|
EECR = ucWrite1;
|
292 |
|
|
EECR = ( ucWrite1 | ucWrite2 );
|
293 |
|
|
}
|
294 |
|
|
/*-----------------------------------------------------------*/
|
295 |
|
|
|
296 |
|
|
void vApplicationIdleHook( void )
|
297 |
|
|
{
|
298 |
|
|
vCoRoutineSchedule();
|
299 |
|
|
}
|
300 |
|
|
|