]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_AT91SAM7S/portISR.c
744a58e99ab7c2bb44ae2925715b2c11b7fdb8e1
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_AT91SAM7S / portISR.c
1 /*\r
2     FreeRTOS V7.2.0 - 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 /* Scheduler includes. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \r
85 #include "AT91SAM7X256.h"\r
86 \r
87 /* Constants required to handle interrupts. */\r
88 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned char ) 0x01 )\r
89 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned long ) 0 )\r
90 \r
91 /* Constants required to handle critical sections. */\r
92 #define portNO_CRITICAL_NESTING         ( ( unsigned long ) 0 )\r
93 volatile unsigned long ulCriticalNesting = 9999UL;\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* ISR to handle manual context switches (from a call to taskYIELD()). */\r
98 void vPortYieldProcessor( void ) __attribute__((interrupt("SWI"), naked));\r
99 \r
100 /* \r
101  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
102  * function here.\r
103  */\r
104 void vPortISRStartFirstTask( void );\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 void vPortISRStartFirstTask( void )\r
108 {\r
109         /* Simply start the scheduler.  This is included here as it can only be\r
110         called from ARM mode. */\r
111         portRESTORE_CONTEXT();\r
112 }\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Called by portYIELD() or taskYIELD() to manually force a context switch.\r
117  *\r
118  * When a context switch is performed from the task level the saved task \r
119  * context is made to look as if it occurred from within the tick ISR.  This\r
120  * way the same restore context function can be used when restoring the context\r
121  * saved from the ISR or that saved from a call to vPortYieldProcessor.\r
122  */\r
123 void vPortYieldProcessor( void )\r
124 {\r
125         /* Within an IRQ ISR the link register has an offset from the true return \r
126         address, but an SWI ISR does not.  Add the offset manually so the same \r
127         ISR return code can be used in both cases. */\r
128         asm volatile ( "ADD             LR, LR, #4" );\r
129 \r
130         /* Perform the context switch.  First save the context of the current task. */\r
131         portSAVE_CONTEXT();\r
132 \r
133         /* Find the highest priority task that is ready to run. */\r
134         vTaskSwitchContext();\r
135 \r
136         /* Restore the context of the new task. */\r
137         portRESTORE_CONTEXT();  \r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* \r
142  * The ISR used for the scheduler tick depends on whether the cooperative or\r
143  * the preemptive scheduler is being used.\r
144  */\r
145 \r
146 #if configUSE_PREEMPTION == 0\r
147 \r
148         /* The cooperative scheduler requires a normal IRQ service routine to \r
149         simply increment the system tick. */\r
150         void vNonPreemptiveTick( void ) __attribute__ ((interrupt ("IRQ")));\r
151         void vNonPreemptiveTick( void )\r
152         {               \r
153                 unsigned long ulDummy;\r
154                 \r
155                 /* Increment the tick count - which may wake some tasks but as the\r
156                 preemptive scheduler is not being used any woken task is not given\r
157                 processor time no matter what its priority. */\r
158                 vTaskIncrementTick();\r
159                 \r
160                 /* Clear the PIT interrupt. */\r
161                 ulDummy = AT91C_BASE_PITC->PITC_PIVR;\r
162                 \r
163                 /* End the interrupt in the AIC. */\r
164                 AT91C_BASE_AIC->AIC_EOICR = ulDummy;\r
165         }\r
166 \r
167 #else\r
168 \r
169         /* The preemptive scheduler is defined as "naked" as the full context is\r
170         saved on entry as part of the context switch. */\r
171         void vPreemptiveTick( void ) __attribute__((naked));\r
172         void vPreemptiveTick( void )\r
173         {\r
174                 /* Save the context of the current task. */\r
175                 portSAVE_CONTEXT();                     \r
176 \r
177                 /* Increment the tick count - this may wake a task. */\r
178                 vTaskIncrementTick();\r
179 \r
180                 /* Find the highest priority task that is ready to run. */\r
181                 vTaskSwitchContext();\r
182                 \r
183                 /* End the interrupt in the AIC. */\r
184                 AT91C_BASE_AIC->AIC_EOICR = AT91C_BASE_PITC->PITC_PIVR;;\r
185                 \r
186                 portRESTORE_CONTEXT();\r
187         }\r
188 \r
189 #endif\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 /*\r
193  * The interrupt management utilities can only be called from ARM mode.  When\r
194  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
195  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
196  * the utilities are defined as macros in portmacro.h - as per other ports.\r
197  */\r
198 void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
199 void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
200 \r
201 void vPortDisableInterruptsFromThumb( void )\r
202 {\r
203         asm volatile ( \r
204                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
205                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
206                 "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
207                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
208                 "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
209                 "BX             R14" );                                 /* Return back to thumb.                                        */\r
210 }\r
211                 \r
212 void vPortEnableInterruptsFromThumb( void )\r
213 {\r
214         asm volatile ( \r
215                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
216                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
217                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
218                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
219                 "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
220                 "BX             R14" );                                 /* Return back to thumb.                                        */\r
221 }\r
222 \r
223 \r
224 /* The code generated by the GCC compiler uses the stack in different ways at\r
225 different optimisation levels.  The interrupt flags can therefore not always\r
226 be saved to the stack.  Instead the critical section nesting level is stored\r
227 in a variable, which is then saved as part of the stack context. */\r
228 void vPortEnterCritical( void )\r
229 {\r
230         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
231         asm volatile ( \r
232                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
233                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
234                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
235                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
236                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
237 \r
238         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
239         directly.  Increment ulCriticalNesting to keep a count of how many times\r
240         portENTER_CRITICAL() has been called. */\r
241         ulCriticalNesting++;\r
242 }\r
243 \r
244 void vPortExitCritical( void )\r
245 {\r
246         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
247         {\r
248                 /* Decrement the nesting count as we are leaving a critical section. */\r
249                 ulCriticalNesting--;\r
250 \r
251                 /* If the nesting level has reached zero then interrupts should be\r
252                 re-enabled. */\r
253                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
254                 {\r
255                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
256                         asm volatile ( \r
257                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
258                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
259                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
260                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
261                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
262                 }\r
263         }\r
264 }\r
265 \r