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