]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_LPC2000/portISR.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_LPC2000 / portISR.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 /*\r
74         Changes from V2.5.2\r
75 \r
76         + The critical section management functions have been changed.  These no\r
77           longer modify the stack and are safe to use at all optimisation levels.\r
78           The functions are now also the same for both ARM and THUMB modes.\r
79 \r
80         Changes from V2.6.0\r
81 \r
82         + Removed the 'static' from the definition of vNonPreemptiveTick() to\r
83           allow the demo to link when using the cooperative scheduler.\r
84 \r
85         Changes from V3.2.4\r
86 \r
87         + The assembler statements are now included in a single asm block rather\r
88           than each line having its own asm block.\r
89 */\r
90 \r
91 \r
92 /* Scheduler includes. */\r
93 #include "FreeRTOS.h"\r
94 \r
95 /* Constants required to handle interrupts. */\r
96 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )\r
97 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )\r
98 \r
99 /* Constants required to handle critical sections. */\r
100 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
101 volatile uint32_t ulCriticalNesting = 9999UL;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
106 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
107 \r
108 /*\r
109  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
110  * function here.\r
111  */\r
112 void vPortISRStartFirstTask( void );\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vPortISRStartFirstTask( void )\r
116 {\r
117         /* Simply start the scheduler.  This is included here as it can only be\r
118         called from ARM mode. */\r
119         portRESTORE_CONTEXT();\r
120 }\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /*\r
124  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
125  *\r
126  * When a context switch is performed from the task level the saved task\r
127  * context is made to look as if it occurred from within the tick ISR.  This\r
128  * way the same restore context function can be used when restoring the context\r
129  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
130  */\r
131 void vPortYieldProcessor( void )\r
132 {\r
133         /* Within an IRQ ISR the link register has an offset from the true return\r
134         address, but an SWI ISR does not.  Add the offset manually so the same\r
135         ISR return code can be used in both cases. */\r
136         __asm volatile ( "ADD           LR, LR, #4" );\r
137 \r
138         /* Perform the context switch.  First save the context of the current task. */\r
139         portSAVE_CONTEXT();\r
140 \r
141         /* Find the highest priority task that is ready to run. */\r
142         __asm volatile ( "bl vTaskSwitchContext" );\r
143 \r
144         /* Restore the context of the new task. */\r
145         portRESTORE_CONTEXT();\r
146 }\r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /*\r
150  * The ISR used for the scheduler tick.\r
151  */\r
152 void vTickISR( void ) __attribute__((naked));\r
153 void vTickISR( 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         T0_IR = portTIMER_MATCH_ISR_BIT;\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 \r
178 /*\r
179  * The interrupt management utilities can only be called from ARM mode.  When\r
180  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
181  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
182  * the utilities are defined as macros in portmacro.h - as per other ports.\r
183  */\r
184 #ifdef THUMB_INTERWORK\r
185 \r
186         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
187         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
188 \r
189         void vPortDisableInterruptsFromThumb( void )\r
190         {\r
191                 __asm volatile (\r
192                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
193                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
194                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
195                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
196                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
197                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
198         }\r
199 \r
200         void vPortEnableInterruptsFromThumb( void )\r
201         {\r
202                 __asm volatile (\r
203                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
204                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
205                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */\r
206                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
207                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
208                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
209         }\r
210 \r
211 #endif /* THUMB_INTERWORK */\r
212 \r
213 /* The code generated by the GCC compiler uses the stack in different ways at\r
214 different optimisation levels.  The interrupt flags can therefore not always\r
215 be saved to the stack.  Instead the critical section nesting level is stored\r
216 in a variable, which is then saved as part of the stack context. */\r
217 void vPortEnterCritical( void )\r
218 {\r
219         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
220         __asm volatile (\r
221                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
222                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
223                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
224                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
225                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
226 \r
227         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
228         directly.  Increment ulCriticalNesting to keep a count of how many times\r
229         portENTER_CRITICAL() has been called. */\r
230         ulCriticalNesting++;\r
231 }\r
232 \r
233 void vPortExitCritical( void )\r
234 {\r
235         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
236         {\r
237                 /* Decrement the nesting count as we are leaving a critical section. */\r
238                 ulCriticalNesting--;\r
239 \r
240                 /* If the nesting level has reached zero then interrupts should be\r
241                 re-enabled. */\r
242                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
243                 {\r
244                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
245                         __asm volatile (\r
246                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */\r
247                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */\r
248                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */\r
249                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */\r
250                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
251                 }\r
252         }\r
253 }\r