1 |
572 |
jeremybenn |
/*This file has been prepared for Doxygen automatic documentation generation.*/
|
2 |
|
|
/*! \file *********************************************************************
|
3 |
|
|
*
|
4 |
|
|
* \brief FreeRTOS port source for AVR32 UC3.
|
5 |
|
|
*
|
6 |
|
|
* - Compiler: GNU GCC for AVR32
|
7 |
|
|
* - Supported devices: All AVR32 devices can be used.
|
8 |
|
|
* - AppNote:
|
9 |
|
|
*
|
10 |
|
|
* \author Atmel Corporation: http://www.atmel.com \n
|
11 |
|
|
* Support and FAQ: http://support.atmel.no/
|
12 |
|
|
*
|
13 |
|
|
*****************************************************************************/
|
14 |
|
|
|
15 |
|
|
/*
|
16 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
17 |
|
|
|
18 |
|
|
***************************************************************************
|
19 |
|
|
* *
|
20 |
|
|
* If you are: *
|
21 |
|
|
* *
|
22 |
|
|
* + New to FreeRTOS, *
|
23 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
24 |
|
|
* + Looking for basic training, *
|
25 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
26 |
|
|
* *
|
27 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
28 |
|
|
* *
|
29 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
30 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
31 |
|
|
* *
|
32 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
33 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
34 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
35 |
|
|
* exceptional circumstances). Thank you for your support! *
|
36 |
|
|
* *
|
37 |
|
|
***************************************************************************
|
38 |
|
|
|
39 |
|
|
This file is part of the FreeRTOS distribution.
|
40 |
|
|
|
41 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
42 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
43 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
44 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
45 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
46 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
47 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
48 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
49 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
50 |
|
|
more details. You should have received a copy of the GNU General Public
|
51 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
52 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
53 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
54 |
|
|
FreeRTOS WEB site.
|
55 |
|
|
|
56 |
|
|
1 tab == 4 spaces!
|
57 |
|
|
|
58 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
59 |
|
|
contact details.
|
60 |
|
|
|
61 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
62 |
|
|
critical systems.
|
63 |
|
|
|
64 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
65 |
|
|
licensing and training services.
|
66 |
|
|
*/
|
67 |
|
|
|
68 |
|
|
|
69 |
|
|
#ifndef PORTMACRO_H
|
70 |
|
|
#define PORTMACRO_H
|
71 |
|
|
|
72 |
|
|
/*-----------------------------------------------------------
|
73 |
|
|
* Port specific definitions.
|
74 |
|
|
*
|
75 |
|
|
* The settings in this file configure FreeRTOS correctly for the
|
76 |
|
|
* given hardware and compiler.
|
77 |
|
|
*
|
78 |
|
|
* These settings should not be altered.
|
79 |
|
|
*-----------------------------------------------------------
|
80 |
|
|
*/
|
81 |
|
|
#include <avr32/io.h>
|
82 |
|
|
#include "intc.h"
|
83 |
|
|
#include "compiler.h"
|
84 |
|
|
|
85 |
|
|
#ifdef __cplusplus
|
86 |
|
|
extern "C" {
|
87 |
|
|
#endif
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
/* Type definitions. */
|
91 |
|
|
#define portCHAR char
|
92 |
|
|
#define portFLOAT float
|
93 |
|
|
#define portDOUBLE double
|
94 |
|
|
#define portLONG long
|
95 |
|
|
#define portSHORT short
|
96 |
|
|
#define portSTACK_TYPE unsigned portLONG
|
97 |
|
|
#define portBASE_TYPE portLONG
|
98 |
|
|
|
99 |
|
|
#define TASK_DELAY_MS(x) ( (x) /portTICK_RATE_MS )
|
100 |
|
|
#define TASK_DELAY_S(x) ( (x)*1000 /portTICK_RATE_MS )
|
101 |
|
|
#define TASK_DELAY_MIN(x) ( (x)*60*1000/portTICK_RATE_MS )
|
102 |
|
|
|
103 |
|
|
#define configTICK_TC_IRQ ATPASTE2(AVR32_TC_IRQ, configTICK_TC_CHANNEL)
|
104 |
|
|
|
105 |
|
|
#if( configUSE_16_BIT_TICKS == 1 )
|
106 |
|
|
typedef unsigned portSHORT portTickType;
|
107 |
|
|
#define portMAX_DELAY ( portTickType ) 0xffff
|
108 |
|
|
#else
|
109 |
|
|
typedef unsigned portLONG portTickType;
|
110 |
|
|
#define portMAX_DELAY ( portTickType ) 0xffffffff
|
111 |
|
|
#endif
|
112 |
|
|
/*-----------------------------------------------------------*/
|
113 |
|
|
|
114 |
|
|
/* Architecture specifics. */
|
115 |
|
|
#define portSTACK_GROWTH ( -1 )
|
116 |
|
|
#define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ )
|
117 |
|
|
#define portBYTE_ALIGNMENT 4
|
118 |
|
|
#define portNOP() {__asm__ __volatile__ ("nop");}
|
119 |
|
|
/*-----------------------------------------------------------*/
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
/*-----------------------------------------------------------*/
|
123 |
|
|
|
124 |
|
|
/* INTC-specific. */
|
125 |
|
|
#define DISABLE_ALL_EXCEPTIONS() Disable_global_exception()
|
126 |
|
|
#define ENABLE_ALL_EXCEPTIONS() Enable_global_exception()
|
127 |
|
|
|
128 |
|
|
#define DISABLE_ALL_INTERRUPTS() Disable_global_interrupt()
|
129 |
|
|
#define ENABLE_ALL_INTERRUPTS() Enable_global_interrupt()
|
130 |
|
|
|
131 |
|
|
#define DISABLE_INT_LEVEL(int_lev) Disable_interrupt_level(int_lev)
|
132 |
|
|
#define ENABLE_INT_LEVEL(int_lev) Enable_interrupt_level(int_lev)
|
133 |
|
|
|
134 |
|
|
|
135 |
|
|
/*
|
136 |
|
|
* Debug trace.
|
137 |
|
|
* Activated if and only if configDBG is nonzero.
|
138 |
|
|
* Prints a formatted string to stdout.
|
139 |
|
|
* The current source file name and line number are output with a colon before
|
140 |
|
|
* the formatted string.
|
141 |
|
|
* A carriage return and a linefeed are appended to the output.
|
142 |
|
|
* stdout is redirected to the USART configured by configDBG_USART.
|
143 |
|
|
* The parameters are the same as for the standard printf function.
|
144 |
|
|
* There is no return value.
|
145 |
|
|
* SHALL NOT BE CALLED FROM WITHIN AN INTERRUPT as fputs and printf use malloc,
|
146 |
|
|
* which is interrupt-unsafe with the current __malloc_lock and __malloc_unlock.
|
147 |
|
|
*/
|
148 |
|
|
#if configDBG
|
149 |
|
|
#define portDBG_TRACE(...) \
|
150 |
|
|
{\
|
151 |
|
|
fputs(__FILE__ ":" ASTRINGZ(__LINE__) ": ", stdout);\
|
152 |
|
|
printf(__VA_ARGS__);\
|
153 |
|
|
fputs("\r\n", stdout);\
|
154 |
|
|
}
|
155 |
|
|
#else
|
156 |
|
|
#define portDBG_TRACE(...)
|
157 |
|
|
#endif
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
/* Critical section management. */
|
161 |
|
|
#define portDISABLE_INTERRUPTS() DISABLE_ALL_INTERRUPTS()
|
162 |
|
|
#define portENABLE_INTERRUPTS() ENABLE_ALL_INTERRUPTS()
|
163 |
|
|
|
164 |
|
|
|
165 |
|
|
extern void vPortEnterCritical( void );
|
166 |
|
|
extern void vPortExitCritical( void );
|
167 |
|
|
|
168 |
|
|
#define portENTER_CRITICAL() vPortEnterCritical();
|
169 |
|
|
#define portEXIT_CRITICAL() vPortExitCritical();
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
/* Added as there is no such function in FreeRTOS. */
|
173 |
|
|
extern void *pvPortRealloc( void *pv, size_t xSize );
|
174 |
|
|
/*-----------------------------------------------------------*/
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
/*=============================================================================================*/
|
178 |
|
|
|
179 |
|
|
/*
|
180 |
|
|
* Restore Context for cases other than INTi.
|
181 |
|
|
*/
|
182 |
|
|
#define portRESTORE_CONTEXT() \
|
183 |
|
|
{ \
|
184 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
185 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
186 |
|
|
\
|
187 |
|
|
__asm__ __volatile__ ( \
|
188 |
|
|
/* Set SP to point to new stack */ \
|
189 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
190 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
191 |
|
|
"ld.w r0, r8[0] \n\t"\
|
192 |
|
|
"ld.w sp, r0[0] \n\t"\
|
193 |
|
|
\
|
194 |
|
|
/* Restore ulCriticalNesting variable */ \
|
195 |
|
|
"ld.w r0, sp++ \n\t"\
|
196 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
197 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
198 |
|
|
"st.w r8[0], r0 \n\t"\
|
199 |
|
|
\
|
200 |
|
|
/* Restore R0..R7 */ \
|
201 |
|
|
"ldm sp++, r0-r7 \n\t"\
|
202 |
|
|
/* R0-R7 should not be used below this line */ \
|
203 |
|
|
/* Skip PC and SR (will do it at the end) */ \
|
204 |
|
|
"sub sp, -2*4 \n\t"\
|
205 |
|
|
/* Restore R8..R12 and LR */ \
|
206 |
|
|
"ldm sp++, r8-r12, lr \n\t"\
|
207 |
|
|
/* Restore SR */ \
|
208 |
|
|
"ld.w r0, sp[-8*4]\n\t" /* R0 is modified, is restored later. */ \
|
209 |
|
|
"mtsr %[SR], r0 \n\t"\
|
210 |
|
|
/* Restore r0 */ \
|
211 |
|
|
"ld.w r0, sp[-9*4] \n\t"\
|
212 |
|
|
/* Restore PC */ \
|
213 |
|
|
"ld.w pc, sp[-7*4]" /* Get PC from stack - PC is the 7th register saved */ \
|
214 |
|
|
: \
|
215 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting), \
|
216 |
|
|
[pxCurrentTCB] "i" (&pxCurrentTCB), \
|
217 |
|
|
[SR] "i" (AVR32_SR) \
|
218 |
|
|
); \
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
/*
|
223 |
|
|
* portSAVE_CONTEXT_INT() and portRESTORE_CONTEXT_INT(): for INT0..3 exceptions.
|
224 |
|
|
* portSAVE_CONTEXT_SCALL() and portRESTORE_CONTEXT_SCALL(): for the scall exception.
|
225 |
|
|
*
|
226 |
|
|
* Had to make different versions because registers saved on the system stack
|
227 |
|
|
* are not the same between INT0..3 exceptions and the scall exception.
|
228 |
|
|
*/
|
229 |
|
|
|
230 |
|
|
// Task context stack layout:
|
231 |
|
|
// R8 (*)
|
232 |
|
|
// R9 (*)
|
233 |
|
|
// R10 (*)
|
234 |
|
|
// R11 (*)
|
235 |
|
|
// R12 (*)
|
236 |
|
|
// R14/LR (*)
|
237 |
|
|
// R15/PC (*)
|
238 |
|
|
// SR (*)
|
239 |
|
|
// R0
|
240 |
|
|
// R1
|
241 |
|
|
// R2
|
242 |
|
|
// R3
|
243 |
|
|
// R4
|
244 |
|
|
// R5
|
245 |
|
|
// R6
|
246 |
|
|
// R7
|
247 |
|
|
// ulCriticalNesting
|
248 |
|
|
// (*) automatically done for INT0..INT3, but not for SCALL
|
249 |
|
|
|
250 |
|
|
/*
|
251 |
|
|
* The ISR used for the scheduler tick depends on whether the cooperative or
|
252 |
|
|
* the preemptive scheduler is being used.
|
253 |
|
|
*/
|
254 |
|
|
#if configUSE_PREEMPTION == 0
|
255 |
|
|
|
256 |
|
|
/*
|
257 |
|
|
* portSAVE_CONTEXT_OS_INT() for OS Tick exception.
|
258 |
|
|
*/
|
259 |
|
|
#define portSAVE_CONTEXT_OS_INT() \
|
260 |
|
|
{ \
|
261 |
|
|
/* Save R0..R7 */ \
|
262 |
|
|
__asm__ __volatile__ ("stm --sp, r0-r7"); \
|
263 |
|
|
\
|
264 |
|
|
/* With the cooperative scheduler, as there is no context switch by interrupt, */ \
|
265 |
|
|
/* there is also no context save. */ \
|
266 |
|
|
}
|
267 |
|
|
|
268 |
|
|
/*
|
269 |
|
|
* portRESTORE_CONTEXT_OS_INT() for Tick exception.
|
270 |
|
|
*/
|
271 |
|
|
#define portRESTORE_CONTEXT_OS_INT() \
|
272 |
|
|
{ \
|
273 |
|
|
__asm__ __volatile__ ( \
|
274 |
|
|
/* Restore R0..R7 */ \
|
275 |
|
|
"ldm sp++, r0-r7\n\t" \
|
276 |
|
|
\
|
277 |
|
|
/* With the cooperative scheduler, as there is no context switch by interrupt, */ \
|
278 |
|
|
/* there is also no context restore. */ \
|
279 |
|
|
"rete" \
|
280 |
|
|
); \
|
281 |
|
|
}
|
282 |
|
|
|
283 |
|
|
#else
|
284 |
|
|
|
285 |
|
|
/*
|
286 |
|
|
* portSAVE_CONTEXT_OS_INT() for OS Tick exception.
|
287 |
|
|
*/
|
288 |
|
|
#define portSAVE_CONTEXT_OS_INT() \
|
289 |
|
|
{ \
|
290 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
291 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
292 |
|
|
\
|
293 |
|
|
/* When we come here */ \
|
294 |
|
|
/* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
|
295 |
|
|
\
|
296 |
|
|
__asm__ __volatile__ ( \
|
297 |
|
|
/* Save R0..R7 */ \
|
298 |
|
|
"stm --sp, r0-r7 \n\t"\
|
299 |
|
|
\
|
300 |
|
|
/* Save ulCriticalNesting variable - R0 is overwritten */ \
|
301 |
|
|
"mov r8, LO(%[ulCriticalNesting])\n\t" \
|
302 |
|
|
"orh r8, HI(%[ulCriticalNesting])\n\t" \
|
303 |
|
|
"ld.w r0, r8[0] \n\t"\
|
304 |
|
|
"st.w --sp, r0 \n\t"\
|
305 |
|
|
\
|
306 |
|
|
/* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
|
307 |
|
|
/* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
|
308 |
|
|
/* level and allow other lower interrupt level to occur). */ \
|
309 |
|
|
/* In this case we don't want to do a task switch because we don't know what the stack */ \
|
310 |
|
|
/* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
|
311 |
|
|
/* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
|
312 |
|
|
/* will just be restoring the interrupt handler, no way!!! */ \
|
313 |
|
|
/* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
|
314 |
|
|
"ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
|
315 |
|
|
"bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
|
316 |
|
|
"cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
|
317 |
|
|
"brhi LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
|
318 |
|
|
\
|
319 |
|
|
/* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
|
320 |
|
|
/* NOTE: we don't enter a critical section here because all interrupt handlers */ \
|
321 |
|
|
/* MUST perform a SAVE_CONTEXT/RESTORE_CONTEXT in the same way as */ \
|
322 |
|
|
/* portSAVE_CONTEXT_OS_INT/port_RESTORE_CONTEXT_OS_INT if they call OS functions. */ \
|
323 |
|
|
/* => all interrupt handlers must use portENTER_SWITCHING_ISR/portEXIT_SWITCHING_ISR. */ \
|
324 |
|
|
"mov r8, LO(%[pxCurrentTCB])\n\t" \
|
325 |
|
|
"orh r8, HI(%[pxCurrentTCB])\n\t" \
|
326 |
|
|
"ld.w r0, r8[0]\n\t" \
|
327 |
|
|
"st.w r0[0], sp\n" \
|
328 |
|
|
\
|
329 |
|
|
"LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE]:" \
|
330 |
|
|
: \
|
331 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting), \
|
332 |
|
|
[pxCurrentTCB] "i" (&pxCurrentTCB), \
|
333 |
|
|
[LINE] "i" (__LINE__) \
|
334 |
|
|
); \
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
/*
|
338 |
|
|
* portRESTORE_CONTEXT_OS_INT() for Tick exception.
|
339 |
|
|
*/
|
340 |
|
|
#define portRESTORE_CONTEXT_OS_INT() \
|
341 |
|
|
{ \
|
342 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
343 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
344 |
|
|
\
|
345 |
|
|
/* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
|
346 |
|
|
/* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
|
347 |
|
|
/* level and allow other lower interrupt level to occur). */ \
|
348 |
|
|
/* In this case we don't want to do a task switch because we don't know what the stack */ \
|
349 |
|
|
/* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
|
350 |
|
|
/* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
|
351 |
|
|
/* will just be restoring the interrupt handler, no way!!! */ \
|
352 |
|
|
__asm__ __volatile__ ( \
|
353 |
|
|
"ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
|
354 |
|
|
"bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
|
355 |
|
|
"cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
|
356 |
|
|
"brhi LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]" \
|
357 |
|
|
: \
|
358 |
|
|
: [LINE] "i" (__LINE__) \
|
359 |
|
|
); \
|
360 |
|
|
\
|
361 |
|
|
/* Else */ \
|
362 |
|
|
/* because it is here safe, always call vTaskSwitchContext() since an OS tick occurred. */ \
|
363 |
|
|
/* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */\
|
364 |
|
|
portENTER_CRITICAL(); \
|
365 |
|
|
vTaskSwitchContext(); \
|
366 |
|
|
portEXIT_CRITICAL(); \
|
367 |
|
|
\
|
368 |
|
|
/* Restore all registers */ \
|
369 |
|
|
\
|
370 |
|
|
__asm__ __volatile__ ( \
|
371 |
|
|
/* Set SP to point to new stack */ \
|
372 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
373 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
374 |
|
|
"ld.w r0, r8[0] \n\t"\
|
375 |
|
|
"ld.w sp, r0[0] \n"\
|
376 |
|
|
\
|
377 |
|
|
"LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
|
378 |
|
|
\
|
379 |
|
|
/* Restore ulCriticalNesting variable */ \
|
380 |
|
|
"ld.w r0, sp++ \n\t" \
|
381 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
382 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
383 |
|
|
"st.w r8[0], r0 \n\t"\
|
384 |
|
|
\
|
385 |
|
|
/* Restore R0..R7 */ \
|
386 |
|
|
"ldm sp++, r0-r7 \n\t"\
|
387 |
|
|
\
|
388 |
|
|
/* Now, the stack should be R8..R12, LR, PC and SR */ \
|
389 |
|
|
"rete" \
|
390 |
|
|
: \
|
391 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting), \
|
392 |
|
|
[pxCurrentTCB] "i" (&pxCurrentTCB), \
|
393 |
|
|
[LINE] "i" (__LINE__) \
|
394 |
|
|
); \
|
395 |
|
|
}
|
396 |
|
|
|
397 |
|
|
#endif
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
/*
|
401 |
|
|
* portSAVE_CONTEXT_SCALL() for SupervisorCALL exception.
|
402 |
|
|
*
|
403 |
|
|
* NOTE: taskYIELD()(== SCALL) MUST NOT be called in a mode > supervisor mode.
|
404 |
|
|
*
|
405 |
|
|
*/
|
406 |
|
|
#define portSAVE_CONTEXT_SCALL() \
|
407 |
|
|
{ \
|
408 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
409 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
410 |
|
|
\
|
411 |
|
|
/* Warning: the stack layout after SCALL doesn't match the one after an interrupt. */ \
|
412 |
|
|
/* If SR[M2:M0] == 001 */ \
|
413 |
|
|
/* PC and SR are on the stack. */ \
|
414 |
|
|
/* Else (other modes) */ \
|
415 |
|
|
/* Nothing on the stack. */ \
|
416 |
|
|
\
|
417 |
|
|
/* WARNING NOTE: the else case cannot happen as it is strictly forbidden to call */ \
|
418 |
|
|
/* vTaskDelay() and vTaskDelayUntil() OS functions (that result in a taskYield()) */ \
|
419 |
|
|
/* in an interrupt|exception handler. */ \
|
420 |
|
|
\
|
421 |
|
|
__asm__ __volatile__ ( \
|
422 |
|
|
/* in order to save R0-R7 */ \
|
423 |
|
|
"sub sp, 6*4 \n\t"\
|
424 |
|
|
/* Save R0..R7 */ \
|
425 |
|
|
"stm --sp, r0-r7 \n\t"\
|
426 |
|
|
\
|
427 |
|
|
/* in order to save R8-R12 and LR */ \
|
428 |
|
|
/* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
|
429 |
|
|
"sub r7, sp,-16*4 \n\t"\
|
430 |
|
|
/* Copy PC and SR in other places in the stack. */ \
|
431 |
|
|
"ld.w r0, r7[-2*4] \n\t" /* Read SR */\
|
432 |
|
|
"st.w r7[-8*4], r0 \n\t" /* Copy SR */\
|
433 |
|
|
"ld.w r0, r7[-1*4] \n\t" /* Read PC */\
|
434 |
|
|
"st.w r7[-7*4], r0 \n\t" /* Copy PC */\
|
435 |
|
|
\
|
436 |
|
|
/* Save R8..R12 and LR on the stack. */ \
|
437 |
|
|
"stm --r7, r8-r12, lr \n\t"\
|
438 |
|
|
\
|
439 |
|
|
/* Arriving here we have the following stack organizations: */ \
|
440 |
|
|
/* R8..R12, LR, PC, SR, R0..R7. */ \
|
441 |
|
|
\
|
442 |
|
|
/* Now we can finalize the save. */ \
|
443 |
|
|
\
|
444 |
|
|
/* Save ulCriticalNesting variable - R0 is overwritten */ \
|
445 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
446 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
447 |
|
|
"ld.w r0, r8[0] \n\t"\
|
448 |
|
|
"st.w --sp, r0" \
|
449 |
|
|
: \
|
450 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting) \
|
451 |
|
|
); \
|
452 |
|
|
\
|
453 |
|
|
/* Disable the its which may cause a context switch (i.e. cause a change of */ \
|
454 |
|
|
/* pxCurrentTCB). */ \
|
455 |
|
|
/* Basically, all accesses to the pxCurrentTCB structure should be put in a */ \
|
456 |
|
|
/* critical section because it is a global structure. */ \
|
457 |
|
|
portENTER_CRITICAL(); \
|
458 |
|
|
\
|
459 |
|
|
/* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
|
460 |
|
|
__asm__ __volatile__ ( \
|
461 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
462 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
463 |
|
|
"ld.w r0, r8[0] \n\t"\
|
464 |
|
|
"st.w r0[0], sp" \
|
465 |
|
|
: \
|
466 |
|
|
: [pxCurrentTCB] "i" (&pxCurrentTCB) \
|
467 |
|
|
); \
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
/*
|
471 |
|
|
* portRESTORE_CONTEXT() for SupervisorCALL exception.
|
472 |
|
|
*/
|
473 |
|
|
#define portRESTORE_CONTEXT_SCALL() \
|
474 |
|
|
{ \
|
475 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
476 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
477 |
|
|
\
|
478 |
|
|
/* Restore all registers */ \
|
479 |
|
|
\
|
480 |
|
|
/* Set SP to point to new stack */ \
|
481 |
|
|
__asm__ __volatile__ ( \
|
482 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
483 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
484 |
|
|
"ld.w r0, r8[0] \n\t"\
|
485 |
|
|
"ld.w sp, r0[0]" \
|
486 |
|
|
: \
|
487 |
|
|
: [pxCurrentTCB] "i" (&pxCurrentTCB) \
|
488 |
|
|
); \
|
489 |
|
|
\
|
490 |
|
|
/* Leave pxCurrentTCB variable access critical section */ \
|
491 |
|
|
portEXIT_CRITICAL(); \
|
492 |
|
|
\
|
493 |
|
|
__asm__ __volatile__ ( \
|
494 |
|
|
/* Restore ulCriticalNesting variable */ \
|
495 |
|
|
"ld.w r0, sp++ \n\t"\
|
496 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
497 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
498 |
|
|
"st.w r8[0], r0 \n\t"\
|
499 |
|
|
\
|
500 |
|
|
/* skip PC and SR */ \
|
501 |
|
|
/* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
|
502 |
|
|
"sub r7, sp, -10*4 \n\t"\
|
503 |
|
|
/* Restore r8-r12 and LR */ \
|
504 |
|
|
"ldm r7++, r8-r12, lr \n\t"\
|
505 |
|
|
\
|
506 |
|
|
/* RETS will take care of the extra PC and SR restore. */ \
|
507 |
|
|
/* So, we have to prepare the stack for this. */ \
|
508 |
|
|
"ld.w r0, r7[-8*4] \n\t" /* Read SR */\
|
509 |
|
|
"st.w r7[-2*4], r0 \n\t" /* Copy SR */\
|
510 |
|
|
"ld.w r0, r7[-7*4] \n\t" /* Read PC */\
|
511 |
|
|
"st.w r7[-1*4], r0 \n\t" /* Copy PC */\
|
512 |
|
|
\
|
513 |
|
|
/* Restore R0..R7 */ \
|
514 |
|
|
"ldm sp++, r0-r7 \n\t"\
|
515 |
|
|
\
|
516 |
|
|
"sub sp, -6*4 \n\t"\
|
517 |
|
|
\
|
518 |
|
|
"rets" \
|
519 |
|
|
: \
|
520 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting) \
|
521 |
|
|
); \
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
|
525 |
|
|
/*
|
526 |
|
|
* The ISR used depends on whether the cooperative or
|
527 |
|
|
* the preemptive scheduler is being used.
|
528 |
|
|
*/
|
529 |
|
|
#if configUSE_PREEMPTION == 0
|
530 |
|
|
|
531 |
|
|
/*
|
532 |
|
|
* ISR entry and exit macros. These are only required if a task switch
|
533 |
|
|
* is required from the ISR.
|
534 |
|
|
*/
|
535 |
|
|
#define portENTER_SWITCHING_ISR() \
|
536 |
|
|
{ \
|
537 |
|
|
/* Save R0..R7 */ \
|
538 |
|
|
__asm__ __volatile__ ("stm --sp, r0-r7"); \
|
539 |
|
|
\
|
540 |
|
|
/* With the cooperative scheduler, as there is no context switch by interrupt, */ \
|
541 |
|
|
/* there is also no context save. */ \
|
542 |
|
|
}
|
543 |
|
|
|
544 |
|
|
/*
|
545 |
|
|
* Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
|
546 |
|
|
*/
|
547 |
|
|
#define portEXIT_SWITCHING_ISR() \
|
548 |
|
|
{ \
|
549 |
|
|
__asm__ __volatile__ ( \
|
550 |
|
|
/* Restore R0..R7 */ \
|
551 |
|
|
"ldm sp++, r0-r7 \n\t"\
|
552 |
|
|
\
|
553 |
|
|
/* With the cooperative scheduler, as there is no context switch by interrupt, */ \
|
554 |
|
|
/* there is also no context restore. */ \
|
555 |
|
|
"rete" \
|
556 |
|
|
); \
|
557 |
|
|
}
|
558 |
|
|
|
559 |
|
|
#else
|
560 |
|
|
|
561 |
|
|
/*
|
562 |
|
|
* ISR entry and exit macros. These are only required if a task switch
|
563 |
|
|
* is required from the ISR.
|
564 |
|
|
*/
|
565 |
|
|
#define portENTER_SWITCHING_ISR() \
|
566 |
|
|
{ \
|
567 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
568 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
569 |
|
|
\
|
570 |
|
|
/* When we come here */ \
|
571 |
|
|
/* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
|
572 |
|
|
\
|
573 |
|
|
__asm__ __volatile__ ( \
|
574 |
|
|
/* Save R0..R7 */ \
|
575 |
|
|
"stm --sp, r0-r7 \n\t"\
|
576 |
|
|
\
|
577 |
|
|
/* Save ulCriticalNesting variable - R0 is overwritten */ \
|
578 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
579 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
580 |
|
|
"ld.w r0, r8[0] \n\t"\
|
581 |
|
|
"st.w --sp, r0 \n\t"\
|
582 |
|
|
\
|
583 |
|
|
/* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
|
584 |
|
|
/* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
|
585 |
|
|
/* level and allow other lower interrupt level to occur). */ \
|
586 |
|
|
/* In this case we don't want to do a task switch because we don't know what the stack */ \
|
587 |
|
|
/* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
|
588 |
|
|
/* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
|
589 |
|
|
/* will just be restoring the interrupt handler, no way!!! */ \
|
590 |
|
|
/* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
|
591 |
|
|
"ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
|
592 |
|
|
"bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
|
593 |
|
|
"cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
|
594 |
|
|
"brhi LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
|
595 |
|
|
\
|
596 |
|
|
/* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
|
597 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
598 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
599 |
|
|
"ld.w r0, r8[0] \n\t"\
|
600 |
|
|
"st.w r0[0], sp \n"\
|
601 |
|
|
\
|
602 |
|
|
"LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE]:" \
|
603 |
|
|
: \
|
604 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting), \
|
605 |
|
|
[pxCurrentTCB] "i" (&pxCurrentTCB), \
|
606 |
|
|
[LINE] "i" (__LINE__) \
|
607 |
|
|
); \
|
608 |
|
|
}
|
609 |
|
|
|
610 |
|
|
/*
|
611 |
|
|
* Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
|
612 |
|
|
*/
|
613 |
|
|
#define portEXIT_SWITCHING_ISR() \
|
614 |
|
|
{ \
|
615 |
|
|
extern volatile unsigned portLONG ulCriticalNesting; \
|
616 |
|
|
extern volatile void *volatile pxCurrentTCB; \
|
617 |
|
|
\
|
618 |
|
|
__asm__ __volatile__ ( \
|
619 |
|
|
/* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
|
620 |
|
|
/* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
|
621 |
|
|
/* level and allow other lower interrupt level to occur). */ \
|
622 |
|
|
/* In this case it's of no use to switch context and restore a new SP because we purposedly */ \
|
623 |
|
|
/* did not previously save SP in its TCB. */ \
|
624 |
|
|
"ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
|
625 |
|
|
"bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
|
626 |
|
|
"cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
|
627 |
|
|
"brhi LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE] \n\t"\
|
628 |
|
|
\
|
629 |
|
|
/* If a switch is required then we just need to call */ \
|
630 |
|
|
/* vTaskSwitchContext() as the context has already been */ \
|
631 |
|
|
/* saved. */ \
|
632 |
|
|
"cp.w r12, 1 \n\t" /* Check if Switch context is required. */\
|
633 |
|
|
"brne LABEL_ISR_RESTORE_CONTEXT_%[LINE]" \
|
634 |
|
|
: \
|
635 |
|
|
: [LINE] "i" (__LINE__) \
|
636 |
|
|
); \
|
637 |
|
|
\
|
638 |
|
|
/* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */ \
|
639 |
|
|
portENTER_CRITICAL(); \
|
640 |
|
|
vTaskSwitchContext(); \
|
641 |
|
|
portEXIT_CRITICAL(); \
|
642 |
|
|
\
|
643 |
|
|
__asm__ __volatile__ ( \
|
644 |
|
|
"LABEL_ISR_RESTORE_CONTEXT_%[LINE]: \n\t"\
|
645 |
|
|
/* Restore the context of which ever task is now the highest */ \
|
646 |
|
|
/* priority that is ready to run. */ \
|
647 |
|
|
\
|
648 |
|
|
/* Restore all registers */ \
|
649 |
|
|
\
|
650 |
|
|
/* Set SP to point to new stack */ \
|
651 |
|
|
"mov r8, LO(%[pxCurrentTCB]) \n\t"\
|
652 |
|
|
"orh r8, HI(%[pxCurrentTCB]) \n\t"\
|
653 |
|
|
"ld.w r0, r8[0] \n\t"\
|
654 |
|
|
"ld.w sp, r0[0] \n"\
|
655 |
|
|
\
|
656 |
|
|
"LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
|
657 |
|
|
\
|
658 |
|
|
/* Restore ulCriticalNesting variable */ \
|
659 |
|
|
"ld.w r0, sp++ \n\t"\
|
660 |
|
|
"mov r8, LO(%[ulCriticalNesting]) \n\t"\
|
661 |
|
|
"orh r8, HI(%[ulCriticalNesting]) \n\t"\
|
662 |
|
|
"st.w r8[0], r0 \n\t"\
|
663 |
|
|
\
|
664 |
|
|
/* Restore R0..R7 */ \
|
665 |
|
|
"ldm sp++, r0-r7 \n\t"\
|
666 |
|
|
\
|
667 |
|
|
/* Now, the stack should be R8..R12, LR, PC and SR */ \
|
668 |
|
|
"rete" \
|
669 |
|
|
: \
|
670 |
|
|
: [ulCriticalNesting] "i" (&ulCriticalNesting), \
|
671 |
|
|
[pxCurrentTCB] "i" (&pxCurrentTCB), \
|
672 |
|
|
[LINE] "i" (__LINE__) \
|
673 |
|
|
); \
|
674 |
|
|
}
|
675 |
|
|
|
676 |
|
|
#endif
|
677 |
|
|
|
678 |
|
|
|
679 |
|
|
#define portYIELD() {__asm__ __volatile__ ("scall");}
|
680 |
|
|
|
681 |
|
|
/* Task function macros as described on the FreeRTOS.org WEB site. */
|
682 |
|
|
#define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
|
683 |
|
|
#define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
|
684 |
|
|
|
685 |
|
|
#ifdef __cplusplus
|
686 |
|
|
}
|
687 |
|
|
#endif
|
688 |
|
|
|
689 |
|
|
#endif /* PORTMACRO_H */
|