]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_AT91FR40008/portISR.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_AT91FR40008 / portISR.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /*-----------------------------------------------------------\r
68  * Components that can be compiled to either ARM or THUMB mode are\r
69  * contained in port.c  The ISR routines, which can only be compiled\r
70  * to ARM mode, are contained in this file.\r
71  *----------------------------------------------------------*/\r
72 \r
73 /*\r
74         Changes from V3.2.4\r
75 \r
76         + The assembler statements are now included in a single asm block rather\r
77           than each line having its own asm block.\r
78 */\r
79 \r
80 \r
81 /* Scheduler includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \r
85 /* Constants required to handle interrupts. */\r
86 #define portCLEAR_AIC_INTERRUPT         ( ( uint32_t ) 0 )\r
87 \r
88 /* Constants required to handle critical sections. */\r
89 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
90 volatile uint32_t ulCriticalNesting = 9999UL;\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
95 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
96 \r
97 /* \r
98  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
99  * function here.\r
100  */\r
101 void vPortISRStartFirstTask( void );\r
102 /*-----------------------------------------------------------*/\r
103 \r
104 void vPortISRStartFirstTask( void )\r
105 {\r
106         /* Simply start the scheduler.  This is included here as it can only be\r
107         called from ARM mode. */\r
108         portRESTORE_CONTEXT();\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /*\r
113  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
114  *\r
115  * When a context switch is performed from the task level the saved task \r
116  * context is made to look as if it occurred from within the tick ISR.  This\r
117  * way the same restore context function can be used when restoring the context\r
118  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
119  */\r
120 void vPortYieldProcessor( void )\r
121 {\r
122         /* Within an IRQ ISR the link register has an offset from the true return \r
123         address, but an SWI ISR does not.  Add the offset manually so the same \r
124         ISR return code can be used in both cases. */\r
125         asm volatile ( "ADD             LR, LR, #4" );\r
126 \r
127         /* Perform the context switch.  First save the context of the current task. */\r
128         portSAVE_CONTEXT();\r
129 \r
130         /* Find the highest priority task that is ready to run. */\r
131         vTaskSwitchContext();\r
132 \r
133         /* Restore the context of the new task. */\r
134         portRESTORE_CONTEXT();  \r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 /* \r
139  * The ISR used for the scheduler tick depends on whether the cooperative or\r
140  * the preemptive scheduler is being used.\r
141  */\r
142 \r
143 #if configUSE_PREEMPTION == 0\r
144 \r
145         /* The cooperative scheduler requires a normal IRQ service routine to \r
146         simply increment the system tick. */\r
147         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
148         void vNonPreemptiveTick( void )\r
149         {               \r
150         static volatile uint32_t ulDummy;\r
151 \r
152                 /* Clear tick timer interrupt indication. */\r
153                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
154 \r
155                 xTaskIncrementTick();\r
156 \r
157                 /* Acknowledge the interrupt at AIC level... */\r
158                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
159         }\r
160 \r
161 #else  /* else preemption is turned on */\r
162 \r
163         /* The preemptive scheduler is defined as "naked" as the full context is\r
164         saved on entry as part of the context switch. */\r
165         void vPreemptiveTick( void ) __attribute__((naked));\r
166         void vPreemptiveTick( void )\r
167         {\r
168                 /* Save the context of the interrupted task. */\r
169                 portSAVE_CONTEXT();     \r
170 \r
171                 /* WARNING - Do not use local (stack) variables here.  Use globals\r
172                                          if you must! */\r
173                 static volatile uint32_t ulDummy;\r
174 \r
175                 /* Clear tick timer interrupt indication. */\r
176                 ulDummy = portTIMER_REG_BASE_PTR->TC_SR;  \r
177 \r
178                 /* Increment the RTOS tick count, then look for the highest priority \r
179                 task that is ready to run. */\r
180                 if( xTaskIncrementTick() != pdFALSE )\r
181                 {\r
182                         vTaskSwitchContext();\r
183                 }\r
184 \r
185                 /* Acknowledge the interrupt at AIC level... */\r
186                 AT91C_BASE_AIC->AIC_EOICR = portCLEAR_AIC_INTERRUPT;\r
187 \r
188                 /* Restore the context of the new task. */\r
189                 portRESTORE_CONTEXT();\r
190         }\r
191 \r
192 #endif\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /*\r
196  * The interrupt management utilities can only be called from ARM mode.  When\r
197  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
198  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
199  * the utilities are defined as macros in portmacro.h - as per other ports.\r
200  */\r
201 #ifdef THUMB_INTERWORK\r
202 \r
203         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
204         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
205 \r
206         void vPortDisableInterruptsFromThumb( void )\r
207         {\r
208                 asm volatile ( \r
209                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
210                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
211                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
212                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
213                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
214                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
215         }\r
216                         \r
217         void vPortEnableInterruptsFromThumb( void )\r
218         {\r
219                 asm volatile ( \r
220                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
221                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
222                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
223                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
224                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
225                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
226         }\r
227 \r
228 #endif /* THUMB_INTERWORK */\r
229 \r
230 /* The code generated by the GCC compiler uses the stack in different ways at\r
231 different optimisation levels.  The interrupt flags can therefore not always\r
232 be saved to the stack.  Instead the critical section nesting level is stored\r
233 in a variable, which is then saved as part of the stack context. */\r
234 void vPortEnterCritical( void )\r
235 {\r
236         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
237         asm volatile ( \r
238                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
239                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
240                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
241                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
242                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
243 \r
244         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
245         directly.  Increment ulCriticalNesting to keep a count of how many times\r
246         portENTER_CRITICAL() has been called. */\r
247         ulCriticalNesting++;\r
248 }\r
249 \r
250 void vPortExitCritical( void )\r
251 {\r
252         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
253         {\r
254                 /* Decrement the nesting count as we are leaving a critical section. */\r
255                 ulCriticalNesting--;\r
256 \r
257                 /* If the nesting level has reached zero then interrupts should be\r
258                 re-enabled. */\r
259                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
260                 {\r
261                         /* Enable interrupts as per portEXIT_CRITICAL().                                */\r
262                         asm volatile ( \r
263                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
264                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
265                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
266                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
267                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
268                 }\r
269         }\r
270 }\r
271 \r