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