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 |
|
|
/*
|
55 |
|
|
Changes from V4.2.1
|
56 |
|
|
|
57 |
|
|
+ Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
|
58 |
|
|
*/
|
59 |
|
|
|
60 |
|
|
/*-----------------------------------------------------------
|
61 |
|
|
* Implementation of functions defined in portable.h for the PIC24 port.
|
62 |
|
|
*----------------------------------------------------------*/
|
63 |
|
|
|
64 |
|
|
/* Scheduler include files. */
|
65 |
|
|
#include "FreeRTOS.h"
|
66 |
|
|
#include "task.h"
|
67 |
|
|
|
68 |
|
|
/* Hardware specifics. */
|
69 |
|
|
#define portBIT_SET 1
|
70 |
|
|
#define portTIMER_PRESCALE 8
|
71 |
|
|
#define portINITIAL_SR 0
|
72 |
|
|
|
73 |
|
|
/* Defined for backward compatability with project created prior to
|
74 |
|
|
FreeRTOS.org V4.3.0. */
|
75 |
|
|
#ifndef configKERNEL_INTERRUPT_PRIORITY
|
76 |
|
|
#define configKERNEL_INTERRUPT_PRIORITY 1
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
/* The program counter is only 23 bits. */
|
80 |
|
|
#define portUNUSED_PR_BITS 0x7f
|
81 |
|
|
|
82 |
|
|
/* Records the nesting depth of calls to portENTER_CRITICAL(). */
|
83 |
|
|
unsigned portBASE_TYPE uxCriticalNesting = 0xef;
|
84 |
|
|
|
85 |
|
|
#if configKERNEL_INTERRUPT_PRIORITY != 1
|
86 |
|
|
#error If configKERNEL_INTERRUPT_PRIORITY is not 1 then the #32 in the following macros needs changing to equal the portINTERRUPT_BITS value, which is ( configKERNEL_INTERRUPT_PRIORITY << 5 )
|
87 |
|
|
#endif
|
88 |
|
|
|
89 |
|
|
#ifdef MPLAB_PIC24_PORT
|
90 |
|
|
|
91 |
|
|
#define portRESTORE_CONTEXT() \
|
92 |
|
|
asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
|
93 |
|
|
"MOV [W0], W15 \n" \
|
94 |
|
|
"POP W0 \n" /* Restore the critical nesting counter for the task. */ \
|
95 |
|
|
"MOV W0, _uxCriticalNesting \n" \
|
96 |
|
|
"POP PSVPAG \n" \
|
97 |
|
|
"POP CORCON \n" \
|
98 |
|
|
"POP TBLPAG \n" \
|
99 |
|
|
"POP RCOUNT \n" /* Restore the registers from the stack. */ \
|
100 |
|
|
"POP W14 \n" \
|
101 |
|
|
"POP.D W12 \n" \
|
102 |
|
|
"POP.D W10 \n" \
|
103 |
|
|
"POP.D W8 \n" \
|
104 |
|
|
"POP.D W6 \n" \
|
105 |
|
|
"POP.D W4 \n" \
|
106 |
|
|
"POP.D W2 \n" \
|
107 |
|
|
"POP.D W0 \n" \
|
108 |
|
|
"POP SR " );
|
109 |
|
|
|
110 |
|
|
#endif /* MPLAB_PIC24_PORT */
|
111 |
|
|
|
112 |
|
|
#ifdef MPLAB_DSPIC_PORT
|
113 |
|
|
|
114 |
|
|
#define portRESTORE_CONTEXT() \
|
115 |
|
|
asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
|
116 |
|
|
"MOV [W0], W15 \n" \
|
117 |
|
|
"POP W0 \n" /* Restore the critical nesting counter for the task. */ \
|
118 |
|
|
"MOV W0, _uxCriticalNesting \n" \
|
119 |
|
|
"POP PSVPAG \n" \
|
120 |
|
|
"POP CORCON \n" \
|
121 |
|
|
"POP DOENDH \n" \
|
122 |
|
|
"POP DOENDL \n" \
|
123 |
|
|
"POP DOSTARTH \n" \
|
124 |
|
|
"POP DOSTARTL \n" \
|
125 |
|
|
"POP DCOUNT \n" \
|
126 |
|
|
"POP ACCBU \n" \
|
127 |
|
|
"POP ACCBH \n" \
|
128 |
|
|
"POP ACCBL \n" \
|
129 |
|
|
"POP ACCAU \n" \
|
130 |
|
|
"POP ACCAH \n" \
|
131 |
|
|
"POP ACCAL \n" \
|
132 |
|
|
"POP TBLPAG \n" \
|
133 |
|
|
"POP RCOUNT \n" /* Restore the registers from the stack. */ \
|
134 |
|
|
"POP W14 \n" \
|
135 |
|
|
"POP.D W12 \n" \
|
136 |
|
|
"POP.D W10 \n" \
|
137 |
|
|
"POP.D W8 \n" \
|
138 |
|
|
"POP.D W6 \n" \
|
139 |
|
|
"POP.D W4 \n" \
|
140 |
|
|
"POP.D W2 \n" \
|
141 |
|
|
"POP.D W0 \n" \
|
142 |
|
|
"POP SR " );
|
143 |
|
|
|
144 |
|
|
#endif /* MPLAB_DSPIC_PORT */
|
145 |
|
|
|
146 |
|
|
/*
|
147 |
|
|
* Setup the timer used to generate the tick interrupt.
|
148 |
|
|
*/
|
149 |
|
|
static void prvSetupTimerInterrupt( void );
|
150 |
|
|
|
151 |
|
|
/*
|
152 |
|
|
* See header file for description.
|
153 |
|
|
*/
|
154 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
155 |
|
|
{
|
156 |
|
|
unsigned short usCode;
|
157 |
|
|
portBASE_TYPE i;
|
158 |
|
|
|
159 |
|
|
const portSTACK_TYPE xInitialStack[] =
|
160 |
|
|
{
|
161 |
|
|
0x1111, /* W1 */
|
162 |
|
|
0x2222, /* W2 */
|
163 |
|
|
0x3333, /* W3 */
|
164 |
|
|
0x4444, /* W4 */
|
165 |
|
|
0x5555, /* W5 */
|
166 |
|
|
0x6666, /* W6 */
|
167 |
|
|
0x7777, /* W7 */
|
168 |
|
|
0x8888, /* W8 */
|
169 |
|
|
0x9999, /* W9 */
|
170 |
|
|
0xaaaa, /* W10 */
|
171 |
|
|
0xbbbb, /* W11 */
|
172 |
|
|
0xcccc, /* W12 */
|
173 |
|
|
0xdddd, /* W13 */
|
174 |
|
|
0xeeee, /* W14 */
|
175 |
|
|
0xcdce, /* RCOUNT */
|
176 |
|
|
0xabac, /* TBLPAG */
|
177 |
|
|
|
178 |
|
|
/* dsPIC specific registers. */
|
179 |
|
|
#ifdef MPLAB_DSPIC_PORT
|
180 |
|
|
0x0202, /* ACCAL */
|
181 |
|
|
0x0303, /* ACCAH */
|
182 |
|
|
0x0404, /* ACCAU */
|
183 |
|
|
0x0505, /* ACCBL */
|
184 |
|
|
0x0606, /* ACCBH */
|
185 |
|
|
0x0707, /* ACCBU */
|
186 |
|
|
0x0808, /* DCOUNT */
|
187 |
|
|
0x090a, /* DOSTARTL */
|
188 |
|
|
0x1010, /* DOSTARTH */
|
189 |
|
|
0x1110, /* DOENDL */
|
190 |
|
|
0x1212, /* DOENDH */
|
191 |
|
|
#endif
|
192 |
|
|
};
|
193 |
|
|
|
194 |
|
|
/* Setup the stack as if a yield had occurred.
|
195 |
|
|
|
196 |
|
|
Save the low bytes of the program counter. */
|
197 |
|
|
usCode = ( unsigned short ) pxCode;
|
198 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) usCode;
|
199 |
|
|
pxTopOfStack++;
|
200 |
|
|
|
201 |
|
|
/* Save the high byte of the program counter. This will always be zero
|
202 |
|
|
here as it is passed in a 16bit pointer. If the address is greater than
|
203 |
|
|
16 bits then the pointer will point to a jump table. */
|
204 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) 0;
|
205 |
|
|
pxTopOfStack++;
|
206 |
|
|
|
207 |
|
|
/* Status register with interrupts enabled. */
|
208 |
|
|
*pxTopOfStack = portINITIAL_SR;
|
209 |
|
|
pxTopOfStack++;
|
210 |
|
|
|
211 |
|
|
/* Parameters are passed in W0. */
|
212 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
|
213 |
|
|
pxTopOfStack++;
|
214 |
|
|
|
215 |
|
|
for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )
|
216 |
|
|
{
|
217 |
|
|
*pxTopOfStack = xInitialStack[ i ];
|
218 |
|
|
pxTopOfStack++;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
*pxTopOfStack = CORCON;
|
222 |
|
|
pxTopOfStack++;
|
223 |
|
|
*pxTopOfStack = PSVPAG;
|
224 |
|
|
pxTopOfStack++;
|
225 |
|
|
|
226 |
|
|
/* Finally the critical nesting depth. */
|
227 |
|
|
*pxTopOfStack = 0x00;
|
228 |
|
|
pxTopOfStack++;
|
229 |
|
|
|
230 |
|
|
return pxTopOfStack;
|
231 |
|
|
}
|
232 |
|
|
/*-----------------------------------------------------------*/
|
233 |
|
|
|
234 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
235 |
|
|
{
|
236 |
|
|
/* Setup a timer for the tick ISR. */
|
237 |
|
|
prvSetupTimerInterrupt();
|
238 |
|
|
|
239 |
|
|
/* Restore the context of the first task to run. */
|
240 |
|
|
portRESTORE_CONTEXT();
|
241 |
|
|
|
242 |
|
|
/* Simulate the end of the yield function. */
|
243 |
|
|
asm volatile ( "return" );
|
244 |
|
|
|
245 |
|
|
/* Should not reach here. */
|
246 |
|
|
return pdTRUE;
|
247 |
|
|
}
|
248 |
|
|
/*-----------------------------------------------------------*/
|
249 |
|
|
|
250 |
|
|
void vPortEndScheduler( void )
|
251 |
|
|
{
|
252 |
|
|
/* It is unlikely that the scheduler for the PIC port will get stopped
|
253 |
|
|
once running. If required disable the tick interrupt here, then return
|
254 |
|
|
to xPortStartScheduler(). */
|
255 |
|
|
}
|
256 |
|
|
/*-----------------------------------------------------------*/
|
257 |
|
|
|
258 |
|
|
/*
|
259 |
|
|
* Setup a timer for a regular tick.
|
260 |
|
|
*/
|
261 |
|
|
static void prvSetupTimerInterrupt( void )
|
262 |
|
|
{
|
263 |
|
|
const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
|
264 |
|
|
|
265 |
|
|
/* Prescale of 8. */
|
266 |
|
|
T1CON = 0;
|
267 |
|
|
TMR1 = 0;
|
268 |
|
|
|
269 |
|
|
PR1 = ( unsigned short ) ulCompareMatch;
|
270 |
|
|
|
271 |
|
|
/* Setup timer 1 interrupt priority. */
|
272 |
|
|
IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
|
273 |
|
|
|
274 |
|
|
/* Clear the interrupt as a starting condition. */
|
275 |
|
|
IFS0bits.T1IF = 0;
|
276 |
|
|
|
277 |
|
|
/* Enable the interrupt. */
|
278 |
|
|
IEC0bits.T1IE = 1;
|
279 |
|
|
|
280 |
|
|
/* Setup the prescale value. */
|
281 |
|
|
T1CONbits.TCKPS0 = 1;
|
282 |
|
|
T1CONbits.TCKPS1 = 0;
|
283 |
|
|
|
284 |
|
|
/* Start the timer. */
|
285 |
|
|
T1CONbits.TON = 1;
|
286 |
|
|
}
|
287 |
|
|
/*-----------------------------------------------------------*/
|
288 |
|
|
|
289 |
|
|
void vPortEnterCritical( void )
|
290 |
|
|
{
|
291 |
|
|
portDISABLE_INTERRUPTS();
|
292 |
|
|
uxCriticalNesting++;
|
293 |
|
|
}
|
294 |
|
|
/*-----------------------------------------------------------*/
|
295 |
|
|
|
296 |
|
|
void vPortExitCritical( void )
|
297 |
|
|
{
|
298 |
|
|
uxCriticalNesting--;
|
299 |
|
|
if( uxCriticalNesting == 0 )
|
300 |
|
|
{
|
301 |
|
|
portENABLE_INTERRUPTS();
|
302 |
|
|
}
|
303 |
|
|
}
|
304 |
|
|
/*-----------------------------------------------------------*/
|
305 |
|
|
|
306 |
|
|
void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )
|
307 |
|
|
{
|
308 |
|
|
/* Clear the timer interrupt. */
|
309 |
|
|
IFS0bits.T1IF = 0;
|
310 |
|
|
|
311 |
|
|
vTaskIncrementTick();
|
312 |
|
|
|
313 |
|
|
#if configUSE_PREEMPTION == 1
|
314 |
|
|
portYIELD();
|
315 |
|
|
#endif
|
316 |
|
|
}
|