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