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