]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/ARM7_LPC23xx/portISR.c
Update version number to V7.0.1.
[freertos] / Source / portable / GCC / ARM7_LPC23xx / portISR.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /*-----------------------------------------------------------\r
56  * Components that can be compiled to either ARM or THUMB mode are\r
57  * contained in port.c  The ISR routines, which can only be compiled\r
58  * to ARM mode, are contained in this file.\r
59  *----------------------------------------------------------*/\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 /* Constants required to handle critical sections. */\r
70 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
71 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
76 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
77 \r
78 /* \r
79  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
80  * function here.\r
81  */\r
82 void vPortISRStartFirstTask( void );\r
83 /*-----------------------------------------------------------*/\r
84 \r
85 void vPortISRStartFirstTask( void )\r
86 {\r
87         /* Simply start the scheduler.  This is included here as it can only be\r
88         called from ARM mode. */\r
89         portRESTORE_CONTEXT();\r
90 }\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /*\r
94  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
95  *\r
96  * When a context switch is performed from the task level the saved task \r
97  * context is made to look as if it occurred from within the tick ISR.  This\r
98  * way the same restore context function can be used when restoring the context\r
99  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
100  */\r
101 void vPortYieldProcessor( void )\r
102 {\r
103         /* Within an IRQ ISR the link register has an offset from the true return \r
104         address, but an SWI ISR does not.  Add the offset manually so the same \r
105         ISR return code can be used in both cases. */\r
106         __asm volatile ( "ADD           LR, LR, #4" );\r
107 \r
108         /* Perform the context switch.  First save the context of the current task. */\r
109         portSAVE_CONTEXT();\r
110 \r
111         /* Find the highest priority task that is ready to run. */\r
112         __asm volatile( "bl                     vTaskSwitchContext" );\r
113 \r
114         /* Restore the context of the new task. */\r
115         portRESTORE_CONTEXT();  \r
116 }\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* \r
120  * The ISR used for the scheduler tick depends on whether the cooperative or\r
121  * the preemptive scheduler is being used.\r
122  */\r
123 \r
124 \r
125 #if configUSE_PREEMPTION == 0\r
126 \r
127         /* The cooperative scheduler requires a normal IRQ service routine to \r
128         simply increment the system tick. */\r
129         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
130         void vNonPreemptiveTick( void )\r
131         {       \r
132                 vTaskIncrementTick();\r
133                 T0IR = 2;\r
134                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
135         }\r
136 \r
137 #else\r
138 \r
139         /* The preemptive scheduler is defined as "naked" as the full context is\r
140         saved on entry as part of the context switch. */\r
141         void vPreemptiveTick( void ) __attribute__((naked));\r
142         void vPreemptiveTick( void )\r
143         {\r
144                 /* Save the context of the interrupted task. */\r
145                 portSAVE_CONTEXT();     \r
146 \r
147                 /* Increment the RTOS tick count, then look for the highest priority \r
148                 task that is ready to run. */\r
149                 __asm volatile( "bl vTaskIncrementTick" );\r
150                 __asm volatile( "bl vTaskSwitchContext" );\r
151 \r
152                 /* Ready for the next interrupt. */\r
153                 T0IR = 2;\r
154                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
155                 \r
156                 /* Restore the context of the new task. */\r
157                 portRESTORE_CONTEXT();\r
158         }\r
159 \r
160 #endif\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * The interrupt management utilities can only be called from ARM mode.  When\r
165  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
166  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
167  * the utilities are defined as macros in portmacro.h - as per other ports.\r
168  */\r
169 #ifdef THUMB_INTERWORK\r
170 \r
171         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
172         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
173 \r
174         void vPortDisableInterruptsFromThumb( void )\r
175         {\r
176                 __asm volatile ( \r
177                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
178                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
179                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
180                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
181                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
182                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
183         }\r
184                         \r
185         void vPortEnableInterruptsFromThumb( void )\r
186         {\r
187                 __asm volatile ( \r
188                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
189                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
190                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
191                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
192                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
193                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
194         }\r
195 \r
196 #endif /* THUMB_INTERWORK */\r
197 \r
198 /* The code generated by the GCC compiler uses the stack in different ways at\r
199 different optimisation levels.  The interrupt flags can therefore not always\r
200 be saved to the stack.  Instead the critical section nesting level is stored\r
201 in a variable, which is then saved as part of the stack context. */\r
202 void vPortEnterCritical( void )\r
203 {\r
204         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
205         __asm volatile ( \r
206                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
207                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
208                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
209                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
210                 "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 volatile ( \r
231                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
232                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
233                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
234                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
235                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
236                 }\r
237         }\r
238 }\r