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