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