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