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