]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_AT91FR40008/portISR.c
Update to V5.0.2
[freertos] / Source / portable / GCC / ARM7_AT91FR40008 / portISR.c
1 /*\r
2         FreeRTOS.org V5.0.2 - Copyright (C) 2003-2008 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     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 \r
51 /*-----------------------------------------------------------\r
52  * Components that can be compiled to either ARM or THUMB mode are\r
53  * contained in port.c  The ISR routines, which can only be compiled\r
54  * to ARM mode, are contained in this file.\r
55  *----------------------------------------------------------*/\r
56 \r
57 /*\r
58         Changes from V3.2.4\r
59 \r
60         + The assembler statements are now included in a single asm block rather\r
61           than each line having its own asm block.\r
62 */\r
63 \r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Constants required to handle interrupts. */\r
70 #define portCLEAR_AIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
71 \r
72 /* Constants required to handle critical sections. */\r
73 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
74 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
79 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
80 \r
81 /* \r
82  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
83  * function here.\r
84  */\r
85 void vPortISRStartFirstTask( void );\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 void vPortISRStartFirstTask( void )\r
89 {\r
90         /* Simply start the scheduler.  This is included here as it can only be\r
91         called from ARM mode. */\r
92         portRESTORE_CONTEXT();\r
93 }\r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /*\r
97  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
98  *\r
99  * When a context switch is performed from the task level the saved task \r
100  * context is made to look as if it occurred from within the tick ISR.  This\r
101  * way the same restore context function can be used when restoring the context\r
102  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
103  */\r
104 void vPortYieldProcessor( void )\r
105 {\r
106         /* Within an IRQ ISR the link register has an offset from the true return \r
107         address, but an SWI ISR does not.  Add the offset manually so the same \r
108         ISR return code can be used in both cases. */\r
109         asm volatile ( "ADD             LR, LR, #4" );\r
110 \r
111         /* Perform the context switch.  First save the context of the current task. */\r
112         portSAVE_CONTEXT();\r
113 \r
114         /* Find the highest priority task that is ready to run. */\r
115         vTaskSwitchContext();\r
116 \r
117         /* Restore the context of the new task. */\r
118         portRESTORE_CONTEXT();  \r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /* \r
123  * The ISR used for the scheduler tick depends on whether the cooperative or\r
124  * the preemptive scheduler is being used.\r
125  */\r
126 \r
127 #if configUSE_PREEMPTION == 0\r
128 \r
129         /* The cooperative scheduler requires a normal IRQ service routine to \r
130         simply increment the system tick. */\r
131         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
132         void vNonPreemptiveTick( void )\r
133         {               \r
134         static volatile unsigned portLONG ulDummy;\r
135 \r
136                 /* Clear tick timer interrupt indication. */\r
137                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
138 \r
139                 vTaskIncrementTick();\r
140 \r
141                 /* Acknowledge the interrupt at AIC level... */\r
142                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
143         }\r
144 \r
145 #else  /* else preemption is turned on */\r
146 \r
147         /* The preemptive scheduler is defined as "naked" as the full context is\r
148         saved on entry as part of the context switch. */\r
149         void vPreemptiveTick( void ) __attribute__((naked));\r
150         void vPreemptiveTick( void )\r
151         {\r
152                 /* Save the context of the interrupted task. */\r
153                 portSAVE_CONTEXT();     \r
154 \r
155                 /* WARNING - Do not use local (stack) variables here.  Use globals\r
156                                          if you must! */\r
157                 static volatile unsigned portLONG ulDummy;\r
158 \r
159                 /* Clear tick timer interrupt indication. */\r
160                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
161 \r
162                 /* Increment the RTOS tick count, then look for the highest priority \r
163                 task that is ready to run. */\r
164                 vTaskIncrementTick();\r
165                 vTaskSwitchContext();\r
166 \r
167                 /* Acknowledge the interrupt at AIC level... */\r
168                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
169 \r
170                 /* Restore the context of the new task. */\r
171                 portRESTORE_CONTEXT();\r
172         }\r
173 \r
174 #endif\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*\r
178  * The interrupt management utilities can only be called from ARM mode.  When\r
179  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
180  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
181  * the utilities are defined as macros in portmacro.h - as per other ports.\r
182  */\r
183 #ifdef THUMB_INTERWORK\r
184 \r
185         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
186         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
187 \r
188         void vPortDisableInterruptsFromThumb( void )\r
189         {\r
190                 asm volatile ( \r
191                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
192                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
193                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
194                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
195                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
196                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
197         }\r
198                         \r
199         void vPortEnableInterruptsFromThumb( void )\r
200         {\r
201                 asm volatile ( \r
202                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
203                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
204                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
205                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
206                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
207                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
208         }\r
209 \r
210 #endif /* THUMB_INTERWORK */\r
211 \r
212 /* The code generated by the GCC compiler uses the stack in different ways at\r
213 different optimisation levels.  The interrupt flags can therefore not always\r
214 be saved to the stack.  Instead the critical section nesting level is stored\r
215 in a variable, which is then saved as part of the stack context. */\r
216 void vPortEnterCritical( void )\r
217 {\r
218         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
219         asm volatile ( \r
220                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
221                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
222                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
223                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
224                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
225 \r
226         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
227         directly.  Increment ulCriticalNesting to keep a count of how many times\r
228         portENTER_CRITICAL() has been called. */\r
229         ulCriticalNesting++;\r
230 }\r
231 \r
232 void vPortExitCritical( void )\r
233 {\r
234         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
235         {\r
236                 /* Decrement the nesting count as we are leaving a critical section. */\r
237                 ulCriticalNesting--;\r
238 \r
239                 /* If the nesting level has reached zero then interrupts should be\r
240                 re-enabled. */\r
241                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
242                 {\r
243                         /* Enable interrupts as per portEXIT_CRITICAL().                                */\r
244                         asm volatile ( \r
245                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
246                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
247                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
248                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
249                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
250                 }\r
251         }\r
252 }\r
253 \r