]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_AT91FR40008/portISR.c
Update to V4.0.4. Add in STR912 port and demo.
[freertos] / Source / portable / GCC / ARM7_AT91FR40008 / portISR.c
1 /*\r
2         FreeRTOS.org V4.0.4 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 \r
34 /*-----------------------------------------------------------\r
35  * Components that can be compiled to either ARM or THUMB mode are\r
36  * contained in port.c  The ISR routines, which can only be compiled\r
37  * to ARM mode, are contained in this file.\r
38  *----------------------------------------------------------*/\r
39 \r
40 /*\r
41         Changes from V3.2.4\r
42 \r
43         + The assembler statements are now included in a single asm block rather\r
44           than each line having its own asm block.\r
45 */\r
46 \r
47 \r
48 /* Scheduler includes. */\r
49 #include "FreeRTOS.h"\r
50 #include "task.h"\r
51 \r
52 /* Constants required to handle interrupts. */\r
53 #define portCLEAR_AIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
54 \r
55 /* Constants required to handle critical sections. */\r
56 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
57 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
58 \r
59 /*-----------------------------------------------------------*/\r
60 \r
61 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
62 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
63 \r
64 /* \r
65  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
66  * function here.\r
67  */\r
68 void vPortISRStartFirstTask( void );\r
69 /*-----------------------------------------------------------*/\r
70 \r
71 void vPortISRStartFirstTask( void )\r
72 {\r
73         /* Simply start the scheduler.  This is included here as it can only be\r
74         called from ARM mode. */\r
75         portRESTORE_CONTEXT();\r
76 }\r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
81  *\r
82  * When a context switch is performed from the task level the saved task \r
83  * context is made to look as if it occurred from within the tick ISR.  This\r
84  * way the same restore context function can be used when restoring the context\r
85  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
86  */\r
87 void vPortYieldProcessor( void )\r
88 {\r
89         /* Within an IRQ ISR the link register has an offset from the true return \r
90         address, but an SWI ISR does not.  Add the offset manually so the same \r
91         ISR return code can be used in both cases. */\r
92         asm volatile ( "ADD             LR, LR, #4" );\r
93 \r
94         /* Perform the context switch.  First save the context of the current task. */\r
95         portSAVE_CONTEXT();\r
96 \r
97         /* Find the highest priority task that is ready to run. */\r
98         vTaskSwitchContext();\r
99 \r
100         /* Restore the context of the new task. */\r
101         portRESTORE_CONTEXT();  \r
102 }\r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /* \r
106  * The ISR used for the scheduler tick depends on whether the cooperative or\r
107  * the preemptive scheduler is being used.\r
108  */\r
109 \r
110 #if configUSE_PREEMPTION == 0\r
111 \r
112         /* The cooperative scheduler requires a normal IRQ service routine to \r
113         simply increment the system tick. */\r
114         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
115         void vNonPreemptiveTick( void )\r
116         {               \r
117         static volatile unsigned portLONG ulDummy;\r
118 \r
119                 /* Clear tick timer interrupt indication. */\r
120                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
121 \r
122                 vTaskIncrementTick();\r
123 \r
124                 /* Acknowledge the interrupt at AIC level... */\r
125                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
126         }\r
127 \r
128 #else  /* else preemption is turned on */\r
129 \r
130         /* The preemptive scheduler is defined as "naked" as the full context is\r
131         saved on entry as part of the context switch. */\r
132         void vPreemptiveTick( void ) __attribute__((naked));\r
133         void vPreemptiveTick( void )\r
134         {\r
135                 /* Save the context of the interrupted task. */\r
136                 portSAVE_CONTEXT();     \r
137 \r
138                 /* WARNING - Do not use local (stack) variables here.  Use globals\r
139                                          if you must! */\r
140                 static volatile unsigned portLONG ulDummy;\r
141 \r
142                 /* Clear tick timer interrupt indication. */\r
143                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
144 \r
145                 /* Increment the RTOS tick count, then look for the highest priority \r
146                 task that is ready to run. */\r
147                 vTaskIncrementTick();\r
148                 vTaskSwitchContext();\r
149 \r
150                 /* Acknowledge the interrupt at AIC level... */\r
151                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
152 \r
153                 /* Restore the context of the new task. */\r
154                 portRESTORE_CONTEXT();\r
155         }\r
156 \r
157 #endif\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /*\r
161  * The interrupt management utilities can only be called from ARM mode.  When\r
162  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
163  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
164  * the utilities are defined as macros in portmacro.h - as per other ports.\r
165  */\r
166 #ifdef THUMB_INTERWORK\r
167 \r
168         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
169         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
170 \r
171         void vPortDisableInterruptsFromThumb( void )\r
172         {\r
173                 asm volatile ( \r
174                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
175                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
176                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
177                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
178                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
179                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
180         }\r
181                         \r
182         void vPortEnableInterruptsFromThumb( void )\r
183         {\r
184                 asm volatile ( \r
185                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
186                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
187                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
188                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
189                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
190                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
191         }\r
192 \r
193 #endif /* THUMB_INTERWORK */\r
194 \r
195 /* The code generated by the GCC compiler uses the stack in different ways at\r
196 different optimisation levels.  The interrupt flags can therefore not always\r
197 be saved to the stack.  Instead the critical section nesting level is stored\r
198 in a variable, which is then saved as part of the stack context. */\r
199 void vPortEnterCritical( void )\r
200 {\r
201         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
202         asm volatile ( \r
203                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
204                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
205                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
206                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
207                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
208 \r
209         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
210         directly.  Increment ulCriticalNesting to keep a count of how many times\r
211         portENTER_CRITICAL() has been called. */\r
212         ulCriticalNesting++;\r
213 }\r
214 \r
215 void vPortExitCritical( void )\r
216 {\r
217         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
218         {\r
219                 /* Decrement the nesting count as we are leaving a critical section. */\r
220                 ulCriticalNesting--;\r
221 \r
222                 /* If the nesting level has reached zero then interrupts should be\r
223                 re-enabled. */\r
224                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
225                 {\r
226                         /* Enable interrupts as per portEXIT_CRITICAL().                                */\r
227                         asm volatile ( \r
228                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
229                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
230                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
231                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
232                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
233                 }\r
234         }\r
235 }\r
236 \r