]> git.sur5r.net Git - freertos/blob - Source/portable/Keil/ARM7/portISR.c
357240ceccae0d9e677ca2642fd7b65162e2aff3
[freertos] / Source / portable / Keil / ARM7 / portISR.c
1 /*\r
2         FreeRTOS.org V4.2.1 - 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         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 \r
37 /*-----------------------------------------------------------\r
38  * Components that can be compiled to either ARM or THUMB mode are\r
39  * contained in port.c  The ISR routines, which can only be compiled\r
40  * to ARM mode, are contained in this file.\r
41  *----------------------------------------------------------*/\r
42 \r
43 /* This file must always be compiled to ARM mode as it contains ISR \r
44 definitions. */\r
45 #pragma ARM\r
46 \r
47 /* Scheduler includes. */\r
48 #include "FreeRTOS.h"\r
49 #include "task.h"\r
50 \r
51 /* Constants required to handle interrupts. */\r
52 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
53 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /* The code generated by the Keil compiler does not maintain separate\r
58 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
59 use the stack as per other ports.  Instead a variable is used to keep\r
60 track of the critical section nesting.  This variable has to be stored\r
61 as part of the task context and must be initialised to a non zero value. */\r
62 \r
63 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
64 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
69 void vPortYieldProcessor( void );\r
70 \r
71 /* \r
72  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
73  * function here.\r
74  */\r
75 void vPortISRStartFirstTask( void );\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 void vPortISRStartFirstTask( void )\r
80 {\r
81         /* Simply start the scheduler.  This is included here as it can only be\r
82         called from ARM mode. */\r
83         portRESTORE_CONTEXT();\r
84 }\r
85 /*-----------------------------------------------------------*/\r
86 \r
87 /*\r
88  * Interrupt service routine for the SWI interrupt.  The vector table is\r
89  * configured within startup.s.\r
90  *\r
91  * vPortYieldProcessor() is used to manually force a context switch.  The\r
92  * SWI interrupt is generated by a call to taskYIELD() or portYIELD().\r
93  */\r
94 void vPortYieldProcessor( void ) __task\r
95 {\r
96         /* Within an IRQ ISR the link register has an offset from the true return \r
97         address, but an SWI ISR does not.  Add the offset manually so the same \r
98         ISR return code can be used in both cases. */\r
99         __asm{ ADD      LR, LR, #4 };\r
100 \r
101         /* Perform the context switch. */\r
102         portSAVE_CONTEXT();\r
103         vTaskSwitchContext();\r
104         portRESTORE_CONTEXT();  \r
105 }\r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /* \r
109  * The ISR used for the scheduler tick depends on whether the cooperative or\r
110  * the preemptive scheduler is being used.\r
111  */\r
112 \r
113 #if configUSE_PREEMPTION == 0\r
114 \r
115         /* \r
116          * The cooperative scheduler requires a normal IRQ service routine to \r
117          * simply increment the system tick. \r
118          */\r
119         void vNonPreemptiveTick( void );\r
120         void vNonPreemptiveTick( void ) __irq\r
121         {\r
122                 /* Increment the tick count - this may make a delaying task ready\r
123                 to run - but a context switch is not performed. */              \r
124                 vTaskIncrementTick();\r
125 \r
126                 /* Ready for the next interrupt. */\r
127                 T0IR = portTIMER_MATCH_ISR_BIT;\r
128                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
129         }\r
130 \r
131 #else\r
132 \r
133         /* \r
134          * The preemptive scheduler ISR is defined as "naked" as the full context \r
135          * is saved on entry as part of the context switch. \r
136          */\r
137         void vPreemptiveTick( void );\r
138         void vPreemptiveTick( void ) __task\r
139         {\r
140                 /* Save the context of the current task. */\r
141                 portSAVE_CONTEXT();     \r
142 \r
143                 /* Increment the tick count - this may make a delayed task ready to \r
144                 run. */\r
145                 vTaskIncrementTick();\r
146 \r
147                 /* Find the highest priority task that is ready to run. */\r
148                 vTaskSwitchContext();\r
149 \r
150                 /* Ready for the next interrupt. */\r
151                 T0IR = portTIMER_MATCH_ISR_BIT;\r
152                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
153                 \r
154                 /* Restore the context of the highest priority task that is ready to \r
155                 run. */\r
156                 portRESTORE_CONTEXT();\r
157         }\r
158 #endif\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /*\r
162  * The interrupt management utilities can only be called from ARM mode.  When\r
163  * KEIL_THUMB_INTERWORK is defined the utilities are defined as functions here \r
164  * to ensure a switch to ARM mode.  When KEIL_THUMB_INTERWORK is not defined \r
165  * then the utilities are defined as macros in portmacro.h - as per other \r
166  * ports.\r
167  */\r
168 #ifdef KEIL_THUMB_INTERWORK\r
169 \r
170         void vPortDisableInterruptsFromThumb( void ) __task;\r
171         void vPortEnableInterruptsFromThumb( void ) __task;\r
172 \r
173         void vPortDisableInterruptsFromThumb( void ) __task\r
174         {\r
175                 __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
176                 __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
177                 __asm{ ORR              R0, R0, #0xC0   };      /* Disable IRQ, FIQ.                                            */\r
178                 __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
179                 __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                                      */\r
180                 __asm{ BX               R14                             };      /* Return back to thumb.                                        */\r
181         }\r
182                         \r
183         void vPortEnableInterruptsFromThumb( void )     __task\r
184         {\r
185                 __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
186                 __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
187                 __asm{ BIC              R0, R0, #0xC0   };      /* Enable IRQ, FIQ.                                                     */\r
188                 __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
189                 __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                                      */\r
190                 __asm{ BX               R14                             };      /* Return back to thumb.                                        */\r
191         }\r
192 \r
193 #endif /* KEIL_THUMB_INTERWORK */\r
194 \r
195 \r
196 \r
197 /* The code generated by the Keil compiler does not maintain separate\r
198 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
199 use the stack as per other ports.  Instead a variable is used to keep\r
200 track of the critical section nesting.  This necessitates the use of a \r
201 function in place of the macro. */\r
202 \r
203 void vPortEnterCritical( void )\r
204 {\r
205         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
206         __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                                     */\r
207         __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                                            */\r
208         __asm{ ORR              R0, R0, #0xC0   };      /* Disable IRQ, FIQ.                                            */\r
209         __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.                           */\r
210         __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                                      */\r
211 \r
212         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
213         directly.  Increment ulCriticalNesting to keep a count of how many times\r
214         portENTER_CRITICAL() has been called. */\r
215         ulCriticalNesting++;\r
216 }\r
217 \r
218 void vPortExitCritical( void )\r
219 {\r
220         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
221         {\r
222                 /* Decrement the nesting count as we are leaving a critical section. */\r
223                 ulCriticalNesting--;\r
224 \r
225                 /* If the nesting level has reached zero then interrupts should be\r
226                 re-enabled. */\r
227                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
228                 {\r
229                         /* Enable interrupts as per portEXIT_CRITICAL(). */\r
230                         __asm{ STMDB    SP!, {R0}               };      /* Push R0.                                                     */\r
231                         __asm{ MRS              R0, CPSR                };      /* Get CPSR.                                            */\r
232                         __asm{ BIC              R0, R0, #0xC0   };      /* Enable IRQ, FIQ.                                     */\r
233                         __asm{ MSR              CPSR_CXSF, R0   };      /* Write back modified value.           */\r
234                         __asm{ LDMIA    SP!, {R0}               };      /* Pop R0.                                                      */\r
235                 }\r
236         }\r
237 }\r
238 \r
239 \r
240 \r
241 \r
242 \r
243 \r
244 \r
245 \r
246 \r
247 \r