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