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