1 /*This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
4 * \brief FreeRTOS port source for AVR32 UC3.
\r
6 * - Compiler: GNU GCC for AVR32
\r
7 * - Supported devices: All AVR32 devices can be used.
\r
10 * \author Atmel Corporation: http://www.atmel.com \n
\r
11 * Support and FAQ: http://support.atmel.no/
\r
13 *****************************************************************************/
\r
16 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
18 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
19 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
21 ***************************************************************************
\r
23 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
24 * Complete, revised, and edited pdf reference manuals are also *
\r
27 * Purchasing FreeRTOS documentation will not only help you, by *
\r
28 * ensuring you get running as quickly as possible and with an *
\r
29 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
30 * the FreeRTOS project to continue with its mission of providing *
\r
31 * professional grade, cross platform, de facto standard solutions *
\r
32 * for microcontrollers - completely free of charge! *
\r
34 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
36 * Thank you for using FreeRTOS, and thank you for your support! *
\r
38 ***************************************************************************
\r
41 This file is part of the FreeRTOS distribution.
\r
43 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
44 the terms of the GNU General Public License (version 2) as published by the
\r
45 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
46 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
47 distribute a combined work that includes FreeRTOS without being obliged to
\r
48 provide the source code for proprietary components outside of the FreeRTOS
\r
49 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
50 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
51 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
52 more details. You should have received a copy of the GNU General Public
\r
53 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
54 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
55 by writing to Richard Barry, contact details for whom are available on the
\r
60 ***************************************************************************
\r
62 * Having a problem? Start by reading the FAQ "My application does *
\r
63 * not run, what could be wrong?" *
\r
65 * http://www.FreeRTOS.org/FAQHelp.html *
\r
67 ***************************************************************************
\r
70 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
71 and contact details.
\r
73 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
74 including FreeRTOS+Trace - an indispensable productivity tool.
\r
76 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
77 the code with commercial support, indemnification, and middleware, under
\r
78 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
79 provide a safety engineered and independently SIL3 certified version under
\r
80 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
87 /*-----------------------------------------------------------
\r
88 * Port specific definitions.
\r
90 * The settings in this file configure FreeRTOS correctly for the
\r
91 * given hardware and compiler.
\r
93 * These settings should not be altered.
\r
94 *-----------------------------------------------------------
\r
96 #include <avr32/io.h>
\r
98 #include "compiler.h"
\r
105 /* Type definitions. */
\r
106 #define portCHAR char
\r
107 #define portFLOAT float
\r
108 #define portDOUBLE double
\r
109 #define portLONG long
\r
110 #define portSHORT short
\r
111 #define portSTACK_TYPE unsigned portLONG
\r
112 #define portBASE_TYPE portLONG
\r
114 #define TASK_DELAY_MS(x) ( (x) /portTICK_RATE_MS )
\r
115 #define TASK_DELAY_S(x) ( (x)*1000 /portTICK_RATE_MS )
\r
116 #define TASK_DELAY_MIN(x) ( (x)*60*1000/portTICK_RATE_MS )
\r
118 #define configTICK_TC_IRQ ATPASTE2(AVR32_TC_IRQ, configTICK_TC_CHANNEL)
\r
120 #if( configUSE_16_BIT_TICKS == 1 )
\r
121 typedef unsigned portSHORT portTickType;
\r
122 #define portMAX_DELAY ( portTickType ) 0xffff
\r
124 typedef unsigned portLONG portTickType;
\r
125 #define portMAX_DELAY ( portTickType ) 0xffffffff
\r
127 /*-----------------------------------------------------------*/
\r
129 /* Architecture specifics. */
\r
130 #define portSTACK_GROWTH ( -1 )
\r
131 #define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ )
\r
132 #define portBYTE_ALIGNMENT 4
\r
133 #define portNOP() {__asm__ __volatile__ ("nop");}
\r
134 /*-----------------------------------------------------------*/
\r
137 /*-----------------------------------------------------------*/
\r
139 /* INTC-specific. */
\r
140 #define DISABLE_ALL_EXCEPTIONS() Disable_global_exception()
\r
141 #define ENABLE_ALL_EXCEPTIONS() Enable_global_exception()
\r
143 #define DISABLE_ALL_INTERRUPTS() Disable_global_interrupt()
\r
144 #define ENABLE_ALL_INTERRUPTS() Enable_global_interrupt()
\r
146 #define DISABLE_INT_LEVEL(int_lev) Disable_interrupt_level(int_lev)
\r
147 #define ENABLE_INT_LEVEL(int_lev) Enable_interrupt_level(int_lev)
\r
152 * Activated if and only if configDBG is nonzero.
\r
153 * Prints a formatted string to stdout.
\r
154 * The current source file name and line number are output with a colon before
\r
155 * the formatted string.
\r
156 * A carriage return and a linefeed are appended to the output.
\r
157 * stdout is redirected to the USART configured by configDBG_USART.
\r
158 * The parameters are the same as for the standard printf function.
\r
159 * There is no return value.
\r
160 * SHALL NOT BE CALLED FROM WITHIN AN INTERRUPT as fputs and printf use malloc,
\r
161 * which is interrupt-unsafe with the current __malloc_lock and __malloc_unlock.
\r
164 #define portDBG_TRACE(...) \
\r
166 fputs(__FILE__ ":" ASTRINGZ(__LINE__) ": ", stdout);\
\r
167 printf(__VA_ARGS__);\
\r
168 fputs("\r\n", stdout);\
\r
171 #define portDBG_TRACE(...)
\r
175 /* Critical section management. */
\r
176 #define portDISABLE_INTERRUPTS() DISABLE_ALL_INTERRUPTS()
\r
177 #define portENABLE_INTERRUPTS() ENABLE_ALL_INTERRUPTS()
\r
180 extern void vPortEnterCritical( void );
\r
181 extern void vPortExitCritical( void );
\r
183 #define portENTER_CRITICAL() vPortEnterCritical();
\r
184 #define portEXIT_CRITICAL() vPortExitCritical();
\r
187 /* Added as there is no such function in FreeRTOS. */
\r
188 extern void *pvPortRealloc( void *pv, size_t xSize );
\r
189 /*-----------------------------------------------------------*/
\r
192 /*=============================================================================================*/
\r
195 * Restore Context for cases other than INTi.
\r
197 #define portRESTORE_CONTEXT() \
\r
199 extern volatile unsigned portLONG ulCriticalNesting; \
\r
200 extern volatile void *volatile pxCurrentTCB; \
\r
202 __asm__ __volatile__ ( \
\r
203 /* Set SP to point to new stack */ \
\r
204 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
205 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
206 "ld.w r0, r8[0] \n\t"\
\r
207 "ld.w sp, r0[0] \n\t"\
\r
209 /* Restore ulCriticalNesting variable */ \
\r
210 "ld.w r0, sp++ \n\t"\
\r
211 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
212 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
213 "st.w r8[0], r0 \n\t"\
\r
215 /* Restore R0..R7 */ \
\r
216 "ldm sp++, r0-r7 \n\t"\
\r
217 /* R0-R7 should not be used below this line */ \
\r
218 /* Skip PC and SR (will do it at the end) */ \
\r
219 "sub sp, -2*4 \n\t"\
\r
220 /* Restore R8..R12 and LR */ \
\r
221 "ldm sp++, r8-r12, lr \n\t"\
\r
223 "ld.w r0, sp[-8*4]\n\t" /* R0 is modified, is restored later. */ \
\r
224 "mtsr %[SR], r0 \n\t"\
\r
226 "ld.w r0, sp[-9*4] \n\t"\
\r
228 "ld.w pc, sp[-7*4]" /* Get PC from stack - PC is the 7th register saved */ \
\r
230 : [ulCriticalNesting] "i" (&ulCriticalNesting), \
\r
231 [pxCurrentTCB] "i" (&pxCurrentTCB), \
\r
232 [SR] "i" (AVR32_SR) \
\r
238 * portSAVE_CONTEXT_INT() and portRESTORE_CONTEXT_INT(): for INT0..3 exceptions.
\r
239 * portSAVE_CONTEXT_SCALL() and portRESTORE_CONTEXT_SCALL(): for the scall exception.
\r
241 * Had to make different versions because registers saved on the system stack
\r
242 * are not the same between INT0..3 exceptions and the scall exception.
\r
245 // Task context stack layout:
\r
262 // ulCriticalNesting
\r
263 // (*) automatically done for INT0..INT3, but not for SCALL
\r
266 * The ISR used for the scheduler tick depends on whether the cooperative or
\r
267 * the preemptive scheduler is being used.
\r
269 #if configUSE_PREEMPTION == 0
\r
272 * portSAVE_CONTEXT_OS_INT() for OS Tick exception.
\r
274 #define portSAVE_CONTEXT_OS_INT() \
\r
276 /* Save R0..R7 */ \
\r
277 __asm__ __volatile__ ("stm --sp, r0-r7"); \
\r
279 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
\r
280 /* there is also no context save. */ \
\r
284 * portRESTORE_CONTEXT_OS_INT() for Tick exception.
\r
286 #define portRESTORE_CONTEXT_OS_INT() \
\r
288 __asm__ __volatile__ ( \
\r
289 /* Restore R0..R7 */ \
\r
290 "ldm sp++, r0-r7\n\t" \
\r
292 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
\r
293 /* there is also no context restore. */ \
\r
301 * portSAVE_CONTEXT_OS_INT() for OS Tick exception.
\r
303 #define portSAVE_CONTEXT_OS_INT() \
\r
305 extern volatile unsigned portLONG ulCriticalNesting; \
\r
306 extern volatile void *volatile pxCurrentTCB; \
\r
308 /* When we come here */ \
\r
309 /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
\r
311 __asm__ __volatile__ ( \
\r
312 /* Save R0..R7 */ \
\r
313 "stm --sp, r0-r7 \n\t"\
\r
315 /* Save ulCriticalNesting variable - R0 is overwritten */ \
\r
316 "mov r8, LO(%[ulCriticalNesting])\n\t" \
\r
317 "orh r8, HI(%[ulCriticalNesting])\n\t" \
\r
318 "ld.w r0, r8[0] \n\t"\
\r
319 "st.w --sp, r0 \n\t"\
\r
321 /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
\r
322 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
\r
323 /* level and allow other lower interrupt level to occur). */ \
\r
324 /* In this case we don't want to do a task switch because we don't know what the stack */ \
\r
325 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
\r
326 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
\r
327 /* will just be restoring the interrupt handler, no way!!! */ \
\r
328 /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
\r
329 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
\r
330 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
\r
331 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
\r
332 "brhi LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
\r
334 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
\r
335 /* NOTE: we don't enter a critical section here because all interrupt handlers */ \
\r
336 /* MUST perform a SAVE_CONTEXT/RESTORE_CONTEXT in the same way as */ \
\r
337 /* portSAVE_CONTEXT_OS_INT/port_RESTORE_CONTEXT_OS_INT if they call OS functions. */ \
\r
338 /* => all interrupt handlers must use portENTER_SWITCHING_ISR/portEXIT_SWITCHING_ISR. */ \
\r
339 "mov r8, LO(%[pxCurrentTCB])\n\t" \
\r
340 "orh r8, HI(%[pxCurrentTCB])\n\t" \
\r
341 "ld.w r0, r8[0]\n\t" \
\r
342 "st.w r0[0], sp\n" \
\r
344 "LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE]:" \
\r
346 : [ulCriticalNesting] "i" (&ulCriticalNesting), \
\r
347 [pxCurrentTCB] "i" (&pxCurrentTCB), \
\r
348 [LINE] "i" (__LINE__) \
\r
353 * portRESTORE_CONTEXT_OS_INT() for Tick exception.
\r
355 #define portRESTORE_CONTEXT_OS_INT() \
\r
357 extern volatile unsigned portLONG ulCriticalNesting; \
\r
358 extern volatile void *volatile pxCurrentTCB; \
\r
360 /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
\r
361 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
\r
362 /* level and allow other lower interrupt level to occur). */ \
\r
363 /* In this case we don't want to do a task switch because we don't know what the stack */ \
\r
364 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
\r
365 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
\r
366 /* will just be restoring the interrupt handler, no way!!! */ \
\r
367 __asm__ __volatile__ ( \
\r
368 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \
\r
369 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \
\r
370 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \
\r
371 "brhi LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]" \
\r
373 : [LINE] "i" (__LINE__) \
\r
377 /* because it is here safe, always call vTaskSwitchContext() since an OS tick occurred. */ \
\r
378 /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */\
\r
379 portENTER_CRITICAL(); \
\r
380 vTaskSwitchContext(); \
\r
381 portEXIT_CRITICAL(); \
\r
383 /* Restore all registers */ \
\r
385 __asm__ __volatile__ ( \
\r
386 /* Set SP to point to new stack */ \
\r
387 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
388 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
389 "ld.w r0, r8[0] \n\t"\
\r
390 "ld.w sp, r0[0] \n"\
\r
392 "LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
\r
394 /* Restore ulCriticalNesting variable */ \
\r
395 "ld.w r0, sp++ \n\t" \
\r
396 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
397 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
398 "st.w r8[0], r0 \n\t"\
\r
400 /* Restore R0..R7 */ \
\r
401 "ldm sp++, r0-r7 \n\t"\
\r
403 /* Now, the stack should be R8..R12, LR, PC and SR */ \
\r
406 : [ulCriticalNesting] "i" (&ulCriticalNesting), \
\r
407 [pxCurrentTCB] "i" (&pxCurrentTCB), \
\r
408 [LINE] "i" (__LINE__) \
\r
416 * portSAVE_CONTEXT_SCALL() for SupervisorCALL exception.
\r
418 * NOTE: taskYIELD()(== SCALL) MUST NOT be called in a mode > supervisor mode.
\r
421 #define portSAVE_CONTEXT_SCALL() \
\r
423 extern volatile unsigned portLONG ulCriticalNesting; \
\r
424 extern volatile void *volatile pxCurrentTCB; \
\r
426 /* Warning: the stack layout after SCALL doesn't match the one after an interrupt. */ \
\r
427 /* If SR[M2:M0] == 001 */ \
\r
428 /* PC and SR are on the stack. */ \
\r
429 /* Else (other modes) */ \
\r
430 /* Nothing on the stack. */ \
\r
432 /* WARNING NOTE: the else case cannot happen as it is strictly forbidden to call */ \
\r
433 /* vTaskDelay() and vTaskDelayUntil() OS functions (that result in a taskYield()) */ \
\r
434 /* in an interrupt|exception handler. */ \
\r
436 __asm__ __volatile__ ( \
\r
437 /* in order to save R0-R7 */ \
\r
438 "sub sp, 6*4 \n\t"\
\r
439 /* Save R0..R7 */ \
\r
440 "stm --sp, r0-r7 \n\t"\
\r
442 /* in order to save R8-R12 and LR */ \
\r
443 /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
\r
444 "sub r7, sp,-16*4 \n\t"\
\r
445 /* Copy PC and SR in other places in the stack. */ \
\r
446 "ld.w r0, r7[-2*4] \n\t" /* Read SR */\
\r
447 "st.w r7[-8*4], r0 \n\t" /* Copy SR */\
\r
448 "ld.w r0, r7[-1*4] \n\t" /* Read PC */\
\r
449 "st.w r7[-7*4], r0 \n\t" /* Copy PC */\
\r
451 /* Save R8..R12 and LR on the stack. */ \
\r
452 "stm --r7, r8-r12, lr \n\t"\
\r
454 /* Arriving here we have the following stack organizations: */ \
\r
455 /* R8..R12, LR, PC, SR, R0..R7. */ \
\r
457 /* Now we can finalize the save. */ \
\r
459 /* Save ulCriticalNesting variable - R0 is overwritten */ \
\r
460 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
461 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
462 "ld.w r0, r8[0] \n\t"\
\r
465 : [ulCriticalNesting] "i" (&ulCriticalNesting) \
\r
468 /* Disable the its which may cause a context switch (i.e. cause a change of */ \
\r
469 /* pxCurrentTCB). */ \
\r
470 /* Basically, all accesses to the pxCurrentTCB structure should be put in a */ \
\r
471 /* critical section because it is a global structure. */ \
\r
472 portENTER_CRITICAL(); \
\r
474 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
\r
475 __asm__ __volatile__ ( \
\r
476 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
477 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
478 "ld.w r0, r8[0] \n\t"\
\r
481 : [pxCurrentTCB] "i" (&pxCurrentTCB) \
\r
486 * portRESTORE_CONTEXT() for SupervisorCALL exception.
\r
488 #define portRESTORE_CONTEXT_SCALL() \
\r
490 extern volatile unsigned portLONG ulCriticalNesting; \
\r
491 extern volatile void *volatile pxCurrentTCB; \
\r
493 /* Restore all registers */ \
\r
495 /* Set SP to point to new stack */ \
\r
496 __asm__ __volatile__ ( \
\r
497 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
498 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
499 "ld.w r0, r8[0] \n\t"\
\r
502 : [pxCurrentTCB] "i" (&pxCurrentTCB) \
\r
505 /* Leave pxCurrentTCB variable access critical section */ \
\r
506 portEXIT_CRITICAL(); \
\r
508 __asm__ __volatile__ ( \
\r
509 /* Restore ulCriticalNesting variable */ \
\r
510 "ld.w r0, sp++ \n\t"\
\r
511 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
512 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
513 "st.w r8[0], r0 \n\t"\
\r
515 /* skip PC and SR */ \
\r
516 /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \
\r
517 "sub r7, sp, -10*4 \n\t"\
\r
518 /* Restore r8-r12 and LR */ \
\r
519 "ldm r7++, r8-r12, lr \n\t"\
\r
521 /* RETS will take care of the extra PC and SR restore. */ \
\r
522 /* So, we have to prepare the stack for this. */ \
\r
523 "ld.w r0, r7[-8*4] \n\t" /* Read SR */\
\r
524 "st.w r7[-2*4], r0 \n\t" /* Copy SR */\
\r
525 "ld.w r0, r7[-7*4] \n\t" /* Read PC */\
\r
526 "st.w r7[-1*4], r0 \n\t" /* Copy PC */\
\r
528 /* Restore R0..R7 */ \
\r
529 "ldm sp++, r0-r7 \n\t"\
\r
531 "sub sp, -6*4 \n\t"\
\r
535 : [ulCriticalNesting] "i" (&ulCriticalNesting) \
\r
541 * The ISR used depends on whether the cooperative or
\r
542 * the preemptive scheduler is being used.
\r
544 #if configUSE_PREEMPTION == 0
\r
547 * ISR entry and exit macros. These are only required if a task switch
\r
548 * is required from the ISR.
\r
550 #define portENTER_SWITCHING_ISR() \
\r
552 /* Save R0..R7 */ \
\r
553 __asm__ __volatile__ ("stm --sp, r0-r7"); \
\r
555 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
\r
556 /* there is also no context save. */ \
\r
560 * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
\r
562 #define portEXIT_SWITCHING_ISR() \
\r
564 __asm__ __volatile__ ( \
\r
565 /* Restore R0..R7 */ \
\r
566 "ldm sp++, r0-r7 \n\t"\
\r
568 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \
\r
569 /* there is also no context restore. */ \
\r
577 * ISR entry and exit macros. These are only required if a task switch
\r
578 * is required from the ISR.
\r
580 #define portENTER_SWITCHING_ISR() \
\r
582 extern volatile unsigned portLONG ulCriticalNesting; \
\r
583 extern volatile void *volatile pxCurrentTCB; \
\r
585 /* When we come here */ \
\r
586 /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \
\r
588 __asm__ __volatile__ ( \
\r
589 /* Save R0..R7 */ \
\r
590 "stm --sp, r0-r7 \n\t"\
\r
592 /* Save ulCriticalNesting variable - R0 is overwritten */ \
\r
593 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
594 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
595 "ld.w r0, r8[0] \n\t"\
\r
596 "st.w --sp, r0 \n\t"\
\r
598 /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
\r
599 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
\r
600 /* level and allow other lower interrupt level to occur). */ \
\r
601 /* In this case we don't want to do a task switch because we don't know what the stack */ \
\r
602 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \
\r
603 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \
\r
604 /* will just be restoring the interrupt handler, no way!!! */ \
\r
605 /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \
\r
606 "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
\r
607 "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
\r
608 "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
\r
609 "brhi LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE] \n\t"\
\r
611 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \
\r
612 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
613 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
614 "ld.w r0, r8[0] \n\t"\
\r
615 "st.w r0[0], sp \n"\
\r
617 "LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE]:" \
\r
619 : [ulCriticalNesting] "i" (&ulCriticalNesting), \
\r
620 [pxCurrentTCB] "i" (&pxCurrentTCB), \
\r
621 [LINE] "i" (__LINE__) \
\r
626 * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1
\r
628 #define portEXIT_SWITCHING_ISR() \
\r
630 extern volatile unsigned portLONG ulCriticalNesting; \
\r
631 extern volatile void *volatile pxCurrentTCB; \
\r
633 __asm__ __volatile__ ( \
\r
634 /* Check if INT0 or higher were being handled (case where the OS tick interrupted another */ \
\r
635 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \
\r
636 /* level and allow other lower interrupt level to occur). */ \
\r
637 /* In this case it's of no use to switch context and restore a new SP because we purposedly */ \
\r
638 /* did not previously save SP in its TCB. */ \
\r
639 "ld.w r0, sp[9*4] \n\t" /* Read SR in stack */\
\r
640 "bfextu r0, r0, 22, 3 \n\t" /* Extract the mode bits to R0. */\
\r
641 "cp.w r0, 1 \n\t" /* Compare the mode bits with supervisor mode(b'001) */\
\r
642 "brhi LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE] \n\t"\
\r
644 /* If a switch is required then we just need to call */ \
\r
645 /* vTaskSwitchContext() as the context has already been */ \
\r
647 "cp.w r12, 1 \n\t" /* Check if Switch context is required. */\
\r
648 "brne LABEL_ISR_RESTORE_CONTEXT_%[LINE]" \
\r
650 : [LINE] "i" (__LINE__) \
\r
653 /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */ \
\r
654 portENTER_CRITICAL(); \
\r
655 vTaskSwitchContext(); \
\r
656 portEXIT_CRITICAL(); \
\r
658 __asm__ __volatile__ ( \
\r
659 "LABEL_ISR_RESTORE_CONTEXT_%[LINE]: \n\t"\
\r
660 /* Restore the context of which ever task is now the highest */ \
\r
661 /* priority that is ready to run. */ \
\r
663 /* Restore all registers */ \
\r
665 /* Set SP to point to new stack */ \
\r
666 "mov r8, LO(%[pxCurrentTCB]) \n\t"\
\r
667 "orh r8, HI(%[pxCurrentTCB]) \n\t"\
\r
668 "ld.w r0, r8[0] \n\t"\
\r
669 "ld.w sp, r0[0] \n"\
\r
671 "LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE]: \n\t"\
\r
673 /* Restore ulCriticalNesting variable */ \
\r
674 "ld.w r0, sp++ \n\t"\
\r
675 "mov r8, LO(%[ulCriticalNesting]) \n\t"\
\r
676 "orh r8, HI(%[ulCriticalNesting]) \n\t"\
\r
677 "st.w r8[0], r0 \n\t"\
\r
679 /* Restore R0..R7 */ \
\r
680 "ldm sp++, r0-r7 \n\t"\
\r
682 /* Now, the stack should be R8..R12, LR, PC and SR */ \
\r
685 : [ulCriticalNesting] "i" (&ulCriticalNesting), \
\r
686 [pxCurrentTCB] "i" (&pxCurrentTCB), \
\r
687 [LINE] "i" (__LINE__) \
\r
694 #define portYIELD() {__asm__ __volatile__ ("scall");}
\r
696 /* Task function macros as described on the FreeRTOS.org WEB site. */
\r
697 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
\r
698 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
\r
704 #endif /* PORTMACRO_H */
\r