]> begriffs open source - freertos/blob - portable/GCC/ARM7_AT91FR40008/portISR.c
Add SPDX-License-Identifier: MIT to MIT licensed files.
[freertos] / portable / GCC / ARM7_AT91FR40008 / portISR.c
1 /*\r
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
8  * this software and associated documentation files (the "Software"), to deal in\r
9  * the Software without restriction, including without limitation the rights to\r
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
11  * the Software, and to permit persons to whom the Software is furnished to do so,\r
12  * subject to the following conditions:\r
13  *\r
14  * The above copyright notice and this permission notice shall be included in all\r
15  * copies or substantial portions of the Software.\r
16  *\r
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\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 V3.2.4\r
38 \r
39         + The assembler statements are now included in a single asm block rather\r
40           than each line having its own asm block.\r
41 */\r
42 \r
43 \r
44 /* Scheduler includes. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 \r
48 /* Constants required to handle interrupts. */\r
49 #define portCLEAR_AIC_INTERRUPT         ( ( uint32_t ) 0 )\r
50 \r
51 /* Constants required to handle critical sections. */\r
52 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
53 volatile uint32_t ulCriticalNesting = 9999UL;\r
54 \r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
58 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
59 \r
60 /* \r
61  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
62  * function here.\r
63  */\r
64 void vPortISRStartFirstTask( void );\r
65 /*-----------------------------------------------------------*/\r
66 \r
67 void vPortISRStartFirstTask( void )\r
68 {\r
69         /* Simply start the scheduler.  This is included here as it can only be\r
70         called from ARM mode. */\r
71         portRESTORE_CONTEXT();\r
72 }\r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /*\r
76  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
77  *\r
78  * When a context switch is performed from the task level the saved task \r
79  * context is made to look as if it occurred from within the tick ISR.  This\r
80  * way the same restore context function can be used when restoring the context\r
81  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
82  */\r
83 void vPortYieldProcessor( void )\r
84 {\r
85         /* Within an IRQ ISR the link register has an offset from the true return \r
86         address, but an SWI ISR does not.  Add the offset manually so the same \r
87         ISR return code can be used in both cases. */\r
88         asm volatile ( "ADD             LR, LR, #4" );\r
89 \r
90         /* Perform the context switch.  First save the context of the current task. */\r
91         portSAVE_CONTEXT();\r
92 \r
93         /* Find the highest priority task that is ready to run. */\r
94         vTaskSwitchContext();\r
95 \r
96         /* Restore the context of the new task. */\r
97         portRESTORE_CONTEXT();  \r
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* \r
102  * The ISR used for the scheduler tick depends on whether the cooperative or\r
103  * the preemptive scheduler is being used.\r
104  */\r
105 \r
106 #if configUSE_PREEMPTION == 0\r
107 \r
108         /* The cooperative scheduler requires a normal IRQ service routine to \r
109         simply increment the system tick. */\r
110         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
111         void vNonPreemptiveTick( void )\r
112         {               \r
113         static volatile uint32_t ulDummy;\r
114 \r
115                 /* Clear tick timer interrupt indication. */\r
116                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
117 \r
118                 xTaskIncrementTick();\r
119 \r
120                 /* Acknowledge the interrupt at AIC level... */\r
121                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
122         }\r
123 \r
124 #else  /* else preemption is turned on */\r
125 \r
126         /* The preemptive scheduler is defined as "naked" as the full context is\r
127         saved on entry as part of the context switch. */\r
128         void vPreemptiveTick( void ) __attribute__((naked));\r
129         void vPreemptiveTick( void )\r
130         {\r
131                 /* Save the context of the interrupted task. */\r
132                 portSAVE_CONTEXT();     \r
133 \r
134                 /* WARNING - Do not use local (stack) variables here.  Use globals\r
135                                          if you must! */\r
136                 static volatile uint32_t ulDummy;\r
137 \r
138                 /* Clear tick timer interrupt indication. */\r
139                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
140 \r
141                 /* Increment the RTOS tick count, then look for the highest priority \r
142                 task that is ready to run. */\r
143                 if( xTaskIncrementTick() != pdFALSE )\r
144                 {\r
145                         vTaskSwitchContext();\r
146                 }\r
147 \r
148                 /* Acknowledge the interrupt at AIC level... */\r
149                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
150 \r
151                 /* Restore the context of the new task. */\r
152                 portRESTORE_CONTEXT();\r
153         }\r
154 \r
155 #endif\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * The interrupt management utilities can only be called from ARM mode.  When\r
160  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
161  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
162  * the utilities are defined as macros in portmacro.h - as per other ports.\r
163  */\r
164 #ifdef THUMB_INTERWORK\r
165 \r
166         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
167         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
168 \r
169         void vPortDisableInterruptsFromThumb( void )\r
170         {\r
171                 asm volatile ( \r
172                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
173                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
174                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
175                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
176                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
177                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
178         }\r
179                         \r
180         void vPortEnableInterruptsFromThumb( void )\r
181         {\r
182                 asm volatile ( \r
183                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
184                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
185                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
186                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
187                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
188                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
189         }\r
190 \r
191 #endif /* THUMB_INTERWORK */\r
192 \r
193 /* The code generated by the GCC compiler uses the stack in different ways at\r
194 different optimisation levels.  The interrupt flags can therefore not always\r
195 be saved to the stack.  Instead the critical section nesting level is stored\r
196 in a variable, which is then saved as part of the stack context. */\r
197 void vPortEnterCritical( void )\r
198 {\r
199         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
200         asm volatile ( \r
201                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
202                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
203                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
204                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
205                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
206 \r
207         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
208         directly.  Increment ulCriticalNesting to keep a count of how many times\r
209         portENTER_CRITICAL() has been called. */\r
210         ulCriticalNesting++;\r
211 }\r
212 \r
213 void vPortExitCritical( void )\r
214 {\r
215         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
216         {\r
217                 /* Decrement the nesting count as we are leaving a critical section. */\r
218                 ulCriticalNesting--;\r
219 \r
220                 /* If the nesting level has reached zero then interrupts should be\r
221                 re-enabled. */\r
222                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
223                 {\r
224                         /* Enable interrupts as per portEXIT_CRITICAL().                                */\r
225                         asm volatile ( \r
226                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
227                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
228                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
229                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
230                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
231                 }\r
232         }\r
233 }\r
234 \r