]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/STR75x/portISR.c
a9c20d2f503e5878000958d76d231e7a79298638
[freertos] / Source / portable / GCC / STR75x / portISR.c
1 /*\r
2         FreeRTOS.org V4.1.2 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 \r
34 /*-----------------------------------------------------------\r
35  * Components that can be compiled to either ARM or THUMB mode are\r
36  * contained in port.c  The ISR routines, which can only be compiled\r
37  * to ARM mode, are contained in this file.\r
38  *----------------------------------------------------------*/\r
39 \r
40 /*\r
41 */\r
42 \r
43 \r
44 /* Scheduler includes. */\r
45 #include "FreeRTOS.h"\r
46 #include "task.h"\r
47 \r
48 /* Constants required to handle interrupts. */\r
49 #define portTIMER_MATCH_ISR_BIT         ( ( unsigned portCHAR ) 0x01 )\r
50 #define portCLEAR_VIC_INTERRUPT         ( ( unsigned portLONG ) 0 )\r
51 \r
52 /* Constants required to handle critical sections. */\r
53 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )\r
54 \r
55 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /* \r
60  * The scheduler can only be started from ARM mode, hence the inclusion of this\r
61  * function here.\r
62  */\r
63 void vPortISRStartFirstTask( void );\r
64 /*-----------------------------------------------------------*/\r
65 \r
66 void vPortISRStartFirstTask( void )\r
67 {\r
68         /* Simply start the scheduler.  This is included here as it can only be\r
69         called from ARM mode. */\r
70         asm volatile (                                                                                                          \\r
71         "LDR            R0, =pxCurrentTCB                                                               \n\t"   \\r
72         "LDR            R0, [R0]                                                                                \n\t"   \\r
73         "LDR            LR, [R0]                                                                                \n\t"   \\r
74                                                                                                                                                 \\r
75         /* The critical nesting depth is the first item on the stack. */        \\r
76         /* Load it into the ulCriticalNesting variable. */                                      \\r
77         "LDR            R0, =ulCriticalNesting                                                  \n\t"   \\r
78         "LDMFD  LR!, {R1}                                                                                       \n\t"   \\r
79         "STR            R1, [R0]                                                                                \n\t"   \\r
80                                                                                                                                                 \\r
81         /* Get the SPSR from the stack. */                                                                      \\r
82         "LDMFD  LR!, {R0}                                                                                       \n\t"   \\r
83         "MSR            SPSR, R0                                                                                \n\t"   \\r
84                                                                                                                                                 \\r
85         /* Restore all system mode registers for the task. */                           \\r
86         "LDMFD  LR, {R0-R14}^                                                                           \n\t"   \\r
87         "NOP                                                                                                            \n\t"   \\r
88                                                                                                                                                 \\r
89         /* Restore the return address. */                                                                       \\r
90         "LDR            LR, [LR, #+60]                                                                  \n\t"   \\r
91                                                                                                                                                 \\r
92         /* And return - correcting the offset in the LR to obtain the */        \\r
93         /* correct address. */                                                                                          \\r
94         "SUBS PC, LR, #4 \n\t" \\r
95         );                                                                                                                                      \\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* The preemptive scheduler is defined as "naked" as the full context is\r
100 saved on entry as part of the context switch. */\r
101 void vPortTickISR( void )\r
102 {\r
103         /* Increment the RTOS tick count, then look for the highest priority \r
104         task that is ready to run. */\r
105         vTaskIncrementTick();\r
106         \r
107         #if configUSE_PREEMPTION == 1\r
108                 vTaskSwitchContext();\r
109         #endif\r
110                         \r
111         /* Ready for the next interrupt. */\r
112         TB_ClearITPendingBit( TB_IT_Update );   \r
113 }\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * The interrupt management utilities can only be called from ARM mode.  When\r
119  * THUMB_INTERWORK is defined the utilities are defined as functions here to\r
120  * ensure a switch to ARM mode.  When THUMB_INTERWORK is not defined then\r
121  * the utilities are defined as macros in portmacro.h - as per other ports.\r
122  */\r
123 #ifdef THUMB_INTERWORK\r
124 \r
125         void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));\r
126         void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));\r
127 \r
128         void vPortDisableInterruptsFromThumb( void )\r
129         {\r
130                 asm volatile ( \r
131                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */\r
132                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */\r
133                         "ORR    R0, R0, #0xC0   \n\t"   /* Disable IRQ, FIQ.                                            */\r
134                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */\r
135                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
136                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
137         }\r
138                         \r
139         void vPortEnableInterruptsFromThumb( void )\r
140         {\r
141                 asm volatile ( \r
142                         "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                                                     */      \r
143                         "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                                            */      \r
144                         "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                                                     */      \r
145                         "MSR    CPSR, R0                \n\t"   /* Write back modified value.                           */      \r
146                         "LDMIA  SP!, {R0}               \n\t"   /* Pop R0.                                                                      */\r
147                         "BX             R14" );                                 /* Return back to thumb.                                        */\r
148         }\r
149 \r
150 #endif /* THUMB_INTERWORK */\r
151 \r
152 /* The code generated by the GCC compiler uses the stack in different ways at\r
153 different optimisation levels.  The interrupt flags can therefore not always\r
154 be saved to the stack.  Instead the critical section nesting level is stored\r
155 in a variable, which is then saved as part of the stack context. */\r
156 void vPortEnterCritical( void )\r
157 {\r
158         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
159         asm volatile ( \r
160                 "STMDB  SP!, {R0}                       \n\t"   /* Push R0.                                                             */\r
161                 "MRS    R0, CPSR                        \n\t"   /* Get CPSR.                                                    */\r
162                 "ORR    R0, R0, #0xC0           \n\t"   /* Disable IRQ, FIQ.                                    */\r
163                 "MSR    CPSR, R0                        \n\t"   /* Write back modified value.                   */\r
164                 "LDMIA  SP!, {R0}" );                           /* Pop R0.                                                              */\r
165 \r
166         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
167         directly.  Increment ulCriticalNesting to keep a count of how many times\r
168         portENTER_CRITICAL() has been called. */\r
169         ulCriticalNesting++;\r
170 }\r
171 \r
172 void vPortExitCritical( void )\r
173 {\r
174         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
175         {\r
176                 /* Decrement the nesting count as we are leaving a critical section. */\r
177                 ulCriticalNesting--;\r
178 \r
179                 /* If the nesting level has reached zero then interrupts should be\r
180                 re-enabled. */\r
181                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
182                 {\r
183                         /* Enable interrupts as per portEXIT_CRITICAL().                                        */\r
184                         asm volatile ( \r
185                                 "STMDB  SP!, {R0}               \n\t"   /* Push R0.                                             */      \r
186                                 "MRS    R0, CPSR                \n\t"   /* Get CPSR.                                    */      \r
187                                 "BIC    R0, R0, #0xC0   \n\t"   /* Enable IRQ, FIQ.                             */      \r
188                                 "MSR    CPSR, R0                \n\t"   /* Write back modified value.   */      \r
189                                 "LDMIA  SP!, {R0}" );                   /* Pop R0.                                              */\r
190                 }\r
191         }\r
192 }\r
193 \r
194 \r
195 \r
196 \r
197 \r