4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2004 Giovanni Bajo
34 * \brief CPU-specific IRQ definitions.
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>
47 #include <kern/proc.h> /* proc_needPreempt() / proc_preempt() */
49 #include <cfg/compiler.h> /* for uintXX_t */
50 #include "cfg/cfg_proc.h" /* CONFIG_KERN_PREEMPT */
53 #define IRQ_DISABLE disable_interrupt()
54 #define IRQ_ENABLE enable_interrupt()
57 /* Get IRQ_* definitions from the hosting environment. */
60 #define IRQ_DISABLE FIXME
61 #define IRQ_ENABLE FIXME
62 #define IRQ_SAVE_DISABLE(x) FIXME
63 #define IRQ_RESTORE(x) FIXME
64 #endif /* OS_EMBEDDED */
72 * NOTE: 0 means that an interrupt is not affected by the global IRQ
76 #define IRQ_PRIO_MIN 0xf0
77 #define IRQ_PRIO_MAX 0
79 * To disable interrupts we just raise the system base priority to a
80 * number lower than the default IRQ priority. In this way, all the
81 * "normal" interrupt can't be triggered. High-priority interrupt can
82 * still happen (at the moment only the soft-interrupt svcall uses a
83 * priority greater than the default IRQ priority).
85 * To enable interrupts we set the system base priority to 0, that
86 * means IRQ priority mechanism is disabled, and any interrupt can
89 #define IRQ_PRIO_DISABLED 0x40
90 #define IRQ_PRIO_ENABLED 0
94 register cpu_flags_t reg = IRQ_PRIO_DISABLED; \
97 : : "r"(reg) : "memory", "cc"); \
102 register cpu_flags_t reg = IRQ_PRIO_ENABLED; \
105 : : "r"(reg) : "memory", "cc"); \
108 #define CPU_READ_FLAGS() \
110 register cpu_flags_t reg; \
113 : "=r"(reg) : : "memory", "cc"); \
117 #define IRQ_SAVE_DISABLE(x) \
119 x = CPU_READ_FLAGS(); \
123 #define IRQ_RESTORE(x) \
127 : : "r"(x) : "memory", "cc"); \
130 #define IRQ_ENABLED() (CPU_READ_FLAGS() == IRQ_PRIO_ENABLED)
132 INLINE bool irq_running(void)
134 register uint32_t ret;
137 * Check if the current stack pointer is the main stack or
138 * process stack: we use the main stack only in Handler mode,
139 * so this means we're running inside an ISR.
146 "moveq %0, #1\n\t" : "=r"(ret) : : "cc");
149 #define IRQ_RUNNING() irq_running()
151 #if CONFIG_KERN_PREEMPT
153 #define DECLARE_ISR_CONTEXT_SWITCH(func) \
155 INLINE void __isr_##func(void); \
159 if (!proc_needPreempt()) \
162 * Set a PendSV request.
164 * The preemption handler will be called immediately
165 * after this ISR in tail-chaining mode (without the
166 * overhead of hardware state saving and restoration
167 * between interrupts).
169 HWREG(NVIC_INT_CTRL) = NVIC_INT_CTRL_PEND_SV; \
171 INLINE void __isr_##func(void)
174 * With task priorities enabled each ISR is used a point to
175 * check if we need to perform a context switch.
177 * Instead, without priorities a context switch can occur only
178 * when the running task expires its time quantum. In this last
179 * case, the context switch can only occur in the timer ISR,
180 * that must be always declared with the
181 * DECLARE_ISR_CONTEXT_SWITCH() macro.
184 #define DECLARE_ISR(func) \
185 DECLARE_ISR_CONTEXT_SWITCH(func)
187 * Interrupt service routine prototype: can be used for
188 * forward declarations.
190 #define ISR_PROTO(func) \
191 ISR_PROTO_CONTEXT_SWITCH(func)
192 #endif /* !CONFIG_KERN_PRI */
196 #define ISR_PROTO(func) void func(void)
199 #define DECLARE_ISR(func) void func(void)
201 #ifndef DECLARE_ISR_CONTEXT_SWITCH
202 #define DECLARE_ISR_CONTEXT_SWITCH(func) void func(void)
204 #ifndef ISR_PROTO_CONTEXT_SWITCH
205 #define ISR_PROTO_CONTEXT_SWITCH(func) void func(void)
210 #ifdef __IAR_SYSTEMS_ICC__
214 #if __CPU_MODE__ == 1 /* Thumb */
216 extern cpu_flags_t get_CPSR(void);
217 extern void set_CPSR(cpu_flags_t flags);
219 #define get_CPSR __get_CPSR
220 #define set_CPSR __set_CPSR
223 #define IRQ_DISABLE __disable_interrupt()
224 #define IRQ_ENABLE __enable_interrupt()
226 #define IRQ_SAVE_DISABLE(x) \
229 __disable_interrupt(); \
232 #define IRQ_RESTORE(x) \
237 #define IRQ_ENABLED() \
238 ((bool)(get_CPSR() & 0xb0))
240 #else /* !__IAR_SYSTEMS_ICC__ */
242 #define IRQ_DISABLE \
246 "orr r0, r0, #0xc0\n\t" \
256 "bic r0, r0, #0xc0\n\t" \
262 #define IRQ_SAVE_DISABLE(x) \
266 "orr r0, %0, #0xc0\n\t" \
274 #define IRQ_RESTORE(x) \
283 #define CPU_READ_FLAGS() \
294 #define IRQ_ENABLED() ((CPU_READ_FLAGS() & 0xc0) != 0xc0)
296 #if CONFIG_KERN_PREEMPT
297 EXTERN_C void asm_irq_switch_context(void);
300 * At the beginning of any ISR immediately ajust the
301 * return address and store all the caller-save
302 * registers (the ISR may change these registers that
303 * are shared with the user-context).
305 #define IRQ_ENTRY() asm volatile ( \
306 "sub lr, lr, #4\n\t" \
307 "stmfd sp!, {r0-r3, ip, lr}\n\t")
308 #define IRQ_EXIT() asm volatile ( \
309 "b asm_irq_switch_context\n\t")
311 * Function attribute to declare an interrupt service
314 * An ISR function must be declared as naked because we
315 * want to add our IRQ_ENTRY() prologue and IRQ_EXIT()
316 * epilogue code to handle the context switch and save
317 * all the registers (not only the callee-save).
320 #define ISR_FUNC __attribute__((naked))
323 * The compiler cannot establish which
324 * registers actually need to be saved, because
325 * the interrupt can happen at any time, so the
326 * "normal" prologue and epilogue used for a
327 * generic function call are not suitable for
330 * Using a naked function has the drawback that
331 * the stack is not automatically adjusted at
332 * this point, like a "normal" function call.
334 * So, an ISR can _only_ contain other function
335 * calls and they can't use the stack in any
338 * NOTE: we need to explicitly disable IRQs after
339 * IRQ_ENTRY(), because the IRQ status flag is not
340 * masked by the hardware and an IRQ ack inside the ISR
341 * may cause the triggering of another IRQ before
342 * exiting from the current ISR.
344 * The respective IRQ_ENABLE is not necessary, because
345 * IRQs will be automatically re-enabled when restoring
346 * the context of the user task.
348 #define DECLARE_ISR_CONTEXT_SWITCH(func) \
349 void ISR_FUNC func(void); \
350 static void __isr_##func(void); \
351 void ISR_FUNC func(void) \
358 static void __isr_##func(void)
360 * Interrupt service routine prototype: can be used for
361 * forward declarations.
363 #define ISR_PROTO_CONTEXT_SWITCH(func) \
364 void ISR_FUNC func(void)
366 * With task priorities enabled each ISR is used a point to
367 * check if we need to perform a context switch.
369 * Instead, without priorities a context switch can occur only
370 * when the running task expires its time quantum. In this last
371 * case, the context switch can only occur in the timer
372 * ISR, that must be always declared with the
373 * DECLARE_ISR_CONTEXT_SWITCH() macro.
376 #define DECLARE_ISR(func) \
377 DECLARE_ISR_CONTEXT_SWITCH(func)
379 #define ISR_PROTO(func) \
380 ISR_PROTO_CONTEXT_SWITCH(func)
381 #endif /* !CONFIG_KERN_PRI */
382 #endif /* CONFIG_KERN_PREEMPT */
385 #define DECLARE_ISR(func) \
386 void __attribute__((interrupt)) func(void)
388 #ifndef DECLARE_ISR_CONTEXT_SWITCH
389 #define DECLARE_ISR_CONTEXT_SWITCH(func) \
390 void __attribute__((interrupt)) func(void)
393 #define ISR_PROTO(func) \
394 void __attribute__((interrupt)) func(void)
396 #ifndef ISR_PROTO_CONTEXT_SWITCH
397 #define ISR_PROTO_CONTEXT_SWITCH(func) \
398 void __attribute__((interrupt)) func(void)
401 #endif /* !__IAR_SYSTEMS_ICC_ */
405 /* Get IRQ_* definitions from the hosting environment. */
408 #define IRQ_DISABLE FIXME
409 #define IRQ_ENABLE FIXME
410 #define IRQ_SAVE_DISABLE(x) FIXME
411 #define IRQ_RESTORE(x) FIXME
412 #define IRQ_ENABLED() FIXME
413 #endif /* OS_EMBEDDED */
417 #define IRQ_DISABLE do { asm(bfset #0x0200,SR); asm(nop); } while (0)
418 #define IRQ_ENABLE do { asm(bfclr #0x0200,SR); asm(nop); } while (0)
420 #define IRQ_SAVE_DISABLE(x) \
421 do { (void)x; asm(move SR,x); asm(bfset #0x0200,SR); } while (0)
422 #define IRQ_RESTORE(x) \
423 do { (void)x; asm(move x,SR); } while (0)
425 static inline bool irq_running(void)
427 extern void *user_sp;
430 #define IRQ_RUNNING() irq_running()
432 static inline bool irq_enabled(void)
436 return !(x & 0x0200);
438 #define IRQ_ENABLED() irq_enabled()
442 #define IRQ_DISABLE asm volatile ("cli" ::)
443 #define IRQ_ENABLE asm volatile ("sei" ::)
445 #define IRQ_SAVE_DISABLE(x) \
447 __asm__ __volatile__( \
448 "in %0,__SREG__\n\t" \
450 : "=r" (x) : /* no inputs */ : "cc" \
454 #define IRQ_RESTORE(x) \
456 __asm__ __volatile__( \
457 "out __SREG__,%0" : /* no outputs */ : "r" (x) : "cc" \
461 #define IRQ_ENABLED() \
464 __asm__ __volatile__( \
465 "in %0,__SREG__\n\t" \
466 : "=r" (sreg) /* no inputs & no clobbers */ \
468 (bool)(sreg & 0x80); \
470 #if CONFIG_KERN_PREEMPT
471 #define DECLARE_ISR_CONTEXT_SWITCH(vect) \
472 INLINE void __isr_##vect(void); \
476 IRQ_PREEMPT_HANDLER(); \
478 INLINE void __isr_##vect(void)
481 * With task priorities enabled each ISR is used a point to
482 * check if we need to perform a context switch.
484 * Instead, without priorities a context switch can occur only
485 * when the running task expires its time quantum. In this last
486 * case, the context switch can only occur in the timer ISR,
487 * that must be always declared with the
488 * DECLARE_ISR_CONTEXT_SWITCH() macro.
491 #define DECLARE_ISR(func) \
492 DECLARE_ISR_CONTEXT_SWITCH(func)
494 * Interrupt service routine prototype: can be used for
495 * forward declarations.
497 #define ISR_PROTO(func) \
498 ISR_PROTO_CONTEXT_SWITCH(func)
499 #endif /* !CONFIG_KERN_PRI */
503 #define ISR_PROTO(vect) ISR(vect)
506 #define DECLARE_ISR(vect) ISR(vect)
508 #ifndef DECLARE_ISR_CONTEXT_SWITCH
509 #define DECLARE_ISR_CONTEXT_SWITCH(vect) ISR(vect)
511 #ifndef ISR_PROTO_CONTEXT_SWITCH
512 #define ISR_PROTO_CONTEXT_SWITCH(vect) ISR(vect)
516 #error No CPU_... defined.
520 /// Ensure callee is running within an interrupt
521 #define ASSERT_IRQ_CONTEXT() ASSERT(IRQ_RUNNING())
523 /// Ensure callee is not running within an interrupt
524 #define ASSERT_USER_CONTEXT() ASSERT(!IRQ_RUNNING())
526 #define IRQ_RUNNING() false
527 #define ASSERT_USER_CONTEXT() do {} while(0)
528 #define ASSERT_IRQ_CONTEXT() do {} while(0)
532 /// Ensure interrupts are enabled
533 #define IRQ_ASSERT_ENABLED() ASSERT(IRQ_ENABLED())
535 /// Ensure interrupts are not enabled
536 #define IRQ_ASSERT_DISABLED() ASSERT(!IRQ_ENABLED())
538 #define IRQ_ASSERT_ENABLED() do {} while(0)
539 #define IRQ_ASSERT_DISABLED() do {} while(0)
543 #ifndef IRQ_PREEMPT_HANDLER
544 #if CONFIG_KERN_PREEMPT
546 * Handle preemptive context switch inside timer IRQ.
548 INLINE void IRQ_PREEMPT_HANDLER(void)
550 if (proc_needPreempt())
554 #define IRQ_PREEMPT_HANDLER() /* Nothing */
559 * Execute \a CODE atomically with respect to interrupts.
561 * \see IRQ_SAVE_DISABLE IRQ_RESTORE
563 #define ATOMIC(CODE) \
565 cpu_flags_t __flags; \
566 IRQ_SAVE_DISABLE(__flags); \
568 IRQ_RESTORE(__flags); \
571 #endif /* CPU_IRQ_H */