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 |
|
|
* Implementation of functions defined in portable.h for the ARM CM3 port.
|
56 |
|
|
*----------------------------------------------------------*/
|
57 |
|
|
|
58 |
|
|
/* Scheduler includes. */
|
59 |
|
|
#include "FreeRTOS.h"
|
60 |
|
|
#include "task.h"
|
61 |
|
|
|
62 |
|
|
#ifndef configKERNEL_INTERRUPT_PRIORITY
|
63 |
|
|
#define configKERNEL_INTERRUPT_PRIORITY 255
|
64 |
|
|
#endif
|
65 |
|
|
|
66 |
|
|
/* Constants required to manipulate the NVIC. */
|
67 |
|
|
#define portNVIC_SYSTICK_CTRL ( ( volatile unsigned long *) 0xe000e010 )
|
68 |
|
|
#define portNVIC_SYSTICK_LOAD ( ( volatile unsigned long *) 0xe000e014 )
|
69 |
|
|
#define portNVIC_INT_CTRL ( ( volatile unsigned long *) 0xe000ed04 )
|
70 |
|
|
#define portNVIC_SYSPRI2 ( ( volatile unsigned long *) 0xe000ed20 )
|
71 |
|
|
#define portNVIC_SYSTICK_CLK 0x00000004
|
72 |
|
|
#define portNVIC_SYSTICK_INT 0x00000002
|
73 |
|
|
#define portNVIC_SYSTICK_ENABLE 0x00000001
|
74 |
|
|
#define portNVIC_PENDSVSET 0x10000000
|
75 |
|
|
#define portNVIC_PENDSV_PRI ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )
|
76 |
|
|
#define portNVIC_SYSTICK_PRI ( ( ( unsigned long ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )
|
77 |
|
|
|
78 |
|
|
/* Constants required to set up the initial stack. */
|
79 |
|
|
#define portINITIAL_XPSR ( 0x01000000 )
|
80 |
|
|
|
81 |
|
|
/* Each task maintains its own interrupt status in the critical nesting
|
82 |
|
|
variable. */
|
83 |
|
|
static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;
|
84 |
|
|
|
85 |
|
|
/*
|
86 |
|
|
* Setup the timer to generate the tick interrupts.
|
87 |
|
|
*/
|
88 |
|
|
static void prvSetupTimerInterrupt( void );
|
89 |
|
|
|
90 |
|
|
/*
|
91 |
|
|
* Exception handlers.
|
92 |
|
|
*/
|
93 |
|
|
void xPortPendSVHandler( void );
|
94 |
|
|
void xPortSysTickHandler( void );
|
95 |
|
|
void vPortSVCHandler( void );
|
96 |
|
|
|
97 |
|
|
/*
|
98 |
|
|
* Start first task is a separate function so it can be tested in isolation.
|
99 |
|
|
*/
|
100 |
|
|
void vPortStartFirstTask( void );
|
101 |
|
|
|
102 |
|
|
/*-----------------------------------------------------------*/
|
103 |
|
|
|
104 |
|
|
/*
|
105 |
|
|
* See header file for description.
|
106 |
|
|
*/
|
107 |
|
|
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
|
108 |
|
|
{
|
109 |
|
|
/* Simulate the stack frame as it would be created by a context switch
|
110 |
|
|
interrupt. */
|
111 |
|
|
pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
|
112 |
|
|
*pxTopOfStack = portINITIAL_XPSR; /* xPSR */
|
113 |
|
|
pxTopOfStack--;
|
114 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* PC */
|
115 |
|
|
pxTopOfStack--;
|
116 |
|
|
*pxTopOfStack = 0; /* LR */
|
117 |
|
|
pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
|
118 |
|
|
*pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
|
119 |
|
|
pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
|
120 |
|
|
|
121 |
|
|
return pxTopOfStack;
|
122 |
|
|
}
|
123 |
|
|
/*-----------------------------------------------------------*/
|
124 |
|
|
|
125 |
|
|
__asm void vPortSVCHandler( void )
|
126 |
|
|
{
|
127 |
|
|
PRESERVE8
|
128 |
|
|
|
129 |
|
|
ldr r3, =pxCurrentTCB /* Restore the context. */
|
130 |
|
|
ldr r1, [r3] /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
|
131 |
|
|
ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
|
132 |
|
|
ldmia r0!, {r4-r11} /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
|
133 |
|
|
msr psp, r0 /* Restore the task stack pointer. */
|
134 |
|
|
mov r0, #0
|
135 |
|
|
msr basepri, r0
|
136 |
|
|
orr r14, #0xd
|
137 |
|
|
bx r14
|
138 |
|
|
}
|
139 |
|
|
/*-----------------------------------------------------------*/
|
140 |
|
|
|
141 |
|
|
__asm void vPortStartFirstTask( void )
|
142 |
|
|
{
|
143 |
|
|
PRESERVE8
|
144 |
|
|
|
145 |
|
|
/* Use the NVIC offset register to locate the stack. */
|
146 |
|
|
ldr r0, =0xE000ED08
|
147 |
|
|
ldr r0, [r0]
|
148 |
|
|
ldr r0, [r0]
|
149 |
|
|
/* Set the msp back to the start of the stack. */
|
150 |
|
|
msr msp, r0
|
151 |
|
|
/* Globally enable interrupts. */
|
152 |
|
|
cpsie i
|
153 |
|
|
/* Call SVC to start the first task. */
|
154 |
|
|
svc 0
|
155 |
|
|
nop
|
156 |
|
|
}
|
157 |
|
|
/*-----------------------------------------------------------*/
|
158 |
|
|
|
159 |
|
|
/*
|
160 |
|
|
* See header file for description.
|
161 |
|
|
*/
|
162 |
|
|
portBASE_TYPE xPortStartScheduler( void )
|
163 |
|
|
{
|
164 |
|
|
/* Make PendSV, CallSV and SysTick the same priroity as the kernel. */
|
165 |
|
|
*(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;
|
166 |
|
|
*(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;
|
167 |
|
|
|
168 |
|
|
/* Start the timer that generates the tick ISR. Interrupts are disabled
|
169 |
|
|
here already. */
|
170 |
|
|
prvSetupTimerInterrupt();
|
171 |
|
|
|
172 |
|
|
/* Initialise the critical nesting count ready for the first task. */
|
173 |
|
|
uxCriticalNesting = 0;
|
174 |
|
|
|
175 |
|
|
/* Start the first task. */
|
176 |
|
|
vPortStartFirstTask();
|
177 |
|
|
|
178 |
|
|
/* Should not get here! */
|
179 |
|
|
return 0;
|
180 |
|
|
}
|
181 |
|
|
/*-----------------------------------------------------------*/
|
182 |
|
|
|
183 |
|
|
void vPortEndScheduler( void )
|
184 |
|
|
{
|
185 |
|
|
/* It is unlikely that the CM3 port will require this function as there
|
186 |
|
|
is nothing to return to. */
|
187 |
|
|
}
|
188 |
|
|
/*-----------------------------------------------------------*/
|
189 |
|
|
|
190 |
|
|
void vPortYieldFromISR( void )
|
191 |
|
|
{
|
192 |
|
|
/* Set a PendSV to request a context switch. */
|
193 |
|
|
*(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
|
194 |
|
|
}
|
195 |
|
|
/*-----------------------------------------------------------*/
|
196 |
|
|
|
197 |
|
|
void vPortEnterCritical( void )
|
198 |
|
|
{
|
199 |
|
|
portDISABLE_INTERRUPTS();
|
200 |
|
|
uxCriticalNesting++;
|
201 |
|
|
}
|
202 |
|
|
/*-----------------------------------------------------------*/
|
203 |
|
|
|
204 |
|
|
void vPortExitCritical( void )
|
205 |
|
|
{
|
206 |
|
|
uxCriticalNesting--;
|
207 |
|
|
if( uxCriticalNesting == 0 )
|
208 |
|
|
{
|
209 |
|
|
portENABLE_INTERRUPTS();
|
210 |
|
|
}
|
211 |
|
|
}
|
212 |
|
|
/*-----------------------------------------------------------*/
|
213 |
|
|
|
214 |
|
|
__asm void xPortPendSVHandler( void )
|
215 |
|
|
{
|
216 |
|
|
extern uxCriticalNesting;
|
217 |
|
|
extern pxCurrentTCB;
|
218 |
|
|
extern vTaskSwitchContext;
|
219 |
|
|
|
220 |
|
|
PRESERVE8
|
221 |
|
|
|
222 |
|
|
mrs r0, psp
|
223 |
|
|
|
224 |
|
|
ldr r3, =pxCurrentTCB /* Get the location of the current TCB. */
|
225 |
|
|
ldr r2, [r3]
|
226 |
|
|
|
227 |
|
|
stmdb r0!, {r4-r11} /* Save the remaining registers. */
|
228 |
|
|
str r0, [r2] /* Save the new top of stack into the first member of the TCB. */
|
229 |
|
|
|
230 |
|
|
stmdb sp!, {r3, r14}
|
231 |
|
|
mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
|
232 |
|
|
msr basepri, r0
|
233 |
|
|
bl vTaskSwitchContext
|
234 |
|
|
mov r0, #0
|
235 |
|
|
msr basepri, r0
|
236 |
|
|
ldmia sp!, {r3, r14}
|
237 |
|
|
|
238 |
|
|
ldr r1, [r3]
|
239 |
|
|
ldr r0, [r1] /* The first item in pxCurrentTCB is the task top of stack. */
|
240 |
|
|
ldmia r0!, {r4-r11} /* Pop the registers and the critical nesting count. */
|
241 |
|
|
msr psp, r0
|
242 |
|
|
bx r14
|
243 |
|
|
nop
|
244 |
|
|
}
|
245 |
|
|
/*-----------------------------------------------------------*/
|
246 |
|
|
|
247 |
|
|
void xPortSysTickHandler( void )
|
248 |
|
|
{
|
249 |
|
|
unsigned long ulDummy;
|
250 |
|
|
|
251 |
|
|
/* If using preemption, also force a context switch. */
|
252 |
|
|
#if configUSE_PREEMPTION == 1
|
253 |
|
|
*(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
|
254 |
|
|
#endif
|
255 |
|
|
|
256 |
|
|
ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
|
257 |
|
|
{
|
258 |
|
|
vTaskIncrementTick();
|
259 |
|
|
}
|
260 |
|
|
portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
|
261 |
|
|
}
|
262 |
|
|
/*-----------------------------------------------------------*/
|
263 |
|
|
|
264 |
|
|
/*
|
265 |
|
|
* Setup the systick timer to generate the tick interrupts at the required
|
266 |
|
|
* frequency.
|
267 |
|
|
*/
|
268 |
|
|
void prvSetupTimerInterrupt( void )
|
269 |
|
|
{
|
270 |
|
|
/* Configure SysTick to interrupt at the requested rate. */
|
271 |
|
|
*(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
|
272 |
|
|
*(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
|
273 |
|
|
}
|
274 |
|
|
/*-----------------------------------------------------------*/
|
275 |
|
|
|
276 |
|
|
__asm void vPortSetInterruptMask( void )
|
277 |
|
|
{
|
278 |
|
|
PRESERVE8
|
279 |
|
|
|
280 |
|
|
push { r0 }
|
281 |
|
|
mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
|
282 |
|
|
msr basepri, r0
|
283 |
|
|
pop { r0 }
|
284 |
|
|
bx r14
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
/*-----------------------------------------------------------*/
|
288 |
|
|
|
289 |
|
|
__asm void vPortClearInterruptMask( void )
|
290 |
|
|
{
|
291 |
|
|
PRESERVE8
|
292 |
|
|
|
293 |
|
|
push { r0 }
|
294 |
|
|
mov r0, #0
|
295 |
|
|
msr basepri, r0
|
296 |
|
|
pop { r0 }
|
297 |
|
|
bx r14
|
298 |
|
|
}
|