00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00015 /* 00016 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd. 00017 00018 *************************************************************************** 00019 * * 00020 * If you are: * 00021 * * 00022 * + New to FreeRTOS, * 00023 * + Wanting to learn FreeRTOS or multitasking in general quickly * 00024 * + Looking for basic training, * 00025 * + Wanting to improve your FreeRTOS skills and productivity * 00026 * * 00027 * then take a look at the FreeRTOS eBook * 00028 * * 00029 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" * 00030 * http://www.FreeRTOS.org/Documentation * 00031 * * 00032 * A pdf reference manual is also available. Both are usually delivered * 00033 * to your inbox within 20 minutes to two hours when purchased between 8am * 00034 * and 8pm GMT (although please allow up to 24 hours in case of * 00035 * exceptional circumstances). Thank you for your support! * 00036 * * 00037 *************************************************************************** 00038 00039 This file is part of the FreeRTOS distribution. 00040 00041 FreeRTOS is free software; you can redistribute it and/or modify it under 00042 the terms of the GNU General Public License (version 2) as published by the 00043 Free Software Foundation AND MODIFIED BY the FreeRTOS exception. 00044 ***NOTE*** The exception to the GPL is included to allow you to distribute 00045 a combined work that includes FreeRTOS without being obliged to provide the 00046 source code for proprietary components outside of the FreeRTOS kernel. 00047 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT 00048 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00049 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00050 more details. You should have received a copy of the GNU General Public 00051 License and the FreeRTOS license exception along with FreeRTOS; if not it 00052 can be viewed here: http://www.freertos.org/a00114.html and also obtained 00053 by writing to Richard Barry, contact details for whom are available on the 00054 FreeRTOS WEB site. 00055 00056 1 tab == 4 spaces! 00057 00058 http://www.FreeRTOS.org - Documentation, latest information, license and 00059 contact details. 00060 00061 http://www.SafeRTOS.com - A version that is certified for use in safety 00062 critical systems. 00063 00064 http://www.OpenRTOS.com - Commercial support, development, porting, 00065 licensing and training services. 00066 */ 00067 00068 00069 #ifndef PORTMACRO_H 00070 #define PORTMACRO_H 00071 00072 /*----------------------------------------------------------- 00073 * Port specific definitions. 00074 * 00075 * The settings in this file configure FreeRTOS correctly for the 00076 * given hardware and compiler. 00077 * 00078 * These settings should not be altered. 00079 *----------------------------------------------------------- 00080 */ 00081 #include <avr32/io.h> 00082 #include "intc.h" 00083 #include "compiler.h" 00084 00085 #ifdef __cplusplus 00086 extern "C" { 00087 #endif 00088 00089 00090 /* Type definitions. */ 00091 #define portCHAR char 00092 #define portFLOAT float 00093 #define portDOUBLE double 00094 #define portLONG long 00095 #define portSHORT short 00096 #define portSTACK_TYPE unsigned portLONG 00097 #define portBASE_TYPE portLONG 00098 00099 #define TASK_DELAY_MS(x) ( (x) /portTICK_RATE_MS ) 00100 #define TASK_DELAY_S(x) ( (x)*1000 /portTICK_RATE_MS ) 00101 #define TASK_DELAY_MIN(x) ( (x)*60*1000/portTICK_RATE_MS ) 00102 00103 #define configTICK_TC_IRQ ATPASTE2(AVR32_TC_IRQ, configTICK_TC_CHANNEL) 00104 00105 #if( configUSE_16_BIT_TICKS == 1 ) 00106 typedef unsigned portSHORT portTickType; 00107 #define portMAX_DELAY ( portTickType ) 0xffff 00108 #else 00109 typedef unsigned portLONG portTickType; 00110 #define portMAX_DELAY ( portTickType ) 0xffffffff 00111 #endif 00112 /*-----------------------------------------------------------*/ 00113 00114 /* Architecture specifics. */ 00115 #define portSTACK_GROWTH ( -1 ) 00116 #define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ ) 00117 #define portBYTE_ALIGNMENT 4 00118 #define portNOP() {__asm__ __volatile__ ("nop");} 00119 /*-----------------------------------------------------------*/ 00120 00121 00122 /*-----------------------------------------------------------*/ 00123 00124 /* INTC-specific. */ 00125 #define DISABLE_ALL_EXCEPTIONS() Disable_global_exception() 00126 #define ENABLE_ALL_EXCEPTIONS() Enable_global_exception() 00127 00128 #define DISABLE_ALL_INTERRUPTS() Disable_global_interrupt() 00129 #define ENABLE_ALL_INTERRUPTS() Enable_global_interrupt() 00130 00131 #define DISABLE_INT_LEVEL(int_level) Disable_interrupt_level(int_level) 00132 #define ENABLE_INT_LEVEL(int_level) Enable_interrupt_level(int_level) 00133 00134 00135 /* 00136 * Debug trace. 00137 * Activated if and only if configDBG is nonzero. 00138 * Prints a formatted string to stdout. 00139 * The current source file name and line number are output with a colon before 00140 * the formatted string. 00141 * A carriage return and a linefeed are appended to the output. 00142 * stdout is redirected to the USART configured by configDBG_USART. 00143 * The parameters are the same as for the standard printf function. 00144 * There is no return value. 00145 * SHALL NOT BE CALLED FROM WITHIN AN INTERRUPT as fputs and printf use malloc, 00146 * which is interrupt-unsafe with the current __malloc_lock and __malloc_unlock. 00147 */ 00148 #if configDBG 00149 #define portDBG_TRACE(...) \ 00150 {\ 00151 fputs(__FILE__ ":" ASTRINGZ(__LINE__) ": ", stdout);\ 00152 printf(__VA_ARGS__);\ 00153 fputs("\r\n", stdout);\ 00154 } 00155 #else 00156 #define portDBG_TRACE(...) 00157 #endif 00158 00159 00160 /* Critical section management. */ 00161 #define portDISABLE_INTERRUPTS() DISABLE_ALL_INTERRUPTS() 00162 #define portENABLE_INTERRUPTS() ENABLE_ALL_INTERRUPTS() 00163 00164 00165 extern void vPortEnterCritical( void ); 00166 extern void vPortExitCritical( void ); 00167 00168 #define portENTER_CRITICAL() vPortEnterCritical(); 00169 #define portEXIT_CRITICAL() vPortExitCritical(); 00170 00171 00172 /* Added as there is no such function in FreeRTOS. */ 00173 extern void *pvPortRealloc( void *pv, size_t xSize ); 00174 /*-----------------------------------------------------------*/ 00175 00176 00177 /*=============================================================================================*/ 00178 00179 /* 00180 * Restore Context for cases other than INTi. 00181 */ 00182 #define portRESTORE_CONTEXT() \ 00183 { \ 00184 extern volatile unsigned portLONG ulCriticalNesting; \ 00185 extern volatile void *volatile pxCurrentTCB; \ 00186 \ 00187 __asm__ __volatile__ ( \ 00188 /* Set SP to point to new stack */ \ 00189 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00190 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00191 "ld.w r0, r8[0]\n\t" \ 00192 "ld.w sp, r0[0]\n\t" \ 00193 \ 00194 /* Restore ulCriticalNesting variable */ \ 00195 "ld.w r0, sp++\n\t" \ 00196 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00197 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00198 "st.w r8[0], r0\n\t" \ 00199 \ 00200 /* Restore R0..R7 */ \ 00201 "ldm sp++, r0-r7\n\t" \ 00202 /* R0-R7 should not be used below this line */ \ 00203 /* Skip PC and SR (will do it at the end) */ \ 00204 "sub sp, -2*4\n\t" \ 00205 /* Restore R8..R12 and LR */ \ 00206 "ldm sp++, r8-r12, lr\n\t" \ 00207 /* Restore SR */ \ 00208 "ld.w r0, sp[-8*4]\n\t" /* R0 is modified, is restored later. */ \ 00209 "mtsr %[SR], r0\n\t" \ 00210 /* Restore r0 */ \ 00211 "ld.w r0, sp[-9*4]\n\t" \ 00212 /* Restore PC */ \ 00213 "ld.w pc, sp[-7*4]" /* Get PC from stack - PC is the 7th register saved */ \ 00214 : \ 00215 : [ulCriticalNesting] "i" (&ulCriticalNesting), \ 00216 [pxCurrentTCB] "i" (&pxCurrentTCB), \ 00217 [SR] "i" (AVR32_SR) \ 00218 ); \ 00219 } 00220 00221 00222 /* 00223 * portSAVE_CONTEXT_INT() and portRESTORE_CONTEXT_INT(): for AVR32_INTC_INT0..3 exceptions. 00224 * portSAVE_CONTEXT_SCALL() and portRESTORE_CONTEXT_SCALL(): for the scall exception. 00225 * 00226 * Had to make different versions because registers saved on the system stack 00227 * are not the same between AVR32_INTC_INT0..3 exceptions and the scall exception. 00228 */ 00229 00230 // Task context stack layout: 00231 // R8 (*) 00232 // R9 (*) 00233 // R10 (*) 00234 // R11 (*) 00235 // R12 (*) 00236 // R14/LR (*) 00237 // R15/PC (*) 00238 // SR (*) 00239 // R0 00240 // R1 00241 // R2 00242 // R3 00243 // R4 00244 // R5 00245 // R6 00246 // R7 00247 // ulCriticalNesting 00248 // (*) automatically done for AVR32_INTC_INT0..AVR32_INTC_INT3, but not for SCALL 00249 00250 /* 00251 * The ISR used for the scheduler tick depends on whether the cooperative or 00252 * the preemptive scheduler is being used. 00253 */ 00254 #if configUSE_PREEMPTION == 0 00255 00256 /* 00257 * portSAVE_CONTEXT_OS_INT() for OS Tick exception. 00258 */ 00259 #define portSAVE_CONTEXT_OS_INT() \ 00260 { \ 00261 /* Save R0..R7 */ \ 00262 __asm__ __volatile__ ("stm --sp, r0-r7"); \ 00263 \ 00264 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \ 00265 /* there is also no context save. */ \ 00266 } 00267 00268 /* 00269 * portRESTORE_CONTEXT_OS_INT() for Tick exception. 00270 */ 00271 #define portRESTORE_CONTEXT_OS_INT() \ 00272 { \ 00273 __asm__ __volatile__ ( \ 00274 /* Restore R0..R7 */ \ 00275 "ldm sp++, r0-r7\n\t" \ 00276 \ 00277 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \ 00278 /* there is also no context restore. */ \ 00279 "rete" \ 00280 ); \ 00281 } 00282 00283 #else 00284 00285 /* 00286 * portSAVE_CONTEXT_OS_INT() for OS Tick exception. 00287 */ 00288 #define portSAVE_CONTEXT_OS_INT() \ 00289 { \ 00290 extern volatile unsigned portLONG ulCriticalNesting; \ 00291 extern volatile void *volatile pxCurrentTCB; \ 00292 \ 00293 /* When we come here */ \ 00294 /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \ 00295 \ 00296 __asm__ __volatile__ ( \ 00297 /* Save R0..R7 */ \ 00298 "stm --sp, r0-r7\n\t" \ 00299 \ 00300 /* Save ulCriticalNesting variable - R0 is overwritten */ \ 00301 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00302 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00303 "ld.w r0, r8[0]\n\t" \ 00304 "st.w --sp, r0\n\t" \ 00305 \ 00306 /* Check if AVR32_INTC_INT0 or higher were being handled (case where the OS tick interrupted another */ \ 00307 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \ 00308 /* level and allow other lower interrupt level to occur). */ \ 00309 /* In this case we don't want to do a task switch because we don't know what the stack */ \ 00310 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \ 00311 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \ 00312 /* will just be restoring the interrupt handler, no way!!! */ \ 00313 /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \ 00314 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \ 00315 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \ 00316 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \ 00317 "brhi LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE]\n\t" \ 00318 \ 00319 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \ 00320 /* NOTE: we don't enter a critical section here because all interrupt handlers */ \ 00321 /* MUST perform a SAVE_CONTEXT/RESTORE_CONTEXT in the same way as */ \ 00322 /* portSAVE_CONTEXT_OS_INT/port_RESTORE_CONTEXT_OS_INT if they call OS functions. */ \ 00323 /* => all interrupt handlers must use portENTER_SWITCHING_ISR/portEXIT_SWITCHING_ISR. */ \ 00324 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00325 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00326 "ld.w r0, r8[0]\n\t" \ 00327 "st.w r0[0], sp\n" \ 00328 \ 00329 "LABEL_INT_SKIP_SAVE_CONTEXT_%[LINE]:" \ 00330 : \ 00331 : [ulCriticalNesting] "i" (&ulCriticalNesting), \ 00332 [pxCurrentTCB] "i" (&pxCurrentTCB), \ 00333 [LINE] "i" (__LINE__) \ 00334 ); \ 00335 } 00336 00337 /* 00338 * portRESTORE_CONTEXT_OS_INT() for Tick exception. 00339 */ 00340 #define portRESTORE_CONTEXT_OS_INT() \ 00341 { \ 00342 extern volatile unsigned portLONG ulCriticalNesting; \ 00343 extern volatile void *volatile pxCurrentTCB; \ 00344 \ 00345 /* Check if AVR32_INTC_INT0 or higher were being handled (case where the OS tick interrupted another */ \ 00346 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \ 00347 /* level and allow other lower interrupt level to occur). */ \ 00348 /* In this case we don't want to do a task switch because we don't know what the stack */ \ 00349 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \ 00350 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \ 00351 /* will just be restoring the interrupt handler, no way!!! */ \ 00352 __asm__ __volatile__ ( \ 00353 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */ \ 00354 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */ \ 00355 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */ \ 00356 "brhi LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]" \ 00357 : \ 00358 : [LINE] "i" (__LINE__) \ 00359 ); \ 00360 \ 00361 /* Else */ \ 00362 /* because it is here safe, always call vTaskSwitchContext() since an OS tick occurred. */ \ 00363 /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */ \ 00364 portENTER_CRITICAL(); \ 00365 vTaskSwitchContext(); \ 00366 portEXIT_CRITICAL(); \ 00367 \ 00368 /* Restore all registers */ \ 00369 \ 00370 __asm__ __volatile__ ( \ 00371 /* Set SP to point to new stack */ \ 00372 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00373 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00374 "ld.w r0, r8[0]\n\t" \ 00375 "ld.w sp, r0[0]\n" \ 00376 \ 00377 "LABEL_INT_SKIP_RESTORE_CONTEXT_%[LINE]:\n\t" \ 00378 \ 00379 /* Restore ulCriticalNesting variable */ \ 00380 "ld.w r0, sp++\n\t" \ 00381 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00382 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00383 "st.w r8[0], r0\n\t" \ 00384 \ 00385 /* Restore R0..R7 */ \ 00386 "ldm sp++, r0-r7\n\t" \ 00387 \ 00388 /* Now, the stack should be R8..R12, LR, PC and SR */ \ 00389 "rete" \ 00390 : \ 00391 : [ulCriticalNesting] "i" (&ulCriticalNesting), \ 00392 [pxCurrentTCB] "i" (&pxCurrentTCB), \ 00393 [LINE] "i" (__LINE__) \ 00394 ); \ 00395 } 00396 00397 #endif 00398 00399 00400 /* 00401 * portSAVE_CONTEXT_SCALL() for SupervisorCALL exception. 00402 * 00403 * NOTE: taskYIELD()(== SCALL) MUST NOT be called in a mode > supervisor mode. 00404 * 00405 */ 00406 #define portSAVE_CONTEXT_SCALL() \ 00407 { \ 00408 extern volatile unsigned portLONG ulCriticalNesting; \ 00409 extern volatile void *volatile pxCurrentTCB; \ 00410 \ 00411 /* Warning: the stack layout after SCALL doesn't match the one after an interrupt. */ \ 00412 /* If SR[M2:M0] == 001 */ \ 00413 /* PC and SR are on the stack. */ \ 00414 /* Else (other modes) */ \ 00415 /* Nothing on the stack. */ \ 00416 \ 00417 /* WARNING NOTE: the else case cannot happen as it is strictly forbidden to call */ \ 00418 /* vTaskDelay() and vTaskDelayUntil() OS functions (that result in a taskYield()) */ \ 00419 /* in an interrupt|exception handler. */ \ 00420 \ 00421 __asm__ __volatile__ ( \ 00422 /* in order to save R0-R7 */ \ 00423 "sub sp, 6*4\n\t" \ 00424 /* Save R0..R7 */ \ 00425 "stm --sp, r0-r7\n\t" \ 00426 \ 00427 /* in order to save R8-R12 and LR */ \ 00428 /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \ 00429 "sub r7, sp,-16*4\n\t" \ 00430 /* Copy PC and SR in other places in the stack. */ \ 00431 "ld.w r0, r7[-2*4]\n\t" /* Read SR */\ 00432 "st.w r7[-8*4], r0\n\t" /* Copy SR */\ 00433 "ld.w r0, r7[-1*4]\n\t" /* Read PC */\ 00434 "st.w r7[-7*4], r0\n\t" /* Copy PC */\ 00435 \ 00436 /* Save R8..R12 and LR on the stack. */ \ 00437 "stm --r7, r8-r12, lr\n\t" \ 00438 \ 00439 /* Arriving here we have the following stack organizations: */ \ 00440 /* R8..R12, LR, PC, SR, R0..R7. */ \ 00441 \ 00442 /* Now we can finalize the save. */ \ 00443 \ 00444 /* Save ulCriticalNesting variable - R0 is overwritten */ \ 00445 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00446 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00447 "ld.w r0, r8[0]\n\t" \ 00448 "st.w --sp, r0" \ 00449 : \ 00450 : [ulCriticalNesting] "i" (&ulCriticalNesting) \ 00451 ); \ 00452 \ 00453 /* Disable the its which may cause a context switch (i.e. cause a change of */ \ 00454 /* pxCurrentTCB). */ \ 00455 /* Basically, all accesses to the pxCurrentTCB structure should be put in a */ \ 00456 /* critical section because it is a global structure. */ \ 00457 portENTER_CRITICAL(); \ 00458 \ 00459 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \ 00460 __asm__ __volatile__ ( \ 00461 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00462 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00463 "ld.w r0, r8[0]\n\t" \ 00464 "st.w r0[0], sp" \ 00465 : \ 00466 : [pxCurrentTCB] "i" (&pxCurrentTCB) \ 00467 ); \ 00468 } 00469 00470 /* 00471 * portRESTORE_CONTEXT() for SupervisorCALL exception. 00472 */ 00473 #define portRESTORE_CONTEXT_SCALL() \ 00474 { \ 00475 extern volatile unsigned portLONG ulCriticalNesting; \ 00476 extern volatile void *volatile pxCurrentTCB; \ 00477 \ 00478 /* Restore all registers */ \ 00479 \ 00480 /* Set SP to point to new stack */ \ 00481 __asm__ __volatile__ ( \ 00482 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00483 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00484 "ld.w r0, r8[0]\n\t" \ 00485 "ld.w sp, r0[0]" \ 00486 : \ 00487 : [pxCurrentTCB] "i" (&pxCurrentTCB) \ 00488 ); \ 00489 \ 00490 /* Leave pxCurrentTCB variable access critical section */ \ 00491 portEXIT_CRITICAL(); \ 00492 \ 00493 __asm__ __volatile__ ( \ 00494 /* Restore ulCriticalNesting variable */ \ 00495 "ld.w r0, sp++\n\t" \ 00496 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00497 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00498 "st.w r8[0], r0\n\t" \ 00499 \ 00500 /* skip PC and SR */ \ 00501 /* do not use SP if interrupts occurs, SP must be left at bottom of stack */ \ 00502 "sub r7, sp, -10*4\n\t" \ 00503 /* Restore r8-r12 and LR */ \ 00504 "ldm r7++, r8-r12, lr\n\t" \ 00505 \ 00506 /* RETS will take care of the extra PC and SR restore. */ \ 00507 /* So, we have to prepare the stack for this. */ \ 00508 "ld.w r0, r7[-8*4]\n\t" /* Read SR */\ 00509 "st.w r7[-2*4], r0\n\t" /* Copy SR */\ 00510 "ld.w r0, r7[-7*4]\n\t" /* Read PC */\ 00511 "st.w r7[-1*4], r0\n\t" /* Copy PC */\ 00512 \ 00513 /* Restore R0..R7 */ \ 00514 "ldm sp++, r0-r7\n\t" \ 00515 \ 00516 "sub sp, -6*4\n\t" \ 00517 \ 00518 "rets" \ 00519 : \ 00520 : [ulCriticalNesting] "i" (&ulCriticalNesting) \ 00521 ); \ 00522 } 00523 00524 00525 /* 00526 * The ISR used depends on whether the cooperative or 00527 * the preemptive scheduler is being used. 00528 */ 00529 #if configUSE_PREEMPTION == 0 00530 00531 /* 00532 * ISR entry and exit macros. These are only required if a task switch 00533 * is required from the ISR. 00534 */ 00535 #define portENTER_SWITCHING_ISR() \ 00536 { \ 00537 /* Save R0..R7 */ \ 00538 __asm__ __volatile__ ("stm --sp, r0-r7"); \ 00539 \ 00540 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \ 00541 /* there is also no context save. */ \ 00542 } 00543 00544 /* 00545 * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1 00546 */ 00547 #define portEXIT_SWITCHING_ISR() \ 00548 { \ 00549 __asm__ __volatile__ ( \ 00550 /* Restore R0..R7 */ \ 00551 "ldm sp++, r0-r7\n\t" \ 00552 \ 00553 /* With the cooperative scheduler, as there is no context switch by interrupt, */ \ 00554 /* there is also no context restore. */ \ 00555 "rete" \ 00556 ); \ 00557 } 00558 00559 #else 00560 00561 /* 00562 * ISR entry and exit macros. These are only required if a task switch 00563 * is required from the ISR. 00564 */ 00565 #define portENTER_SWITCHING_ISR() \ 00566 { \ 00567 extern volatile unsigned portLONG ulCriticalNesting; \ 00568 extern volatile void *volatile pxCurrentTCB; \ 00569 \ 00570 /* When we come here */ \ 00571 /* Registers R8..R12, LR, PC and SR had already been pushed to system stack */ \ 00572 \ 00573 __asm__ __volatile__ ( \ 00574 /* Save R0..R7 */ \ 00575 "stm --sp, r0-r7\n\t" \ 00576 \ 00577 /* Save ulCriticalNesting variable - R0 is overwritten */ \ 00578 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00579 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00580 "ld.w r0, r8[0]\n\t" \ 00581 "st.w --sp, r0\n\t" \ 00582 \ 00583 /* Check if AVR32_INTC_INT0 or higher were being handled (case where the OS tick interrupted another */ \ 00584 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \ 00585 /* level and allow other lower interrupt level to occur). */ \ 00586 /* In this case we don't want to do a task switch because we don't know what the stack */ \ 00587 /* currently looks like (we don't know what the interrupted interrupt handler was doing). */ \ 00588 /* Saving SP in pxCurrentTCB and then later restoring it (thinking restoring the task) */ \ 00589 /* will just be restoring the interrupt handler, no way!!! */ \ 00590 /* So, since we won't do a vTaskSwitchContext(), it's of no use to save SP. */ \ 00591 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */\ 00592 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */\ 00593 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */\ 00594 "brhi LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE]\n\t" \ 00595 \ 00596 /* Store SP in the first member of the structure pointed to by pxCurrentTCB */ \ 00597 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00598 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00599 "ld.w r0, r8[0]\n\t" \ 00600 "st.w r0[0], sp\n" \ 00601 \ 00602 "LABEL_ISR_SKIP_SAVE_CONTEXT_%[LINE]:" \ 00603 : \ 00604 : [ulCriticalNesting] "i" (&ulCriticalNesting), \ 00605 [pxCurrentTCB] "i" (&pxCurrentTCB), \ 00606 [LINE] "i" (__LINE__) \ 00607 ); \ 00608 } 00609 00610 /* 00611 * Input parameter: in R12, boolean. Perform a vTaskSwitchContext() if 1 00612 */ 00613 #define portEXIT_SWITCHING_ISR() \ 00614 { \ 00615 extern volatile unsigned portLONG ulCriticalNesting; \ 00616 extern volatile void *volatile pxCurrentTCB; \ 00617 \ 00618 __asm__ __volatile__ ( \ 00619 /* Check if AVR32_INTC_INT0 or higher were being handled (case where the OS tick interrupted another */ \ 00620 /* interrupt handler (which was of a higher priority level but decided to lower its priority */ \ 00621 /* level and allow other lower interrupt level to occur). */ \ 00622 /* In this case it's of no use to switch context and restore a new SP because we purposedly */ \ 00623 /* did not previously save SP in its TCB. */ \ 00624 "ld.w r0, sp[9*4]\n\t" /* Read SR in stack */\ 00625 "bfextu r0, r0, 22, 3\n\t" /* Extract the mode bits to R0. */\ 00626 "cp.w r0, 1\n\t" /* Compare the mode bits with supervisor mode(b'001) */\ 00627 "brhi LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE]\n\t" \ 00628 \ 00629 /* If a switch is required then we just need to call */ \ 00630 /* vTaskSwitchContext() as the context has already been */ \ 00631 /* saved. */ \ 00632 "cp.w r12, 1\n\t" /* Check if Switch context is required. */\ 00633 "brne LABEL_ISR_RESTORE_CONTEXT_%[LINE]" \ 00634 : \ 00635 : [LINE] "i" (__LINE__) \ 00636 ); \ 00637 \ 00638 /* A critical section has to be used here because vTaskSwitchContext handles FreeRTOS linked lists. */ \ 00639 portENTER_CRITICAL(); \ 00640 vTaskSwitchContext(); \ 00641 portEXIT_CRITICAL(); \ 00642 \ 00643 __asm__ __volatile__ ( \ 00644 "LABEL_ISR_RESTORE_CONTEXT_%[LINE]:\n\t" \ 00645 /* Restore the context of which ever task is now the highest */ \ 00646 /* priority that is ready to run. */ \ 00647 \ 00648 /* Restore all registers */ \ 00649 \ 00650 /* Set SP to point to new stack */ \ 00651 "mov r8, LO(%[pxCurrentTCB])\n\t" \ 00652 "orh r8, HI(%[pxCurrentTCB])\n\t" \ 00653 "ld.w r0, r8[0]\n\t" \ 00654 "ld.w sp, r0[0]\n" \ 00655 \ 00656 "LABEL_ISR_SKIP_RESTORE_CONTEXT_%[LINE]:\n\t" \ 00657 \ 00658 /* Restore ulCriticalNesting variable */ \ 00659 "ld.w r0, sp++\n\t" \ 00660 "mov r8, LO(%[ulCriticalNesting])\n\t" \ 00661 "orh r8, HI(%[ulCriticalNesting])\n\t" \ 00662 "st.w r8[0], r0\n\t" \ 00663 \ 00664 /* Restore R0..R7 */ \ 00665 "ldm sp++, r0-r7\n\t" \ 00666 \ 00667 /* Now, the stack should be R8..R12, LR, PC and SR */ \ 00668 "rete" \ 00669 : \ 00670 : [ulCriticalNesting] "i" (&ulCriticalNesting), \ 00671 [pxCurrentTCB] "i" (&pxCurrentTCB), \ 00672 [LINE] "i" (__LINE__) \ 00673 ); \ 00674 } 00675 00676 #endif 00677 00678 00679 #define portYIELD() {__asm__ __volatile__ ("scall");} 00680 00681 /* Task function macros as described on the FreeRTOS.org WEB site. */ 00682 #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters ) 00683 #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters ) 00684 00685 #ifdef __cplusplus 00686 } 00687 #endif 00688 00689 #endif /* PORTMACRO_H */