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