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