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