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