]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_LPC23xx/portISR.c
c966f23c29699715907474c54eedb717ab81337d
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_LPC23xx / portISR.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\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.\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 /* Scheduler includes. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 \r
39 /* Constants required to handle interrupts. */\r
40 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )\r
41 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )\r
42 \r
43 /* Constants required to handle critical sections. */\r
44 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
45 volatile uint32_t ulCriticalNesting = 9999UL;\r
46 \r
47 /*-----------------------------------------------------------*/\r
48 \r
49 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
50 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
51 \r
52 /* \r
53  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
54  * function here.\r
55  */\r
56 void vPortISRStartFirstTask( void );\r
57 /*-----------------------------------------------------------*/\r
58 \r
59 void vPortISRStartFirstTask( void )\r
60 {\r
61         /* Simply start the scheduler.  This is included here as it can only be\r
62         called from ARM mode. */\r
63         portRESTORE_CONTEXT();\r
64 }\r
65 /*-----------------------------------------------------------*/\r
66 \r
67 /*\r
68  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
69  *\r
70  * When a context switch is performed from the task level the saved task \r
71  * context is made to look as if it occurred from within the tick ISR.  This\r
72  * way the same restore context function can be used when restoring the context\r
73  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
74  */\r
75 void vPortYieldProcessor( void )\r
76 {\r
77         /* Within an IRQ ISR the link register has an offset from the true return \r
78         address, but an SWI ISR does not.  Add the offset manually so the same \r
79         ISR return code can be used in both cases. */\r
80         __asm volatile ( "ADD           LR, LR, #4" );\r
81 \r
82         /* Perform the context switch.  First save the context of the current task. */\r
83         portSAVE_CONTEXT();\r
84 \r
85         /* Find the highest priority task that is ready to run. */\r
86         __asm volatile( "bl                     vTaskSwitchContext" );\r
87 \r
88         /* Restore the context of the new task. */\r
89         portRESTORE_CONTEXT();  \r
90 }\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* \r
94  * The ISR used for the scheduler tick depends on whether the cooperative or\r
95  * the preemptive scheduler is being used.\r
96  */\r
97 \r
98 \r
99 #if configUSE_PREEMPTION == 0\r
100 \r
101         /* The cooperative scheduler requires a normal IRQ service routine to \r
102         simply increment the system tick. */\r
103         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
104         void vNonPreemptiveTick( void )\r
105         {       \r
106                 xTaskIncrementTick();\r
107                 T0IR = 2;\r
108                 VICVectAddr = portCLEAR_VIC_INTERRUPT;\r
109         }\r
110 \r
111 #else\r
112 \r
113         /* The preemptive scheduler is defined as "naked" as the full context is\r
114         saved on entry as part of the context switch. */\r
115         void vPreemptiveTick( void ) __attribute__((naked));\r
116         void vPreemptiveTick( 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                 T0IR = 2;\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 #endif\r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /*\r
144  * The interrupt management utilities can only be called from ARM mode.  When\r
145  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
146  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
147  * the utilities are defined as macros in portmacro.h - as per other ports.\r
148  */\r
149 #ifdef THUMB_INTERWORK\r
150 \r
151         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
152         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
153 \r
154         void vPortDisableInterruptsFromThumb( void )\r
155         {\r
156                 __asm volatile ( \r
157                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
158                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
159                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
160                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
161                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
162                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
163         }\r
164                         \r
165         void vPortEnableInterruptsFromThumb( void )\r
166         {\r
167                 __asm volatile ( \r
168                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
169                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
170                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
171                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
172                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
173                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
174         }\r
175 \r
176 #endif /* THUMB_INTERWORK */\r
177 \r
178 /* The code generated by the GCC compiler uses the stack in different ways at\r
179 different optimisation levels.  The interrupt flags can therefore not always\r
180 be saved to the stack.  Instead the critical section nesting level is stored\r
181 in a variable, which is then saved as part of the stack context. */\r
182 void vPortEnterCritical( void )\r
183 {\r
184         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
185         __asm volatile ( \r
186                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
187                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
188                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
189                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
190                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
191 \r
192         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
193         directly.  Increment ulCriticalNesting to keep a count of how many times\r
194         portENTER_CRITICAL() has been called. */\r
195         ulCriticalNesting++;\r
196 }\r
197 \r
198 void vPortExitCritical( void )\r
199 {\r
200         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
201         {\r
202                 /* Decrement the nesting count as we are leaving a critical section. */\r
203                 ulCriticalNesting--;\r
204 \r
205                 /* If the nesting level has reached zero then interrupts should be\r
206                 re-enabled. */\r
207                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
208                 {\r
209                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
210                         __asm volatile ( \r
211                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
212                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
213                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
214                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
215                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
216                 }\r
217         }\r
218 }\r