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