1 |
572 |
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 |
|
|
/* Scheduler includes. */
|
55 |
|
|
#include "FreeRTOS.h"
|
56 |
|
|
#include "task.h"
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
/*-----------------------------------------------------------
|
60 |
|
|
* Implementation of functions defined in portable.h for the H8S port.
|
61 |
|
|
*----------------------------------------------------------*/
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
/*-----------------------------------------------------------*/
|
65 |
|
|
|
66 |
|
|
/* When the task starts interrupts should be enabled. */
|
67 |
|
|
#define portINITIAL_CCR ( ( portSTACK_TYPE ) 0x00 )
|
68 |
|
|
|
69 |
|
|
/* Hardware specific constants used to generate the RTOS tick from the TPU. */
|
70 |
|
|
#define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned char ) 0x20 )
|
71 |
|
|
#define portCLOCK_DIV_64 ( ( unsigned char ) 0x03 )
|
72 |
|
|
#define portCLOCK_DIV ( ( unsigned long ) 64 )
|
73 |
|
|
#define portTGRA_INTERRUPT_ENABLE ( ( unsigned char ) 0x01 )
|
74 |
|
|
#define portTIMER_CHANNEL ( ( unsigned char ) 0x02 )
|
75 |
|
|
#define portMSTP13 ( ( unsigned short ) 0x2000 )
|
76 |
|
|
|
77 |
|
|
/*
|
78 |
|
|
* Setup TPU channel one for the RTOS tick at the requested frequency.
|
79 |
|
|
*/
|
80 |
|
|
static void prvSetupTimerInterrupt( void );
|
81 |
|
|
|
82 |
|
|
/*
|
83 |
|
|
* The ISR used by portYIELD(). This is installed as a trap handler.
|
84 |
|
|
*/
|
85 |
|
|
void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );
|
86 |
|
|
|
87 |
|
|
/*-----------------------------------------------------------*/
|
88 |
|
|
|
89 |
|
|
/*
|
90 |
|
|
* See header file for description.
|
91 |
|
|
*/
|
92 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
93 |
|
|
{
|
94 |
|
|
unsigned long ulValue;
|
95 |
|
|
|
96 |
|
|
/* This requires an even address. */
|
97 |
|
|
ulValue = ( unsigned long ) pxTopOfStack;
|
98 |
|
|
if( ulValue & 1UL )
|
99 |
|
|
{
|
100 |
|
|
pxTopOfStack = pxTopOfStack - 1;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/* Place a few bytes of known values on the bottom of the stack.
|
104 |
|
|
This is just useful for debugging. */
|
105 |
|
|
pxTopOfStack--;
|
106 |
|
|
*pxTopOfStack = 0xaa;
|
107 |
|
|
pxTopOfStack--;
|
108 |
|
|
*pxTopOfStack = 0xbb;
|
109 |
|
|
pxTopOfStack--;
|
110 |
|
|
*pxTopOfStack = 0xcc;
|
111 |
|
|
pxTopOfStack--;
|
112 |
|
|
*pxTopOfStack = 0xdd;
|
113 |
|
|
|
114 |
|
|
/* The initial stack mimics an interrupt stack. First there is the program
|
115 |
|
|
counter (24 bits). */
|
116 |
|
|
ulValue = ( unsigned long ) pxCode;
|
117 |
|
|
|
118 |
|
|
pxTopOfStack--;
|
119 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
120 |
|
|
pxTopOfStack--;
|
121 |
|
|
ulValue >>= 8UL;
|
122 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
123 |
|
|
pxTopOfStack--;
|
124 |
|
|
ulValue >>= 8UL;
|
125 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
126 |
|
|
|
127 |
|
|
/* Followed by the CCR. */
|
128 |
|
|
pxTopOfStack--;
|
129 |
|
|
*pxTopOfStack = portINITIAL_CCR;
|
130 |
|
|
|
131 |
|
|
/* Next all the general purpose registers - with the parameters being passed
|
132 |
|
|
in ER0. The parameter order must match that used by the compiler when the
|
133 |
|
|
"saveall" function attribute is used. */
|
134 |
|
|
|
135 |
|
|
/* ER6 */
|
136 |
|
|
pxTopOfStack--;
|
137 |
|
|
*pxTopOfStack = 0x66;
|
138 |
|
|
pxTopOfStack--;
|
139 |
|
|
*pxTopOfStack = 0x66;
|
140 |
|
|
pxTopOfStack--;
|
141 |
|
|
*pxTopOfStack = 0x66;
|
142 |
|
|
pxTopOfStack--;
|
143 |
|
|
*pxTopOfStack = 0x66;
|
144 |
|
|
|
145 |
|
|
/* ER0 */
|
146 |
|
|
ulValue = ( unsigned long ) pvParameters;
|
147 |
|
|
|
148 |
|
|
pxTopOfStack--;
|
149 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
150 |
|
|
pxTopOfStack--;
|
151 |
|
|
ulValue >>= 8UL;
|
152 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
153 |
|
|
pxTopOfStack--;
|
154 |
|
|
ulValue >>= 8UL;
|
155 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
156 |
|
|
pxTopOfStack--;
|
157 |
|
|
ulValue >>= 8UL;
|
158 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );
|
159 |
|
|
|
160 |
|
|
/* ER1 */
|
161 |
|
|
pxTopOfStack--;
|
162 |
|
|
*pxTopOfStack = 0x11;
|
163 |
|
|
pxTopOfStack--;
|
164 |
|
|
*pxTopOfStack = 0x11;
|
165 |
|
|
pxTopOfStack--;
|
166 |
|
|
*pxTopOfStack = 0x11;
|
167 |
|
|
pxTopOfStack--;
|
168 |
|
|
*pxTopOfStack = 0x11;
|
169 |
|
|
|
170 |
|
|
/* ER2 */
|
171 |
|
|
pxTopOfStack--;
|
172 |
|
|
*pxTopOfStack = 0x22;
|
173 |
|
|
pxTopOfStack--;
|
174 |
|
|
*pxTopOfStack = 0x22;
|
175 |
|
|
pxTopOfStack--;
|
176 |
|
|
*pxTopOfStack = 0x22;
|
177 |
|
|
pxTopOfStack--;
|
178 |
|
|
*pxTopOfStack = 0x22;
|
179 |
|
|
|
180 |
|
|
/* ER3 */
|
181 |
|
|
pxTopOfStack--;
|
182 |
|
|
*pxTopOfStack = 0x33;
|
183 |
|
|
pxTopOfStack--;
|
184 |
|
|
*pxTopOfStack = 0x33;
|
185 |
|
|
pxTopOfStack--;
|
186 |
|
|
*pxTopOfStack = 0x33;
|
187 |
|
|
pxTopOfStack--;
|
188 |
|
|
*pxTopOfStack = 0x33;
|
189 |
|
|
|
190 |
|
|
/* ER4 */
|
191 |
|
|
pxTopOfStack--;
|
192 |
|
|
*pxTopOfStack = 0x44;
|
193 |
|
|
pxTopOfStack--;
|
194 |
|
|
*pxTopOfStack = 0x44;
|
195 |
|
|
pxTopOfStack--;
|
196 |
|
|
*pxTopOfStack = 0x44;
|
197 |
|
|
pxTopOfStack--;
|
198 |
|
|
*pxTopOfStack = 0x44;
|
199 |
|
|
|
200 |
|
|
/* ER5 */
|
201 |
|
|
pxTopOfStack--;
|
202 |
|
|
*pxTopOfStack = 0x55;
|
203 |
|
|
pxTopOfStack--;
|
204 |
|
|
*pxTopOfStack = 0x55;
|
205 |
|
|
pxTopOfStack--;
|
206 |
|
|
*pxTopOfStack = 0x55;
|
207 |
|
|
pxTopOfStack--;
|
208 |
|
|
*pxTopOfStack = 0x55;
|
209 |
|
|
|
210 |
|
|
return pxTopOfStack;
|
211 |
|
|
}
|
212 |
|
|
/*-----------------------------------------------------------*/
|
213 |
|
|
|
214 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
215 |
|
|
{
|
216 |
|
|
extern void * pxCurrentTCB;
|
217 |
|
|
|
218 |
|
|
/* Setup the hardware to generate the tick. */
|
219 |
|
|
prvSetupTimerInterrupt();
|
220 |
|
|
|
221 |
|
|
/* Restore the context of the first task that is going to run. This
|
222 |
|
|
mirrors the function epilogue code generated by the compiler when the
|
223 |
|
|
"saveall" function attribute is used. */
|
224 |
|
|
asm volatile (
|
225 |
|
|
"MOV.L @_pxCurrentTCB, ER6 \n\t"
|
226 |
|
|
"MOV.L @ER6, ER7 \n\t"
|
227 |
|
|
"LDM.L @SP+, (ER4-ER5) \n\t"
|
228 |
|
|
"LDM.L @SP+, (ER0-ER3) \n\t"
|
229 |
|
|
"MOV.L @ER7+, ER6 \n\t"
|
230 |
|
|
"RTE \n\t"
|
231 |
|
|
);
|
232 |
|
|
|
233 |
|
|
( void ) pxCurrentTCB;
|
234 |
|
|
|
235 |
|
|
/* Should not get here. */
|
236 |
|
|
return pdTRUE;
|
237 |
|
|
}
|
238 |
|
|
/*-----------------------------------------------------------*/
|
239 |
|
|
|
240 |
|
|
void vPortEndScheduler( void )
|
241 |
|
|
{
|
242 |
|
|
/* It is unlikely that the h8 port will get stopped. */
|
243 |
|
|
}
|
244 |
|
|
/*-----------------------------------------------------------*/
|
245 |
|
|
|
246 |
|
|
/*
|
247 |
|
|
* Manual context switch. This is a trap handler. The "saveall" function
|
248 |
|
|
* attribute is used so the context is saved by the compiler prologue. All
|
249 |
|
|
* we have to do is save the stack pointer.
|
250 |
|
|
*/
|
251 |
|
|
void vPortYield( void )
|
252 |
|
|
{
|
253 |
|
|
portSAVE_STACK_POINTER();
|
254 |
|
|
vTaskSwitchContext();
|
255 |
|
|
portRESTORE_STACK_POINTER();
|
256 |
|
|
}
|
257 |
|
|
/*-----------------------------------------------------------*/
|
258 |
|
|
|
259 |
|
|
/*
|
260 |
|
|
* The interrupt handler installed for the RTOS tick depends on whether the
|
261 |
|
|
* preemptive or cooperative scheduler is being used.
|
262 |
|
|
*/
|
263 |
|
|
#if( configUSE_PREEMPTION == 1 )
|
264 |
|
|
|
265 |
|
|
/*
|
266 |
|
|
* The preemptive scheduler is used so the ISR calls vTaskSwitchContext().
|
267 |
|
|
* The function prologue saves the context so all we have to do is save
|
268 |
|
|
* the stack pointer.
|
269 |
|
|
*/
|
270 |
|
|
void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );
|
271 |
|
|
void vTickISR( void )
|
272 |
|
|
{
|
273 |
|
|
portSAVE_STACK_POINTER();
|
274 |
|
|
|
275 |
|
|
vTaskIncrementTick();
|
276 |
|
|
vTaskSwitchContext();
|
277 |
|
|
|
278 |
|
|
/* Clear the interrupt. */
|
279 |
|
|
TSR1 &= ~0x01;
|
280 |
|
|
|
281 |
|
|
portRESTORE_STACK_POINTER();
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
#else
|
285 |
|
|
|
286 |
|
|
/*
|
287 |
|
|
* The cooperative scheduler is being used so all we have to do is
|
288 |
|
|
* periodically increment the tick. This can just be a normal ISR and
|
289 |
|
|
* the "saveall" attribute is not required.
|
290 |
|
|
*/
|
291 |
|
|
void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );
|
292 |
|
|
void vTickISR( void )
|
293 |
|
|
{
|
294 |
|
|
vTaskIncrementTick();
|
295 |
|
|
|
296 |
|
|
/* Clear the interrupt. */
|
297 |
|
|
TSR1 &= ~0x01;
|
298 |
|
|
}
|
299 |
|
|
|
300 |
|
|
#endif
|
301 |
|
|
/*-----------------------------------------------------------*/
|
302 |
|
|
|
303 |
|
|
/*
|
304 |
|
|
* Setup timer 1 compare match to generate a tick interrupt.
|
305 |
|
|
*/
|
306 |
|
|
static void prvSetupTimerInterrupt( void )
|
307 |
|
|
{
|
308 |
|
|
const unsigned long ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;
|
309 |
|
|
|
310 |
|
|
/* Turn the module on. */
|
311 |
|
|
MSTPCR &= ~portMSTP13;
|
312 |
|
|
|
313 |
|
|
/* Configure timer 1. */
|
314 |
|
|
TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;
|
315 |
|
|
|
316 |
|
|
/* Configure the compare match value for a tick of configTICK_RATE_HZ. */
|
317 |
|
|
TGR1A = ulCompareMatch;
|
318 |
|
|
|
319 |
|
|
/* Start the timer and enable the interrupt - we can do this here as
|
320 |
|
|
interrupts are globally disabled when this function is called. */
|
321 |
|
|
TIER1 |= portTGRA_INTERRUPT_ENABLE;
|
322 |
|
|
TSTR |= portTIMER_CHANNEL;
|
323 |
|
|
}
|
324 |
|
|
/*-----------------------------------------------------------*/
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|