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