]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_LPC23xx/portISR.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / portable / GCC / ARM7_LPC23xx / portISR.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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 /* Scheduler includes. */\r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 \r
44 /* Constants required to handle interrupts. */\r
45 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
46 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
47 \r
48 /* Constants required to handle critical sections. */\r
49 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
50 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
51 \r
52 /*-----------------------------------------------------------*/\r
53 \r
54 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
55 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
56 \r
57 /* \r
58  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
59  * function here.\r
60  */\r
61 void vPortISRStartFirstTask( void );\r
62 /*-----------------------------------------------------------*/\r
63 \r
64 void vPortISRStartFirstTask( void )\r
65 {\r
66         /* Simply start the scheduler.  This is included here as it can only be\r
67         called from ARM mode. */\r
68         portRESTORE_CONTEXT();\r
69 }\r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /*\r
73  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
74  *\r
75  * When a context switch is performed from the task level the saved task \r
76  * context is made to look as if it occurred from within the tick ISR.  This\r
77  * way the same restore context function can be used when restoring the context\r
78  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
79  */\r
80 void vPortYieldProcessor( void )\r
81 {\r
82         /* Within an IRQ ISR the link register has an offset from the true return \r
83         address, but an SWI ISR does not.  Add the offset manually so the same \r
84         ISR return code can be used in both cases. */\r
85         asm volatile ( "ADD             LR, LR, #4" );\r
86 \r
87         /* Perform the context switch.  First save the context of the current task. */\r
88         portSAVE_CONTEXT();\r
89 \r
90         /* Find the highest priority task that is ready to run. */\r
91         vTaskSwitchContext();\r
92 \r
93         /* Restore the context of the new task. */\r
94         portRESTORE_CONTEXT();  \r
95 }\r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* \r
99  * The ISR used for the scheduler tick depends on whether the cooperative or\r
100  * the preemptive scheduler is being used.\r
101  */\r
102 \r
103 \r
104 #if configUSE_PREEMPTION == 0\r
105 \r
106         /* The cooperative scheduler requires a normal IRQ service routine to \r
107         simply increment the system tick. */\r
108         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
109         void vNonPreemptiveTick( void )\r
110         {       \r
111 \r
112                 FIO2SET |= (1<<0);\r
113 \r
114                 vTaskIncrementTick();\r
115                 T0IR = 2;\r
116                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
117         }\r
118 \r
119 #else\r
120 \r
121         /* The preemptive scheduler is defined as "naked" as the full context is\r
122         saved on entry as part of the context switch. */\r
123         void vPreemptiveTick( void ) __attribute__((naked));\r
124         void vPreemptiveTick( void )\r
125         {\r
126                 /* Save the context of the interrupted task. */\r
127                 portSAVE_CONTEXT();     \r
128 \r
129                 /* Increment the RTOS tick count, then look for the highest priority \r
130                 task that is ready to run. */\r
131                 vTaskIncrementTick();\r
132                 vTaskSwitchContext();\r
133 \r
134                 /* Ready for the next interrupt. */\r
135                 T0IR = 2;\r
136                 VICAddress = portCLEAR_VIC_INTERRUPT;\r
137                 \r
138                 /* Restore the context of the new task. */\r
139                 portRESTORE_CONTEXT();\r
140         }\r
141 \r
142 #endif\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /*\r
146  * The interrupt management utilities can only be called from ARM mode.  When\r
147  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
148  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
149  * the utilities are defined as macros in portmacro.h - as per other ports.\r
150  */\r
151 #ifdef THUMB_INTERWORK\r
152 \r
153         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
154         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
155 \r
156         void vPortDisableInterruptsFromThumb( void )\r
157         {\r
158                 asm volatile ( \r
159                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
160                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
161                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
162                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
163                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
164                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
165         }\r
166                         \r
167         void vPortEnableInterruptsFromThumb( void )\r
168         {\r
169                 asm volatile ( \r
170                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
171                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
172                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
173                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
174                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
175                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
176         }\r
177 \r
178 #endif /* THUMB_INTERWORK */\r
179 \r
180 /* The code generated by the GCC compiler uses the stack in different ways at\r
181 different optimisation levels.  The interrupt flags can therefore not always\r
182 be saved to the stack.  Instead the critical section nesting level is stored\r
183 in a variable, which is then saved as part of the stack context. */\r
184 void vPortEnterCritical( void )\r
185 {\r
186         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
187         asm volatile ( \r
188                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
189                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
190                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
191                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
192                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
193 \r
194         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
195         directly.  Increment ulCriticalNesting to keep a count of how many times\r
196         portENTER_CRITICAL() has been called. */\r
197         ulCriticalNesting++;\r
198 }\r
199 \r
200 void vPortExitCritical( void )\r
201 {\r
202         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
203         {\r
204                 /* Decrement the nesting count as we are leaving a critical section. */\r
205                 ulCriticalNesting--;\r
206 \r
207                 /* If the nesting level has reached zero then interrupts should be\r
208                 re-enabled. */\r
209                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
210                 {\r
211                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
212                         asm volatile ( \r
213                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
214                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
215                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
216                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
217                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
218                 }\r
219         }\r
220 }\r