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