]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/AVR32_UC3/portmacro.h
Update version number ready for the V8.2.3 release.
[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 V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
17     All rights reserved\r
18 \r
19     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
26 \r
27     ***************************************************************************\r
28     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
29     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
30     >>!   obliged to provide the source code for proprietary components     !<<\r
31     >>!   outside of the FreeRTOS kernel.                                   !<<\r
32     ***************************************************************************\r
33 \r
34     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
35     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
36     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
37     link: http://www.freertos.org/a00114.html\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    FreeRTOS provides completely free yet professionally developed,    *\r
42      *    robust, strictly quality controlled, supported, and cross          *\r
43      *    platform software that is more than just the market leader, it     *\r
44      *    is the industry's de facto standard.                               *\r
45      *                                                                       *\r
46      *    Help yourself get started quickly while simultaneously helping     *\r
47      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
48      *    tutorial book, reference manual, or both:                          *\r
49      *    http://www.FreeRTOS.org/Documentation                              *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
54     the FAQ page "My application does not run, what could be wrong?".  Have you\r
55     defined configASSERT()?\r
56 \r
57     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
58     embedded software for free we request you assist our global community by\r
59     participating in the support forum.\r
60 \r
61     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
62     be as productive as possible as early as possible.  Now you can receive\r
63     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
64     Ltd, and the world's leading authority on the world's leading RTOS.\r
65 \r
66     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
67     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
68     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
69 \r
70     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
71     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
72 \r
73     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
74     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
75     licenses offer ticketed support, indemnification and commercial middleware.\r
76 \r
77     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
78     engineered and independently SIL3 certified version for use in safety and\r
79     mission critical applications that require provable dependability.\r
80 \r
81     1 tab == 4 spaces!\r
82 */\r
83 \r
84 \r
85 \r
86 #ifndef PORTMACRO_H\r
87 #define PORTMACRO_H\r
88 \r
89 /*-----------------------------------------------------------\r
90  * Port specific definitions.\r
91  *\r
92  * The settings in this file configure FreeRTOS correctly for the\r
93  * given hardware and compiler.\r
94  *\r
95  * These settings should not be altered.\r
96  *-----------------------------------------------------------\r
97  */\r
98 #include <avr32/io.h>\r
99 #include "intc.h"\r
100 #include "compiler.h"\r
101 \r
102 #ifdef __cplusplus\r
103 extern "C" {\r
104 #endif\r
105 \r
106 \r
107 /* Type definitions. */\r
108 #define portCHAR        char\r
109 #define portFLOAT       float\r
110 #define portDOUBLE      double\r
111 #define portLONG        long\r
112 #define portSHORT       short\r
113 #define portSTACK_TYPE  uint32_t\r
114 #define portBASE_TYPE   long\r
115 \r
116 typedef portSTACK_TYPE StackType_t;\r
117 typedef long BaseType_t;\r
118 typedef unsigned long UBaseType_t;\r
119 \r
120 \r
121 #define TASK_DELAY_MS(x)   ( (x)        /portTICK_PERIOD_MS )\r
122 #define TASK_DELAY_S(x)    ( (x)*1000   /portTICK_PERIOD_MS )\r
123 #define TASK_DELAY_MIN(x)  ( (x)*60*1000/portTICK_PERIOD_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 uint16_t TickType_t;\r
129         #define portMAX_DELAY ( TickType_t ) 0xffff\r
130 #else\r
131   typedef uint32_t TickType_t;\r
132         #define portMAX_DELAY ( TickType_t ) 0xffffffffUL\r
133 #endif\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 /* Architecture specifics. */\r
137 #define portSTACK_GROWTH      ( -1 )\r
138 #define portTICK_PERIOD_MS      ( ( TickType_t ) 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 uint32_t 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 uint32_t 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 uint32_t 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 uint32_t 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 uint32_t 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 uint32_t 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 uint32_t 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