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