]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_LPC2000/portISR.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_LPC2000 / portISR.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*-----------------------------------------------------------\r
31  * Components that can be compiled to either ARM or THUMB mode are\r
32  * contained in port.c  The ISR routines, which can only be compiled\r
33  * to ARM mode, are contained in this file.\r
34  *----------------------------------------------------------*/\r
35 \r
36 /*\r
37         Changes from V2.5.2\r
38 \r
39         + The critical section management functions have been changed.  These no\r
40           longer modify the stack and are safe to use at all optimisation levels.\r
41           The functions are now also the same for both ARM and THUMB modes.\r
42 \r
43         Changes from V2.6.0\r
44 \r
45         + Removed the 'static' from the definition of vNonPreemptiveTick() to\r
46           allow the demo to link when using the cooperative scheduler.\r
47 \r
48         Changes from V3.2.4\r
49 \r
50         + The assembler statements are now included in a single asm block rather\r
51           than each line having its own asm block.\r
52 */\r
53 \r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 \r
58 /* Constants required to handle interrupts. */\r
59 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )\r
60 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )\r
61 \r
62 /* Constants required to handle critical sections. */\r
63 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
64 volatile uint32_t ulCriticalNesting = 9999UL;\r
65 \r
66 /*-----------------------------------------------------------*/\r
67 \r
68 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
69 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
70 \r
71 /*\r
72  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
73  * function here.\r
74  */\r
75 void vPortISRStartFirstTask( void );\r
76 /*-----------------------------------------------------------*/\r
77 \r
78 void vPortISRStartFirstTask( void )\r
79 {\r
80         /* Simply start the scheduler.  This is included here as it can only be\r
81         called from ARM mode. */\r
82         portRESTORE_CONTEXT();\r
83 }\r
84 /*-----------------------------------------------------------*/\r
85 \r
86 /*\r
87  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
88  *\r
89  * When a context switch is performed from the task level the saved task\r
90  * context is made to look as if it occurred from within the tick ISR.  This\r
91  * way the same restore context function can be used when restoring the context\r
92  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
93  */\r
94 void vPortYieldProcessor( void )\r
95 {\r
96         /* Within an IRQ ISR the link register has an offset from the true return\r
97         address, but an SWI ISR does not.  Add the offset manually so the same\r
98         ISR return code can be used in both cases. */\r
99         __asm volatile ( "ADD           LR, LR, #4" );\r
100 \r
101         /* Perform the context switch.  First save the context of the current task. */\r
102         portSAVE_CONTEXT();\r
103 \r
104         /* Find the highest priority task that is ready to run. */\r
105         __asm volatile ( "bl vTaskSwitchContext" );\r
106 \r
107         /* Restore the context of the new task. */\r
108         portRESTORE_CONTEXT();\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * The ISR used for the scheduler tick.\r
114  */\r
115 void vTickISR( void ) __attribute__((naked));\r
116 void vTickISR( void )\r
117 {\r
118         /* Save the context of the interrupted task. */\r
119         portSAVE_CONTEXT();\r
120 \r
121         /* Increment the RTOS tick count, then look for the highest priority\r
122         task that is ready to run. */\r
123         __asm volatile\r
124         (\r
125                 "       bl xTaskIncrementTick   \t\n" \\r
126                 "       cmp r0, #0                              \t\n" \\r
127                 "       beq SkipContextSwitch   \t\n" \\r
128                 "       bl vTaskSwitchContext   \t\n" \\r
129                 "SkipContextSwitch:                     \t\n"\r
130         );\r
131 \r
132         /* Ready for the next interrupt. */\r
133         T0_IR = portTIMER_MATCH_ISR_BIT;\r
134         VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
135 \r
136         /* Restore the context of the new task. */\r
137         portRESTORE_CONTEXT();\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /*\r
142  * The interrupt management utilities can only be called from ARM mode.  When\r
143  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
144  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
145  * the utilities are defined as macros in portmacro.h - as per other ports.\r
146  */\r
147 #ifdef THUMB_INTERWORK\r
148 \r
149         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
150         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
151 \r
152         void vPortDisableInterruptsFromThumb( void )\r
153         {\r
154                 __asm volatile (\r
155                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
156                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
157                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
158                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
159                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
160                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
161         }\r
162 \r
163         void vPortEnableInterruptsFromThumb( void )\r
164         {\r
165                 __asm volatile (\r
166                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
167                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
168                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */\r
169                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
170                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
171                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
172         }\r
173 \r
174 #endif /* THUMB_INTERWORK */\r
175 \r
176 /* The code generated by the GCC compiler uses the stack in different ways at\r
177 different optimisation levels.  The interrupt flags can therefore not always\r
178 be saved to the stack.  Instead the critical section nesting level is stored\r
179 in a variable, which is then saved as part of the stack context. */\r
180 void vPortEnterCritical( void )\r
181 {\r
182         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
183         __asm volatile (\r
184                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
185                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
186                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
187                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
188                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
189 \r
190         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
191         directly.  Increment ulCriticalNesting to keep a count of how many times\r
192         portENTER_CRITICAL() has been called. */\r
193         ulCriticalNesting++;\r
194 }\r
195 \r
196 void vPortExitCritical( void )\r
197 {\r
198         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
199         {\r
200                 /* Decrement the nesting count as we are leaving a critical section. */\r
201                 ulCriticalNesting--;\r
202 \r
203                 /* If the nesting level has reached zero then interrupts should be\r
204                 re-enabled. */\r
205                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
206                 {\r
207                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
208                         __asm volatile (\r
209                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */\r
210                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */\r
211                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */\r
212                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */\r
213                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
214                 }\r
215         }\r
216 }\r