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