011524b8f098bd874ab38d967aea34f6bde31426
[bertos.git] / bertos / cpu / irq.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief CPU-specific IRQ definitions.
35  *
36  * \author Giovanni Bajo <rasky@develer.com>
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41 #ifndef CPU_IRQ_H
42 #define CPU_IRQ_H
43
44 #include "detect.h"
45 #include "types.h"
46
47 #include <cfg/compiler.h> /* for uintXX_t */
48
49 #if CPU_I196
50         #define IRQ_DISABLE             disable_interrupt()
51         #define IRQ_ENABLE              enable_interrupt()
52 #elif CPU_X86
53
54         /* Get IRQ_* definitions from the hosting environment. */
55         #include <cfg/os.h>
56         #if OS_EMBEDDED
57                 #define IRQ_DISABLE             FIXME
58                 #define IRQ_ENABLE              FIXME
59                 #define IRQ_SAVE_DISABLE(x)     FIXME
60                 #define IRQ_RESTORE(x)          FIXME
61         #endif /* OS_EMBEDDED */
62
63
64 #elif CPU_ARM
65
66         #ifdef __IAR_SYSTEMS_ICC__
67
68                 #include <inarm.h>
69
70                 #if __CPU_MODE__ == 1 /* Thumb */
71                         /* Use stubs */
72                         extern cpu_flags_t get_CPSR(void);
73                         extern void set_CPSR(cpu_flags_t flags);
74                 #else
75                         #define get_CPSR __get_CPSR
76                         #define set_CPSR __set_CPSR
77                 #endif
78
79                 #define IRQ_DISABLE __disable_interrupt()
80                 #define IRQ_ENABLE  __enable_interrupt()
81
82                 #define IRQ_SAVE_DISABLE(x) \
83                 do { \
84                         (x) = get_CPSR(); \
85                         __disable_interrupt(); \
86                 } while (0)
87
88                 #define IRQ_RESTORE(x) \
89                 do { \
90                         set_CPSR(x); \
91                 } while (0)
92
93                 #define IRQ_ENABLED() \
94                         ((bool)(get_CPSR() & 0xb0))
95
96         #else /* !__IAR_SYSTEMS_ICC__ */
97
98                 #define IRQ_DISABLE \
99                 do { \
100                         asm volatile ( \
101                                 "mrs r0, cpsr\n\t" \
102                                 "orr r0, r0, #0xc0\n\t" \
103                                 "msr cpsr_c, r0" \
104                                 ::: "r0" \
105                         ); \
106                 } while (0)
107
108                 #define IRQ_ENABLE \
109                 do { \
110                         asm volatile ( \
111                                 "mrs r0, cpsr\n\t" \
112                                 "bic r0, r0, #0xc0\n\t" \
113                                 "msr cpsr_c, r0" \
114                                 ::: "r0" \
115                         ); \
116                 } while (0)
117
118                 #define IRQ_SAVE_DISABLE(x) \
119                 do { \
120                         asm volatile ( \
121                                 "mrs %0, cpsr\n\t" \
122                                 "orr r0, %0, #0xc0\n\t" \
123                                 "msr cpsr_c, r0" \
124                                 : "=r" (x) \
125                                 : /* no inputs */ \
126                                 : "r0" \
127                         ); \
128                 } while (0)
129
130                 #define IRQ_RESTORE(x) \
131                 do { \
132                         asm volatile ( \
133                                 "msr cpsr_c, %0" \
134                                 : /* no outputs */ \
135                                 : "r" (x) \
136                         ); \
137                 } while (0)
138
139                 #define CPU_READ_FLAGS() \
140                 ({ \
141                         cpu_flags_t sreg; \
142                         asm volatile ( \
143                                 "mrs %0, cpsr\n\t" \
144                                 : "=r" (sreg) \
145                                 : /* no inputs */ \
146                         ); \
147                         sreg; \
148                 })
149
150                 #define IRQ_ENABLED() ((CPU_READ_FLAGS() & 0xc0) != 0xc0)
151
152         #endif /* !__IAR_SYSTEMS_ICC_ */
153
154 #elif CPU_PPC
155
156         /* Get IRQ_* definitions from the hosting environment. */
157         #include <cfg/os.h>
158         #if OS_EMBEDDED
159                 #define IRQ_DISABLE         FIXME
160                 #define IRQ_ENABLE          FIXME
161                 #define IRQ_SAVE_DISABLE(x) FIXME
162                 #define IRQ_RESTORE(x)      FIXME
163                 #define IRQ_ENABLED()       FIXME
164         #endif /* OS_EMBEDDED */
165
166 #elif CPU_DSP56K
167
168         #define IRQ_DISABLE             do { asm(bfset #0x0200,SR); asm(nop); } while (0)
169         #define IRQ_ENABLE              do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
170
171         #define IRQ_SAVE_DISABLE(x)  \
172                 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
173         #define IRQ_RESTORE(x)  \
174                 do { (void)x; asm(move x,SR); } while (0)
175
176         static inline bool irq_running(void)
177         {
178                 extern void *user_sp;
179                 return !!user_sp;
180         }
181         #define IRQ_RUNNING() irq_running()
182
183         static inline bool irq_enabled(void)
184         {
185                 uint16_t x;
186                 asm(move SR,x);
187                 return !(x & 0x0200);
188         }
189         #define IRQ_ENABLED() irq_enabled()
190
191 #elif CPU_AVR
192
193         #define IRQ_DISABLE   asm volatile ("cli" ::)
194         #define IRQ_ENABLE    asm volatile ("sei" ::)
195
196         #define IRQ_SAVE_DISABLE(x) \
197         do { \
198                 __asm__ __volatile__( \
199                         "in %0,__SREG__\n\t" \
200                         "cli" \
201                         : "=r" (x) : /* no inputs */ : "cc" \
202                 ); \
203         } while (0)
204
205         #define IRQ_RESTORE(x) \
206         do { \
207                 __asm__ __volatile__( \
208                         "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
209                 ); \
210         } while (0)
211
212         #define IRQ_ENABLED() \
213         ({ \
214                 uint8_t sreg; \
215                 __asm__ __volatile__( \
216                         "in %0,__SREG__\n\t" \
217                         : "=r" (sreg)  /* no inputs & no clobbers */ \
218                 ); \
219                 (bool)(sreg & 0x80); \
220         })
221 #else
222         #error No CPU_... defined.
223 #endif
224
225 #ifndef IRQ_ENTRY
226         #define IRQ_ENTRY() /* NOP */
227 #endif
228
229 #ifndef IRQ_EXIT
230         #define IRQ_EXIT() /* NOP */
231 #endif
232
233 #ifdef IRQ_RUNNING
234         /// Ensure callee is running within an interrupt
235         #define ASSERT_IRQ_CONTEXT()  ASSERT(IRQ_RUNNING())
236
237         /// Ensure callee is not running within an interrupt
238         #define ASSERT_USER_CONTEXT() ASSERT(!IRQ_RUNNING())
239 #else
240         #define ASSERT_USER_CONTEXT()  do {} while(0)
241         #define ASSERT_IRQ_CONTEXT()   do {} while(0)
242 #endif
243
244 #ifdef IRQ_ENABLED
245         /// Ensure interrupts are enabled
246         #define IRQ_ASSERT_ENABLED()  ASSERT(IRQ_ENABLED())
247
248         /// Ensure interrupts are not enabled
249         #define IRQ_ASSERT_DISABLED() ASSERT(!IRQ_ENABLED())
250 #else
251         #define IRQ_ASSERT_ENABLED() do {} while(0)
252         #define IRQ_ASSERT_DISABLED() do {} while(0)
253 #endif
254
255 /**
256  * Execute \a CODE atomically with respect to interrupts.
257  *
258  * \see IRQ_SAVE_DISABLE IRQ_RESTORE
259  */
260 #define ATOMIC(CODE) \
261         do { \
262                 cpu_flags_t __flags; \
263                 IRQ_SAVE_DISABLE(__flags); \
264                 CODE; \
265                 IRQ_RESTORE(__flags); \
266         } while (0)
267
268 #endif /* CPU_IRQ_H */