1 |
584 |
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 standard demo application tasks.
|
57 |
|
|
* In addition to the standard demo tasks, the following tasks and tests are
|
58 |
|
|
* defined and/or created within this file:
|
59 |
|
|
*
|
60 |
|
|
* "Check" task - This only executes every three seconds but has a high priority
|
61 |
|
|
* to ensure it gets processor time. Its main function is to check that all the
|
62 |
|
|
* standard demo tasks are still operational. If everything is running as
|
63 |
|
|
* expected then the check task will toggle an LED every 3 seconds. An error
|
64 |
|
|
* being discovered in any task will cause the toggle rate to increase to 500ms.
|
65 |
|
|
*
|
66 |
|
|
* "Reg test" tasks - These fill the registers with known values, then check
|
67 |
|
|
* that each register still contains its expected value. Each task uses
|
68 |
|
|
* different values. The tasks run with very low priority so get preempted very
|
69 |
|
|
* frequently. A register containing an unexpected value is indicative of an
|
70 |
|
|
* error in the context switching mechanism.
|
71 |
|
|
*
|
72 |
|
|
*
|
73 |
|
|
* Also in addition to the standard demo tasks is a button push task. This is
|
74 |
|
|
* a very basic task that is included as an example of how to write an interrupt
|
75 |
|
|
* service routine that interacts with a task. The button on the target board
|
76 |
|
|
* is used to generate an interrupt that 'gives' a semaphore in order to unblock
|
77 |
|
|
* a task. In doing so the task is synchronised with the interrupt. Each time
|
78 |
|
|
* the task unblocks it simply toggles an LED before entering the Blocked state
|
79 |
|
|
* again to wait for the next button push.
|
80 |
|
|
*/
|
81 |
|
|
|
82 |
|
|
/* Standard includes. */
|
83 |
|
|
#include <stdlib.h>
|
84 |
|
|
#include <string.h>
|
85 |
|
|
|
86 |
|
|
/* Scheduler include files. */
|
87 |
|
|
#include "FreeRTOS.h"
|
88 |
|
|
#include "task.h"
|
89 |
|
|
|
90 |
|
|
/* Standard demo file headers. */
|
91 |
|
|
#include "PollQ.h"
|
92 |
|
|
#include "semtest.h"
|
93 |
|
|
#include "GenQTest.h"
|
94 |
|
|
#include "dynamic.h"
|
95 |
|
|
#include "blocktim.h"
|
96 |
|
|
|
97 |
|
|
/*
|
98 |
|
|
* Priority definitions for most of the tasks in the demo application. Some
|
99 |
|
|
* tasks just use the idle priority.
|
100 |
|
|
*/
|
101 |
|
|
#define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
|
102 |
|
|
#define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
103 |
|
|
#define mainSEMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
|
104 |
|
|
#define mainBUTTON_PRIORITY ( configMAX_PRIORITIES - 1 )
|
105 |
|
|
#define mainGEN_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
|
106 |
|
|
|
107 |
|
|
/* The period between executions of the check task. */
|
108 |
|
|
#define mainNO_ERROR_TOGGLE_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
|
109 |
|
|
#define mainERROR_TOGGLE_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
|
110 |
|
|
|
111 |
|
|
/* The LED toggled by the check task. */
|
112 |
|
|
#define mainLED_0 P7_bit.no6
|
113 |
|
|
|
114 |
|
|
/* A value that is passed in as the parameter to the 'check' task. This is done
|
115 |
|
|
purely to check that the parameter passing mechanism is functioning correctly. */
|
116 |
|
|
#define mainCHECK_PARAMETER_VALUE ( 0x5678 )
|
117 |
|
|
|
118 |
|
|
/*-----------------------------------------------------------*/
|
119 |
|
|
|
120 |
|
|
/*
|
121 |
|
|
* The function that defines the 'check' task as described at the top of this
|
122 |
|
|
* file.
|
123 |
|
|
*/
|
124 |
|
|
static void vErrorChecks( void *pvParameters );
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
/*
|
128 |
|
|
* This function is called from the C startup routine to setup the processor -
|
129 |
|
|
* in particular the clock source.
|
130 |
|
|
*/
|
131 |
|
|
int __low_level_init(void);
|
132 |
|
|
|
133 |
|
|
/*
|
134 |
|
|
* Functions that define the RegTest tasks as described at the top of this file.
|
135 |
|
|
*/
|
136 |
|
|
extern void vRegTest1( void *pvParameters );
|
137 |
|
|
extern void vRegTest2( void *pvParameters );
|
138 |
|
|
|
139 |
|
|
/*
|
140 |
|
|
* Function that defines the button push task as described at the top of this
|
141 |
|
|
* file.
|
142 |
|
|
*/
|
143 |
|
|
extern void vButtonTask( void *pvParameters );
|
144 |
|
|
|
145 |
|
|
/*-----------------------------------------------------------*/
|
146 |
|
|
|
147 |
|
|
/* If an error is discovered by one of the RegTest tasks then this flag is set
|
148 |
|
|
to pdFAIL. The 'check' task then inspects this flag to detect errors within
|
149 |
|
|
the RegTest tasks. */
|
150 |
|
|
static short sRegTestStatus = pdPASS;
|
151 |
|
|
|
152 |
|
|
/* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
|
153 |
|
|
enabled. */
|
154 |
|
|
__root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =
|
155 |
|
|
{
|
156 |
|
|
WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
|
157 |
|
|
};
|
158 |
|
|
|
159 |
|
|
/* Security byte definition */
|
160 |
|
|
__root __far const unsigned portCHAR SecuIDCode[] @ 0x00C4 =
|
161 |
|
|
{
|
162 |
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
|
163 |
|
|
};
|
164 |
|
|
|
165 |
|
|
|
166 |
|
|
/*-----------------------------------------------------------*/
|
167 |
|
|
|
168 |
|
|
short main( void )
|
169 |
|
|
{
|
170 |
|
|
/* Creates all the tasks, then starts the scheduler. */
|
171 |
|
|
|
172 |
|
|
/* First create the 'standard demo' tasks. These are used to demonstrate
|
173 |
|
|
API functions being used and also to test the kernel port. More information
|
174 |
|
|
is provided on the FreeRTOS.org WEB site. */
|
175 |
|
|
vStartDynamicPriorityTasks();
|
176 |
|
|
|
177 |
|
|
/* Create the RegTest tasks as described at the top of this file. */
|
178 |
|
|
xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
|
179 |
|
|
xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );
|
180 |
|
|
|
181 |
|
|
/* Create the button push task as described at the top of this file. */
|
182 |
|
|
xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );
|
183 |
|
|
|
184 |
|
|
/* Create the 'check' task as described at the top of this file. */
|
185 |
|
|
xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );
|
186 |
|
|
|
187 |
|
|
#ifdef __IAR_78K0R_Kx3__
|
188 |
|
|
{
|
189 |
|
|
/* The Kx3 has enough RAM to create more of the standard demo tasks. */
|
190 |
|
|
vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
|
191 |
|
|
vStartSemaphoreTasks(mainSEMTEST_PRIORITY);
|
192 |
|
|
vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );
|
193 |
|
|
vCreateBlockTimeTasks();
|
194 |
|
|
}
|
195 |
|
|
#endif
|
196 |
|
|
|
197 |
|
|
/* Finally start the scheduler running. */
|
198 |
|
|
vTaskStartScheduler();
|
199 |
|
|
|
200 |
|
|
/* If this line is reached then vTaskStartScheduler() returned because there
|
201 |
|
|
was insufficient heap memory remaining for the idle task to be created. */
|
202 |
|
|
for( ;; );
|
203 |
|
|
}
|
204 |
|
|
/*-----------------------------------------------------------*/
|
205 |
|
|
|
206 |
|
|
static void vErrorChecks( void *pvParameters )
|
207 |
|
|
{
|
208 |
|
|
portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
|
209 |
|
|
|
210 |
|
|
/* Ensure the parameter was passed in as expected. This is just a test of
|
211 |
|
|
the kernel port, the parameter is not actually used for anything. The
|
212 |
|
|
pointer will only actually be either 3 or 2 bytes, depending on the memory
|
213 |
|
|
model. */
|
214 |
|
|
if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
|
215 |
|
|
{
|
216 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/* Initialise xLastWakeTime before it is used. After this point it is not
|
220 |
|
|
written to directly. */
|
221 |
|
|
xLastWakeTime = xTaskGetTickCount();
|
222 |
|
|
|
223 |
|
|
/* Cycle for ever, delaying then checking all the other tasks are still
|
224 |
|
|
operating without error. */
|
225 |
|
|
for( ;; )
|
226 |
|
|
{
|
227 |
|
|
/* Wait until it is time to check all the other tasks again. */
|
228 |
|
|
vTaskDelayUntil( &xLastWakeTime, xToggleRate );
|
229 |
|
|
|
230 |
|
|
if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
|
231 |
|
|
{
|
232 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
if( sRegTestStatus != pdPASS )
|
236 |
|
|
{
|
237 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
238 |
|
|
}
|
239 |
|
|
|
240 |
|
|
#ifdef __IAR_78K0R_Kx3__
|
241 |
|
|
{
|
242 |
|
|
/* Only the Kx3 runs all the tasks. */
|
243 |
|
|
if( xArePollingQueuesStillRunning() != pdTRUE)
|
244 |
|
|
{
|
245 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
246 |
|
|
}
|
247 |
|
|
|
248 |
|
|
if( xAreSemaphoreTasksStillRunning() != pdTRUE)
|
249 |
|
|
{
|
250 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
251 |
|
|
}
|
252 |
|
|
|
253 |
|
|
if( xAreGenericQueueTasksStillRunning() != pdTRUE )
|
254 |
|
|
{
|
255 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
256 |
|
|
}
|
257 |
|
|
|
258 |
|
|
if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
|
259 |
|
|
{
|
260 |
|
|
xToggleRate = mainERROR_TOGGLE_PERIOD;
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
#endif
|
264 |
|
|
|
265 |
|
|
/* Toggle the LED. The toggle rate will depend on whether or not an
|
266 |
|
|
error has been found in any tasks. */
|
267 |
|
|
mainLED_0 = !mainLED_0;
|
268 |
|
|
}
|
269 |
|
|
}
|
270 |
|
|
/*-----------------------------------------------------------*/
|
271 |
|
|
|
272 |
|
|
int __low_level_init(void)
|
273 |
|
|
{
|
274 |
|
|
unsigned portCHAR ucResetFlag = RESF;
|
275 |
|
|
|
276 |
|
|
portDISABLE_INTERRUPTS();
|
277 |
|
|
|
278 |
|
|
/* Clock Configuration:
|
279 |
|
|
In this port, to use the internal high speed clock source of the microcontroller
|
280 |
|
|
define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h. To use an external
|
281 |
|
|
clock define configCLOCK_SOURCE as 0. */
|
282 |
|
|
#if configCLOCK_SOURCE == 1
|
283 |
|
|
{
|
284 |
|
|
/* Set XT1 and XT2 in Input Port Mode
|
285 |
|
|
Set X1 and X2 in Input Port Mode
|
286 |
|
|
High speed oscillator frequency 2MHz <= fMX <= 10MHz */
|
287 |
|
|
CMC = 0x00;
|
288 |
|
|
|
289 |
|
|
/* X1 external oszillation stopped. */
|
290 |
|
|
MSTOP = 1;
|
291 |
|
|
|
292 |
|
|
/* Enable internal high speed oszillation. */
|
293 |
|
|
HIOSTOP = 0;
|
294 |
|
|
MCM0 = 0;
|
295 |
|
|
|
296 |
|
|
/* Stop internal subsystem clock. */
|
297 |
|
|
XTSTOP = 1;
|
298 |
|
|
|
299 |
|
|
/* Set clock speed. */
|
300 |
|
|
CSS = 0;
|
301 |
|
|
CKC &= (unsigned portCHAR)~0x07;
|
302 |
|
|
CKC |= 0x00;
|
303 |
|
|
}
|
304 |
|
|
#else
|
305 |
|
|
{
|
306 |
|
|
/* XT1 and XT2 pin in input port mode
|
307 |
|
|
X1 and X2 pin in crystal resonator mode
|
308 |
|
|
High speed oszillation frequency 10MHz < fMX <= 20MHz */
|
309 |
|
|
CMC = 0x41;
|
310 |
|
|
|
311 |
|
|
/* Set oscillation stabilization time. */
|
312 |
|
|
OSTS = 0x07;
|
313 |
|
|
|
314 |
|
|
/* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
|
315 |
|
|
OSMC = 0x01;
|
316 |
|
|
|
317 |
|
|
/* Start up X1 oscillator operation
|
318 |
|
|
Internal high-speed oscillator operating. */
|
319 |
|
|
MSTOP = 0;
|
320 |
|
|
|
321 |
|
|
/* Check oscillation stabilization time status. */
|
322 |
|
|
while(OSTC < 0x07)
|
323 |
|
|
{
|
324 |
|
|
/* Wait until X1 clock stabilization time. */
|
325 |
|
|
portNOP();
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
/* Switch CPU clock to X1 oscillator. */
|
329 |
|
|
MCM0 = 1;
|
330 |
|
|
while(MCS != 1)
|
331 |
|
|
{
|
332 |
|
|
/* Wait until CPU and peripherals operate with fX1 clock. */
|
333 |
|
|
portNOP();
|
334 |
|
|
}
|
335 |
|
|
|
336 |
|
|
/* Stop the internal high-speed oscillator operation. */
|
337 |
|
|
HIOSTOP = 1;
|
338 |
|
|
|
339 |
|
|
/* Stop the XT1 oscillator operation. */
|
340 |
|
|
XTSTOP = 1;
|
341 |
|
|
|
342 |
|
|
/* Operating frequency f = fx
|
343 |
|
|
Change clock generator setting, if necessary. */
|
344 |
|
|
CKC &= 0xF8;
|
345 |
|
|
|
346 |
|
|
/* From here onwards the X1 oscillator is supplied to the CPU. */
|
347 |
|
|
}
|
348 |
|
|
#endif
|
349 |
|
|
|
350 |
|
|
/* LED port initialization - set port register. */
|
351 |
|
|
P7 = 0x80;
|
352 |
|
|
|
353 |
|
|
/* Set port mode register. */
|
354 |
|
|
PM7 = 0x3F;
|
355 |
|
|
|
356 |
|
|
/* Switch pin initialization - enable pull-up resistor. */
|
357 |
|
|
PU12_bit.no0 = 1;
|
358 |
|
|
|
359 |
|
|
/* INTP0 is used by the button on the target board. */
|
360 |
|
|
|
361 |
|
|
/* INTP0 disable. */
|
362 |
|
|
PMK0 = 1;
|
363 |
|
|
|
364 |
|
|
/* INTP0 IF clear. */
|
365 |
|
|
PIF0 = 0;
|
366 |
|
|
EGN0_bit.no0 = 1;
|
367 |
|
|
|
368 |
|
|
/* INTP0 priority low. */
|
369 |
|
|
PPR10 = 0;
|
370 |
|
|
PPR00 = 1;
|
371 |
|
|
|
372 |
|
|
/* Enable ext. INTP0 interrupt */
|
373 |
|
|
PMK0 = 0;
|
374 |
|
|
|
375 |
|
|
return pdTRUE;
|
376 |
|
|
}
|
377 |
|
|
/*-----------------------------------------------------------*/
|
378 |
|
|
|
379 |
|
|
void vRegTestError( void )
|
380 |
|
|
{
|
381 |
|
|
/* Called by the RegTest tasks if an error is found. lRegTestStatus is
|
382 |
|
|
inspected by the check task. */
|
383 |
|
|
sRegTestStatus = pdFAIL;
|
384 |
|
|
|
385 |
|
|
/* Do not return from here as the reg test tasks clobber all registers so
|
386 |
|
|
function calls may not function correctly. */
|
387 |
|
|
for( ;; );
|
388 |
|
|
}
|
389 |
|
|
/*-----------------------------------------------------------*/
|
390 |
|
|
|
391 |
|
|
void vApplicationStackOverflowHook( void )
|
392 |
|
|
{
|
393 |
|
|
/* This will get called if an overflow is detected in the stack of a task.
|
394 |
|
|
Inspect pxCurrentTCB to see which was the offending task. */
|
395 |
|
|
for( ;; );
|
396 |
|
|
}
|
397 |
|
|
|