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 |
|
|
/*-----------------------------------------------------------
|
56 |
|
|
* Components that can be compiled to either ARM or THUMB mode are
|
57 |
|
|
* contained in port.c The ISR routines, which can only be compiled
|
58 |
|
|
* to ARM mode, are contained in this file.
|
59 |
|
|
*----------------------------------------------------------*/
|
60 |
|
|
|
61 |
|
|
/*
|
62 |
|
|
Changes from V3.2.4
|
63 |
|
|
|
64 |
|
|
+ The assembler statements are now included in a single asm block rather
|
65 |
|
|
than each line having its own asm block.
|
66 |
|
|
*/
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
/* Scheduler includes. */
|
70 |
|
|
#include "FreeRTOS.h"
|
71 |
|
|
#include "task.h"
|
72 |
|
|
|
73 |
|
|
/* Constants required to handle interrupts. */
|
74 |
|
|
#define portCLEAR_AIC_INTERRUPT ( ( unsigned long ) 0 )
|
75 |
|
|
|
76 |
|
|
/* Constants required to handle critical sections. */
|
77 |
|
|
#define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
|
78 |
|
|
volatile unsigned long ulCriticalNesting = 9999UL;
|
79 |
|
|
|
80 |
|
|
/*-----------------------------------------------------------*/
|
81 |
|
|
|
82 |
|
|
/* ISR to handle manual context switches (from a call to taskYIELD()). */
|
83 |
|
|
void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));
|
84 |
|
|
|
85 |
|
|
/*
|
86 |
|
|
* The scheduler can only be started from ARM mode, hence the inclusion of this
|
87 |
|
|
* function here.
|
88 |
|
|
*/
|
89 |
|
|
void vPortISRStartFirstTask( void );
|
90 |
|
|
/*-----------------------------------------------------------*/
|
91 |
|
|
|
92 |
|
|
void vPortISRStartFirstTask( void )
|
93 |
|
|
{
|
94 |
|
|
/* Simply start the scheduler. This is included here as it can only be
|
95 |
|
|
called from ARM mode. */
|
96 |
|
|
portRESTORE_CONTEXT();
|
97 |
|
|
}
|
98 |
|
|
/*-----------------------------------------------------------*/
|
99 |
|
|
|
100 |
|
|
/*
|
101 |
|
|
* Called by portYIELD() or taskYIELD() to manually force a context switch.
|
102 |
|
|
*
|
103 |
|
|
* When a context switch is performed from the task level the saved task
|
104 |
|
|
* context is made to look as if it occurred from within the tick ISR. This
|
105 |
|
|
* way the same restore context function can be used when restoring the context
|
106 |
|
|
* saved from the ISR or that saved from a call to vPortYieldProcessor.
|
107 |
|
|
*/
|
108 |
|
|
void vPortYieldProcessor( void )
|
109 |
|
|
{
|
110 |
|
|
/* Within an IRQ ISR the link register has an offset from the true return
|
111 |
|
|
address, but an SWI ISR does not. Add the offset manually so the same
|
112 |
|
|
ISR return code can be used in both cases. */
|
113 |
|
|
asm volatile ( "ADD LR, LR, #4" );
|
114 |
|
|
|
115 |
|
|
/* Perform the context switch. First save the context of the current task. */
|
116 |
|
|
portSAVE_CONTEXT();
|
117 |
|
|
|
118 |
|
|
/* Find the highest priority task that is ready to run. */
|
119 |
|
|
vTaskSwitchContext();
|
120 |
|
|
|
121 |
|
|
/* Restore the context of the new task. */
|
122 |
|
|
portRESTORE_CONTEXT();
|
123 |
|
|
}
|
124 |
|
|
/*-----------------------------------------------------------*/
|
125 |
|
|
|
126 |
|
|
/*
|
127 |
|
|
* The ISR used for the scheduler tick depends on whether the cooperative or
|
128 |
|
|
* the preemptive scheduler is being used.
|
129 |
|
|
*/
|
130 |
|
|
|
131 |
|
|
#if configUSE_PREEMPTION == 0
|
132 |
|
|
|
133 |
|
|
/* The cooperative scheduler requires a normal IRQ service routine to
|
134 |
|
|
simply increment the system tick. */
|
135 |
|
|
void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));
|
136 |
|
|
void vNonPreemptiveTick( void )
|
137 |
|
|
{
|
138 |
|
|
static volatile unsigned long ulDummy;
|
139 |
|
|
|
140 |
|
|
/* Clear tick timer interrupt indication. */
|
141 |
|
|
ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
|
142 |
|
|
|
143 |
|
|
vTaskIncrementTick();
|
144 |
|
|
|
145 |
|
|
/* Acknowledge the interrupt at AIC level... */
|
146 |
|
|
AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
#else /* else preemption is turned on */
|
150 |
|
|
|
151 |
|
|
/* The preemptive scheduler is defined as "naked" as the full context is
|
152 |
|
|
saved on entry as part of the context switch. */
|
153 |
|
|
void vPreemptiveTick( void ) __attribute__((naked));
|
154 |
|
|
void vPreemptiveTick( void )
|
155 |
|
|
{
|
156 |
|
|
/* Save the context of the interrupted task. */
|
157 |
|
|
portSAVE_CONTEXT();
|
158 |
|
|
|
159 |
|
|
/* WARNING - Do not use local (stack) variables here. Use globals
|
160 |
|
|
if you must! */
|
161 |
|
|
static volatile unsigned long ulDummy;
|
162 |
|
|
|
163 |
|
|
/* Clear tick timer interrupt indication. */
|
164 |
|
|
ulDummy = portTIMER_REG_BASE_PTR->TC_SR;
|
165 |
|
|
|
166 |
|
|
/* Increment the RTOS tick count, then look for the highest priority
|
167 |
|
|
task that is ready to run. */
|
168 |
|
|
vTaskIncrementTick();
|
169 |
|
|
vTaskSwitchContext();
|
170 |
|
|
|
171 |
|
|
/* Acknowledge the interrupt at AIC level... */
|
172 |
|
|
AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;
|
173 |
|
|
|
174 |
|
|
/* Restore the context of the new task. */
|
175 |
|
|
portRESTORE_CONTEXT();
|
176 |
|
|
}
|
177 |
|
|
|
178 |
|
|
#endif
|
179 |
|
|
/*-----------------------------------------------------------*/
|
180 |
|
|
|
181 |
|
|
/*
|
182 |
|
|
* The interrupt management utilities can only be called from ARM mode. When
|
183 |
|
|
* THUMB_INTERWORK is defined the utilities are defined as functions here to
|
184 |
|
|
* ensure a switch to ARM mode. When THUMB_INTERWORK is not defined then
|
185 |
|
|
* the utilities are defined as macros in portmacro.h - as per other ports.
|
186 |
|
|
*/
|
187 |
|
|
#ifdef THUMB_INTERWORK
|
188 |
|
|
|
189 |
|
|
void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
|
190 |
|
|
void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));
|
191 |
|
|
|
192 |
|
|
void vPortDisableInterruptsFromThumb( void )
|
193 |
|
|
{
|
194 |
|
|
asm volatile (
|
195 |
|
|
"STMDB SP!, {R0} \n\t" /* Push R0. */
|
196 |
|
|
"MRS R0, CPSR \n\t" /* Get CPSR. */
|
197 |
|
|
"ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
|
198 |
|
|
"MSR CPSR, R0 \n\t" /* Write back modified value. */
|
199 |
|
|
"LDMIA SP!, {R0} \n\t" /* Pop R0. */
|
200 |
|
|
"BX R14" ); /* Return back to thumb. */
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
void vPortEnableInterruptsFromThumb( void )
|
204 |
|
|
{
|
205 |
|
|
asm volatile (
|
206 |
|
|
"STMDB SP!, {R0} \n\t" /* Push R0. */
|
207 |
|
|
"MRS R0, CPSR \n\t" /* Get CPSR. */
|
208 |
|
|
"BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
|
209 |
|
|
"MSR CPSR, R0 \n\t" /* Write back modified value. */
|
210 |
|
|
"LDMIA SP!, {R0} \n\t" /* Pop R0. */
|
211 |
|
|
"BX R14" ); /* Return back to thumb. */
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
#endif /* THUMB_INTERWORK */
|
215 |
|
|
|
216 |
|
|
/* The code generated by the GCC compiler uses the stack in different ways at
|
217 |
|
|
different optimisation levels. The interrupt flags can therefore not always
|
218 |
|
|
be saved to the stack. Instead the critical section nesting level is stored
|
219 |
|
|
in a variable, which is then saved as part of the stack context. */
|
220 |
|
|
void vPortEnterCritical( void )
|
221 |
|
|
{
|
222 |
|
|
/* Disable interrupts as per portDISABLE_INTERRUPTS(); */
|
223 |
|
|
asm volatile (
|
224 |
|
|
"STMDB SP!, {R0} \n\t" /* Push R0. */
|
225 |
|
|
"MRS R0, CPSR \n\t" /* Get CPSR. */
|
226 |
|
|
"ORR R0, R0, #0xC0 \n\t" /* Disable IRQ, FIQ. */
|
227 |
|
|
"MSR CPSR, R0 \n\t" /* Write back modified value. */
|
228 |
|
|
"LDMIA SP!, {R0}" ); /* Pop R0. */
|
229 |
|
|
|
230 |
|
|
/* Now interrupts are disabled ulCriticalNesting can be accessed
|
231 |
|
|
directly. Increment ulCriticalNesting to keep a count of how many times
|
232 |
|
|
portENTER_CRITICAL() has been called. */
|
233 |
|
|
ulCriticalNesting++;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
void vPortExitCritical( void )
|
237 |
|
|
{
|
238 |
|
|
if( ulCriticalNesting > portNO_CRITICAL_NESTING )
|
239 |
|
|
{
|
240 |
|
|
/* Decrement the nesting count as we are leaving a critical section. */
|
241 |
|
|
ulCriticalNesting--;
|
242 |
|
|
|
243 |
|
|
/* If the nesting level has reached zero then interrupts should be
|
244 |
|
|
re-enabled. */
|
245 |
|
|
if( ulCriticalNesting == portNO_CRITICAL_NESTING )
|
246 |
|
|
{
|
247 |
|
|
/* Enable interrupts as per portEXIT_CRITICAL(). */
|
248 |
|
|
asm volatile (
|
249 |
|
|
"STMDB SP!, {R0} \n\t" /* Push R0. */
|
250 |
|
|
"MRS R0, CPSR \n\t" /* Get CPSR. */
|
251 |
|
|
"BIC R0, R0, #0xC0 \n\t" /* Enable IRQ, FIQ. */
|
252 |
|
|
"MSR CPSR, R0 \n\t" /* Write back modified value. */
|
253 |
|
|
"LDMIA SP!, {R0}" ); /* Pop R0. */
|
254 |
|
|
}
|
255 |
|
|
}
|
256 |
|
|
}
|
257 |
|
|
|