]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_LPC2000/portISR.c
Update to V5.0.0.
[freertos] / Source / portable / GCC / ARM7_LPC2000 / portISR.c
1 /*\r
2         FreeRTOS.org V5.0.0 - 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 V2.5.2\r
59                 \r
60         + The critical section management functions have been changed.  These no\r
61           longer modify the stack and are safe to use at all optimisation levels.\r
62           The functions are now also the same for both ARM and THUMB modes.\r
63 \r
64         Changes from V2.6.0\r
65 \r
66         + Removed the 'static' from the definition of vNonPreemptiveTick() to \r
67           allow the demo to link when using the cooperative scheduler.\r
68 \r
69         Changes from V3.2.4\r
70 \r
71         + The assembler statements are now included in a single asm block rather\r
72           than each line having its own asm block.\r
73 */\r
74 \r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 \r
80 /* Constants required to handle interrupts. */\r
81 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
82 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
83 \r
84 /* Constants required to handle critical sections. */\r
85 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
86 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
91 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
92 \r
93 /* \r
94  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
95  * function here.\r
96  */\r
97 void vPortISRStartFirstTask( void );\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 void vPortISRStartFirstTask( void )\r
101 {\r
102         /* Simply start the scheduler.  This is included here as it can only be\r
103         called from ARM mode. */\r
104         portRESTORE_CONTEXT();\r
105 }\r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
110  *\r
111  * When a context switch is performed from the task level the saved task \r
112  * context is made to look as if it occurred from within the tick ISR.  This\r
113  * way the same restore context function can be used when restoring the context\r
114  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
115  */\r
116 void vPortYieldProcessor( void )\r
117 {\r
118         /* Within an IRQ ISR the link register has an offset from the true return \r
119         address, but an SWI ISR does not.  Add the offset manually so the same \r
120         ISR return code can be used in both cases. */\r
121         asm volatile ( "ADD             LR, LR, #4" );\r
122 \r
123         /* Perform the context switch.  First save the context of the current task. */\r
124         portSAVE_CONTEXT();\r
125 \r
126         /* Find the highest priority task that is ready to run. */\r
127         vTaskSwitchContext();\r
128 \r
129         /* Restore the context of the new task. */\r
130         portRESTORE_CONTEXT();  \r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 /* \r
135  * The ISR used for the scheduler tick.\r
136  */\r
137 void vTickISR( void ) __attribute__((naked));\r
138 void vTickISR( void )\r
139 {\r
140         /* Save the context of the interrupted task. */\r
141         portSAVE_CONTEXT();     \r
142 \r
143         /* Increment the RTOS tick count, then look for the highest priority \r
144         task that is ready to run. */\r
145         vTaskIncrementTick();\r
146 \r
147         #if configUSE_PREEMPTION == 1\r
148                 vTaskSwitchContext();\r
149         #endif\r
150 \r
151         /* Ready for the next interrupt. */\r
152         T0_IR = portTIMER_MATCH_ISR_BIT;\r
153         VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
154         \r
155         /* Restore the context of the new task. */\r
156         portRESTORE_CONTEXT();\r
157 }\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