]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_LPC23xx/portISR.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_LPC23xx / portISR.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /*-----------------------------------------------------------\r
67  * Components that can be compiled to either ARM or THUMB mode are\r
68  * contained in port.c  The ISR routines, which can only be compiled\r
69  * to ARM mode, are contained in this file.\r
70  *----------------------------------------------------------*/\r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 /* Constants required to handle interrupts. */\r
77 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
78 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
79 \r
80 /* Constants required to handle critical sections. */\r
81 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
82 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
87 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
88 \r
89 /* \r
90  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
91  * function here.\r
92  */\r
93 void vPortISRStartFirstTask( void );\r
94 /*-----------------------------------------------------------*/\r
95 \r
96 void vPortISRStartFirstTask( void )\r
97 {\r
98         /* Simply start the scheduler.  This is included here as it can only be\r
99         called from ARM mode. */\r
100         portRESTORE_CONTEXT();\r
101 }\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
106  *\r
107  * When a context switch is performed from the task level the saved task \r
108  * context is made to look as if it occurred from within the tick ISR.  This\r
109  * way the same restore context function can be used when restoring the context\r
110  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
111  */\r
112 void vPortYieldProcessor( void )\r
113 {\r
114         /* Within an IRQ ISR the link register has an offset from the true return \r
115         address, but an SWI ISR does not.  Add the offset manually so the same \r
116         ISR return code can be used in both cases. */\r
117         __asm volatile ( "ADD           LR, LR, #4" );\r
118 \r
119         /* Perform the context switch.  First save the context of the current task. */\r
120         portSAVE_CONTEXT();\r
121 \r
122         /* Find the highest priority task that is ready to run. */\r
123         __asm volatile( "bl                     vTaskSwitchContext" );\r
124 \r
125         /* Restore the context of the new task. */\r
126         portRESTORE_CONTEXT();  \r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* \r
131  * The ISR used for the scheduler tick depends on whether the cooperative or\r
132  * the preemptive scheduler is being used.\r
133  */\r
134 \r
135 \r
136 #if configUSE_PREEMPTION == 0\r
137 \r
138         /* The cooperative scheduler requires a normal IRQ service routine to \r
139         simply increment the system tick. */\r
140         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
141         void vNonPreemptiveTick( void )\r
142         {       \r
143                 xTaskIncrementTick();\r
144                 T0IR = 2;\r
145                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
146         }\r
147 \r
148 #else\r
149 \r
150         /* The preemptive scheduler is defined as "naked" as the full context is\r
151         saved on entry as part of the context switch. */\r
152         void vPreemptiveTick( void ) __attribute__((naked));\r
153         void vPreemptiveTick( void )\r
154         {\r
155                 /* Save the context of the interrupted task. */\r
156                 portSAVE_CONTEXT();     \r
157 \r
158                 /* Increment the RTOS tick count, then look for the highest priority \r
159                 task that is ready to run. */\r
160                 __asm volatile\r
161                 (\r
162                         "       bl xTaskIncrementTick   \t\n" \\r
163                         "       cmp r0, #0                              \t\n" \\r
164                         "       beq SkipContextSwitch   \t\n" \\r
165                         "       bl vTaskSwitchContext   \t\n" \\r
166                         "SkipContextSwitch:                     \t\n"\r
167                 );\r
168 \r
169                 /* Ready for the next interrupt. */\r
170                 T0IR = 2;\r
171                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
172                 \r
173                 /* Restore the context of the new task. */\r
174                 portRESTORE_CONTEXT();\r
175         }\r
176 \r
177 #endif\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 /*\r
181  * The interrupt management utilities can only be called from ARM mode.  When\r
182  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
183  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
184  * the utilities are defined as macros in portmacro.h - as per other ports.\r
185  */\r
186 #ifdef THUMB_INTERWORK\r
187 \r
188         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
189         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
190 \r
191         void vPortDisableInterruptsFromThumb( void )\r
192         {\r
193                 __asm volatile ( \r
194                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
195                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
196                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
197                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
198                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
199                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
200         }\r
201                         \r
202         void vPortEnableInterruptsFromThumb( void )\r
203         {\r
204                 __asm volatile ( \r
205                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
206                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
207                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
208                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
209                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
210                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
211         }\r
212 \r
213 #endif /* THUMB_INTERWORK */\r
214 \r
215 /* The code generated by the GCC compiler uses the stack in different ways at\r
216 different optimisation levels.  The interrupt flags can therefore not always\r
217 be saved to the stack.  Instead the critical section nesting level is stored\r
218 in a variable, which is then saved as part of the stack context. */\r
219 void vPortEnterCritical( void )\r
220 {\r
221         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
222         __asm volatile ( \r
223                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
224                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
225                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
226                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
227                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
228 \r
229         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
230         directly.  Increment ulCriticalNesting to keep a count of how many times\r
231         portENTER_CRITICAL() has been called. */\r
232         ulCriticalNesting++;\r
233 }\r
234 \r
235 void vPortExitCritical( void )\r
236 {\r
237         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
238         {\r
239                 /* Decrement the nesting count as we are leaving a critical section. */\r
240                 ulCriticalNesting--;\r
241 \r
242                 /* If the nesting level has reached zero then interrupts should be\r
243                 re-enabled. */\r
244                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
245                 {\r
246                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
247                         __asm volatile ( \r
248                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
249                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
250                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
251                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
252                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
253                 }\r
254         }\r
255 }\r