]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_AT91FR40008/portISR.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_AT91FR40008 / portISR.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 V3.2.4\r
37 \r
38         + The assembler statements are now included in a single asm block rather\r
39           than each line having its own asm block.\r
40 */\r
41 \r
42 \r
43 /* Scheduler includes. */\r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 \r
47 /* Constants required to handle interrupts. */\r
48 #define portCLEAR_AIC_INTERRUPT         ( ( uint32_t ) 0 )\r
49 \r
50 /* Constants required to handle critical sections. */\r
51 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
52 volatile uint32_t ulCriticalNesting = 9999UL;\r
53 \r
54 /*-----------------------------------------------------------*/\r
55 \r
56 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
57 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
58 \r
59 /* \r
60  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
61  * function here.\r
62  */\r
63 void vPortISRStartFirstTask( void );\r
64 /*-----------------------------------------------------------*/\r
65 \r
66 void vPortISRStartFirstTask( void )\r
67 {\r
68         /* Simply start the scheduler.  This is included here as it can only be\r
69         called from ARM mode. */\r
70         portRESTORE_CONTEXT();\r
71 }\r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /*\r
75  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
76  *\r
77  * When a context switch is performed from the task level the saved task \r
78  * context is made to look as if it occurred from within the tick ISR.  This\r
79  * way the same restore context function can be used when restoring the context\r
80  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
81  */\r
82 void vPortYieldProcessor( void )\r
83 {\r
84         /* Within an IRQ ISR the link register has an offset from the true return \r
85         address, but an SWI ISR does not.  Add the offset manually so the same \r
86         ISR return code can be used in both cases. */\r
87         asm volatile ( "ADD             LR, LR, #4" );\r
88 \r
89         /* Perform the context switch.  First save the context of the current task. */\r
90         portSAVE_CONTEXT();\r
91 \r
92         /* Find the highest priority task that is ready to run. */\r
93         vTaskSwitchContext();\r
94 \r
95         /* Restore the context of the new task. */\r
96         portRESTORE_CONTEXT();  \r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /* \r
101  * The ISR used for the scheduler tick depends on whether the cooperative or\r
102  * the preemptive scheduler is being used.\r
103  */\r
104 \r
105 #if configUSE_PREEMPTION == 0\r
106 \r
107         /* The cooperative scheduler requires a normal IRQ service routine to \r
108         simply increment the system tick. */\r
109         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
110         void vNonPreemptiveTick( void )\r
111         {               \r
112         static volatile uint32_t ulDummy;\r
113 \r
114                 /* Clear tick timer interrupt indication. */\r
115                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
116 \r
117                 xTaskIncrementTick();\r
118 \r
119                 /* Acknowledge the interrupt at AIC level... */\r
120                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
121         }\r
122 \r
123 #else  /* else preemption is turned on */\r
124 \r
125         /* The preemptive scheduler is defined as "naked" as the full context is\r
126         saved on entry as part of the context switch. */\r
127         void vPreemptiveTick( void ) __attribute__((naked));\r
128         void vPreemptiveTick( void )\r
129         {\r
130                 /* Save the context of the interrupted task. */\r
131                 portSAVE_CONTEXT();     \r
132 \r
133                 /* WARNING - Do not use local (stack) variables here.  Use globals\r
134                                          if you must! */\r
135                 static volatile uint32_t ulDummy;\r
136 \r
137                 /* Clear tick timer interrupt indication. */\r
138                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
139 \r
140                 /* Increment the RTOS tick count, then look for the highest priority \r
141                 task that is ready to run. */\r
142                 if( xTaskIncrementTick() != pdFALSE )\r
143                 {\r
144                         vTaskSwitchContext();\r
145                 }\r
146 \r
147                 /* Acknowledge the interrupt at AIC level... */\r
148                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
149 \r
150                 /* Restore the context of the new task. */\r
151                 portRESTORE_CONTEXT();\r
152         }\r
153 \r
154 #endif\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 /*\r
158  * The interrupt management utilities can only be called from ARM mode.  When\r
159  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
160  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
161  * the utilities are defined as macros in portmacro.h - as per other ports.\r
162  */\r
163 #ifdef THUMB_INTERWORK\r
164 \r
165         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
166         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
167 \r
168         void vPortDisableInterruptsFromThumb( void )\r
169         {\r
170                 asm volatile ( \r
171                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
172                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
173                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
174                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
175                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
176                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
177         }\r
178                         \r
179         void vPortEnableInterruptsFromThumb( void )\r
180         {\r
181                 asm volatile ( \r
182                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
183                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
184                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
185                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
186                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
187                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
188         }\r
189 \r
190 #endif /* THUMB_INTERWORK */\r
191 \r
192 /* The code generated by the GCC compiler uses the stack in different ways at\r
193 different optimisation levels.  The interrupt flags can therefore not always\r
194 be saved to the stack.  Instead the critical section nesting level is stored\r
195 in a variable, which is then saved as part of the stack context. */\r
196 void vPortEnterCritical( void )\r
197 {\r
198         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
199         asm volatile ( \r
200                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
201                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
202                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
203                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
204                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
205 \r
206         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
207         directly.  Increment ulCriticalNesting to keep a count of how many times\r
208         portENTER_CRITICAL() has been called. */\r
209         ulCriticalNesting++;\r
210 }\r
211 \r
212 void vPortExitCritical( void )\r
213 {\r
214         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
215         {\r
216                 /* Decrement the nesting count as we are leaving a critical section. */\r
217                 ulCriticalNesting--;\r
218 \r
219                 /* If the nesting level has reached zero then interrupts should be\r
220                 re-enabled. */\r
221                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
222                 {\r
223                         /* Enable interrupts as per portEXIT_CRITICAL().                                */\r
224                         asm volatile ( \r
225                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
226                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
227                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
228                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
229                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
230                 }\r
231         }\r
232 }\r
233 \r