]> git.sur5r.net Git - freertos/blob - Source/portable/Keil/ARM7/portISR.c
Update to V5.0.2
[freertos] / Source / portable / Keil / ARM7 / 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 /* This file must always be compiled to ARM mode as it contains ISR \r
58 definitions. */\r
59 #pragma ARM\r
60 \r
61 /* Scheduler includes. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 \r
65 /* Constants required to handle interrupts. */\r
66 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
67 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* The code generated by the Keil compiler does not maintain separate\r
72 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
73 use the stack as per other ports.  Instead a variable is used to keep\r
74 track of the critical section nesting.  This variable has to be stored\r
75 as part of the task context and must be initialised to a non zero value. */\r
76 \r
77 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
78 volatile unsigned portLONG 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 );\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 \r
93 void vPortISRStartFirstTask( void )\r
94 {\r
95         /* Simply start the scheduler.  This is included here as it can only be\r
96         called from ARM mode. */\r
97         portRESTORE_CONTEXT();\r
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * Interrupt service routine for the SWI interrupt.  The vector table is\r
103  * configured within startup.s.\r
104  *\r
105  * vPortYieldProcessor() is used to manually force a context switch.  The\r
106  * SWI interrupt is generated by a call to taskYIELD() or portYIELD().\r
107  */\r
108 void vPortYieldProcessor( void ) __task\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{ ADD      LR, LR, #4 };\r
114 \r
115         /* Perform the context switch. */\r
116         portSAVE_CONTEXT();\r
117         vTaskSwitchContext();\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         /* \r
130          * The cooperative scheduler requires a normal IRQ service routine to \r
131          * simply increment the system tick. \r
132          */\r
133         void vNonPreemptiveTick( void );\r
134         void vNonPreemptiveTick( void ) __irq\r
135         {\r
136                 /* Increment the tick count - this may make a delaying task ready\r
137                 to run - but a context switch is not performed. */              \r
138                 vTaskIncrementTick();\r
139 \r
140                 /* Ready for the next interrupt. */\r
141                 T0IR = portTIMER_MATCH_ISR_BIT;\r
142                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
143         }\r
144 \r
145 #else\r
146 \r
147         /* \r
148          * The preemptive scheduler ISR is defined as "naked" as the full context \r
149          * is saved on entry as part of the context switch. \r
150          */\r
151         void vPreemptiveTick( void );\r
152         void vPreemptiveTick( void ) __task\r
153         {\r
154                 /* Save the context of the current task. */\r
155                 portSAVE_CONTEXT();     \r
156 \r
157                 /* Increment the tick count - this may make a delayed task ready to \r
158                 run. */\r
159                 vTaskIncrementTick();\r
160 \r
161                 /* Find the highest priority task that is ready to run. */\r
162                 vTaskSwitchContext();\r
163 \r
164                 /* Ready for the next interrupt. */\r
165                 T0IR = portTIMER_MATCH_ISR_BIT;\r
166                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
167                 \r
168                 /* Restore the context of the highest priority task that is ready to \r
169                 run. */\r
170                 portRESTORE_CONTEXT();\r
171         }\r
172 #endif\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * The interrupt management utilities can only be called from ARM mode.  When\r
177  * KEIL_THUMB_INTERWORK is defined the utilities are defined as functions here \r
178  * to ensure a switch to ARM mode.  When KEIL_THUMB_INTERWORK is not defined \r
179  * then the utilities are defined as macros in portmacro.h - as per other \r
180  * ports.\r
181  */\r
182 #ifdef KEIL_THUMB_INTERWORK\r
183 \r
184         void vPortDisableInterruptsFromThumb( void ) __task;\r
185         void vPortEnableInterruptsFromThumb( void ) __task;\r
186 \r
187         void vPortDisableInterruptsFromThumb( void ) __task\r
188         {\r
189                 __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
190                 __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
191                 __asm{ ORR              R0, R0, #0xC0   };      /* Disable IRQ, FIQ.                                            */\r
192                 __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
193                 __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                                      */\r
194                 __asm{ BX               R14                             };      /* Return back to thumb.                                        */\r
195         }\r
196                         \r
197         void vPortEnableInterruptsFromThumb( void )     __task\r
198         {\r
199                 __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
200                 __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
201                 __asm{ BIC              R0, R0, #0xC0   };      /* Enable IRQ, FIQ.                                                     */\r
202                 __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
203                 __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                                      */\r
204                 __asm{ BX               R14                             };      /* Return back to thumb.                                        */\r
205         }\r
206 \r
207 #endif /* KEIL_THUMB_INTERWORK */\r
208 \r
209 \r
210 \r
211 /* The code generated by the Keil compiler does not maintain separate\r
212 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
213 use the stack as per other ports.  Instead a variable is used to keep\r
214 track of the critical section nesting.  This necessitates the use of a \r
215 function in place of the macro. */\r
216 \r
217 void vPortEnterCritical( void )\r
218 {\r
219         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
220         __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
221         __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
222         __asm{ ORR              R0, R0, #0xC0   };      /* Disable IRQ, FIQ.                                            */\r
223         __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
224         __asm{ 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{ STMDB    SP!, {R0}               };      /* Push R0.                                                     */\r
245                         __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                            */\r
246                         __asm{ BIC              R0, R0, #0xC0   };      /* Enable IRQ, FIQ.                                     */\r
247                         __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.           */\r
248                         __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                      */\r
249                 }\r
250         }\r
251 }\r
252 \r
253 \r
254 \r
255 \r
256 \r
257 \r
258 \r
259 \r
260 \r
261 \r