1 /**
2  * \file
3  *
4  * \brief Global interrupt management for 8-bit AVR
5  *
6  * Copyright (C) 2010-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef UTILS_INTERRUPT_INTERRUPT_H
47 #define UTILS_INTERRUPT_INTERRUPT_H
48 
49 #include <compiler.h>
50 #include <parts.h>
51 
52 /**
53  * \weakgroup interrupt_group
54  *
55  * @{
56  */
57 
58 #ifdef ISR_CUSTOM_H
59 #  include ISR_CUSTOM_H
60 #else
61 
62 /**
63  * \def ISR
64  * \brief Define service routine for specified interrupt vector
65  *
66  * Usage:
67  * \code
68 	ISR(FOO_vect)
69 	{
70 	    ...
71 	}
72 \endcode
73  *
74  * \param vect Interrupt vector name as found in the device header files.
75  */
76 #if defined(__DOXYGEN__)
77 #  define ISR(vect)
78 #elif defined(__GNUC__)
79 #  include <avr/interrupt.h>
80 #elif defined(__ICCAVR__)
81 #  define __ISR(x) _Pragma(#x)
82 #  define ISR(vect) __ISR(vector=vect) __interrupt void handler_##vect(void)
83 #endif
84 #endif // ISR_CUSTOM_H
85 
86 #if XMEGA
87 /**
88  * \brief Initialize interrupt vectors
89  * Enables all interrupt levels, with vectors located in the application section
90  * and fixed priority scheduling.
91  */
92 #define irq_initialize_vectors() \
93 	PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
94 #elif MEGA_RF
95 #define irq_initialize_vectors()
96 #endif
97 
98 #ifdef __GNUC__
99 #  define cpu_irq_enable()     sei()
100 #  define cpu_irq_disable()    cli()
101 #else
102 #  define cpu_irq_enable()     __enable_interrupt()
103 #  define cpu_irq_disable()    __disable_interrupt()
104 #endif
105 
106 typedef uint8_t irqflags_t;
107 
cpu_irq_save(void)108 static inline irqflags_t cpu_irq_save(void)
109 {
110 	volatile irqflags_t flags = SREG;
111 	cpu_irq_disable();
112 	return flags;
113 }
114 
cpu_irq_restore(irqflags_t flags)115 static inline void cpu_irq_restore(irqflags_t flags)
116 {
117 	barrier();
118 	SREG = flags;
119 }
120 
cpu_irq_is_enabled_flags(irqflags_t flags)121 static inline bool cpu_irq_is_enabled_flags(irqflags_t flags)
122 {
123 #if XMEGA
124 #  ifdef __GNUC__
125 	return flags & CPU_I_bm;
126 #  else
127 	return flags & I_bm;
128 #  endif
129 #elif MEGA || TINY
130 	return flags & (1 << SREG_I);
131 #endif
132 }
133 
134 #define cpu_irq_is_enabled()             cpu_irq_is_enabled_flags(SREG)
135 
136 //! @}
137 
138 /**
139  * \weakgroup interrupt_deprecated_group
140  * @{
141  */
142 // Deprecated definitions.
143 #define Enable_global_interrupt()        cpu_irq_enable()
144 #define Disable_global_interrupt()       cpu_irq_disable()
145 #define Is_global_interrupt_enabled()    cpu_irq_is_enabled()
146 //! @}
147 
148 #endif /* UTILS_INTERRUPT_INTERRUPT_H */
149