Xmega IEC60730 Class B Library  1.0
 All Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
avr_compiler.h
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
2 /**
3  * \file
4  *
5  * \brief
6  * Header file for compiler compatibility
7  *
8  * This file contains some general definitions and macros to ensure code
9  * compatibility with both IAR and GCC.
10  *
11  * \par Application note:
12  * AVR1610: Guide to IEC60730 Class B compliance with XMEGA
13  *
14  * \par Documentation
15  * For comprehensive code documentation, supported compilers, compiler
16  * settings and supported devices see readme.html
17  *
18  * \author
19  * Atmel Corporation: http://www.atmel.com \n
20  * Support email: avr@atmel.com
21  *
22  * Copyright (C) 2012 Atmel Corporation. All rights reserved.
23  *
24  * \page License
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions are met:
28  *
29  * 1. Redistributions of source code must retain the above copyright notice,
30  * this list of conditions and the following disclaimer.
31  *
32  * 2. Redistributions in binary form must reproduce the above copyright notice,
33  * this list of conditions and the following disclaimer in the documentation
34  * and/or other materials provided with the distribution.
35  *
36  * 3. The name of Atmel may not be used to endorse or promote products derived
37  * from this software without specific prior written permission.
38  *
39  * 4. This software may only be redistributed and used in connection with an
40  * Atmel AVR product.
41  *
42  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
43  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
44  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
45  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
46  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
52  * DAMAGE.
53  */
54 
55 
56 
57 #ifndef COMPILER_AVR_H
58 #define COMPILER_AVR_H
59 
60 #include <stdint.h>
61 #include <stdbool.h>
62 #include <stdlib.h>
63 
64 #ifndef F_CPU
65 # define F_CPU 2000000
66 #endif
67 
68 #define STRINGIZE_AUX(X) #X
69 #define STRINGIZE(X) STRINGIZE_AUX(X)
70 #define CONCAT(X,Y,Z) X##Y##Z
71 #define LABEL(X,Y,Z) CONCAT(X,Y,Z)
72 
73 
74 
75 
76 /*! \brief Enable memory mapping of EEPROM, if it is not already enabled.
77  *
78  * \note Use this in conjunction with CLASSB_EEMAP_END() to ensure that the
79  * prior configuration of memory-mapping is preserved.
80  */
81 #define CLASSB_EEMAP_BEGIN() \
82  bool eemapEnabled; \
83  /* Check if memory mapping of EEPROM is already enabled. */ \
84  eemapEnabled = (NVM.CTRLB & NVM_EEMAPEN_bm) ? true : false; \
85 \
86  if (!eemapEnabled) { \
87  /* Ensure that NVM is ready before enabling memory mapping. */ \
88  do {} while (NVM.STATUS & NVM_NVMBUSY_bm); \
89  NVM.CTRLB |= NVM_EEMAPEN_bm; \
90  }
91 
92 /*! \brief Disable memory mapping of EEPROM, unless it was previously enabled.
93  *
94  * \note Use this in conjunction with CLASSB_EEMAP_BEGIN().
95  */
96 #define CLASSB_EEMAP_END() \
97  if (!eemapEnabled) { \
98  NVM.CTRLB &= ~NVM_EEMAPEN_bm; \
99  }
100 
101 
102 /** This macro will protect the following code from interrupts.*/
103 #define ENTER_CRITICAL_REGION() uint8_t volatile saved_sreg = SREG; \
104  cli();
105 #define REENTER_CRITICAL_REGION() saved_sreg = SREG; \
106  cli();
107 
108 /** This macro must always be used in conjunction with ENTER_CRITICAL_REGION
109  so that interrupts are enabled again.*/
110 #define LEAVE_CRITICAL_REGION() SREG = saved_sreg;
111 
112 
113 
114 #if defined(__ICCAVR__)
115 
116 
117 
118 #include <inavr.h>
119 #include <ioavr.h>
120 #include <intrinsics.h>
121 #include <pgmspace.h>
122 
123 #ifndef __HAS_ELPM__
124 #define _MEMATTR __flash
125 #else /* __HAS_ELPM__ */
126 #define _MEMATTR __farflash
127 #endif /* __HAS_ELPM__ */
128 
129 #define cpu_sleep() __sleep()
130 
131 #define delay_us( us ) __delay_cycles((F_CPU / 1000000UL) * (us))
132 
133 /**
134  * Some preprocessor magic to allow for a header file abstraction of
135  * interrupt service routine declarations for the IAR compiler. This
136  * requires the use of the C99 _Pragma() directive (rather than the
137  * old #pragma one that could not be used as a macro replacement), as
138  * well as two different levels of preprocessor concetanations in
139  * order to do both, assign the correct interrupt vector name, as well
140  * as construct a unique function name for the ISR.
141  *
142  * Do *NOT* try to reorder the macros below, or you'll suddenly find
143  * out about all kinds of IAR bugs...
144  */
145 #define PRAGMA(x) _Pragma(#x)
146 #define ISR(vec) PRAGMA(vector=vec) __interrupt void handler_##vec(void)
147 #define sei() (__enable_interrupt( ))
148 #define cli() (__disable_interrupt( ))
149 
150 #define nop() (__no_operation())
151 
152 #define INLINE PRAGMA(inline=forced) static
153 
154 #define ASSEMBLY(x) __asm(x)
155 
156 #define PROGMEM_LOCATION(var, loc) const _MEMATTR var @ loc
157 #define PROGMEM_DECLARE(x) _MEMATTR x
158 #define PROGMEM_STRING(x) ((_MEMATTR const char *)(x))
159 #define PROGMEM_STRING_T char const _MEMATTR *
160 #define PROGMEM_T const _MEMATTR
161 #define PROGMEM_PTR_T const _MEMATTR *
162 #define PROGMEM_BYTE_ARRAY_T uint8_t const _MEMATTR *
163 #define PROGMEM_WORD_ARRAY_T uint16_t const _MEMATTR *
164 #define PROGMEM_READ_BYTE(x) *(x)
165 #define PROGMEM_READ_WORD(x) *(x)
166 #define PROGMEM_READ_DWORD(x) *(x)
167 #define PROGMEM_READ_BYTE_FAR(x) *((uint8_t __farflash*)x)
168 #define PROGMEM_READ_WORD_FAR(x) *((uint16_t __farflash*)x)
169 #define PROGMEM_READ_DWORD_FAR(x) *((uint32_t __farflash*)x)
170 
171 #define NO_INIT __no_init
172 
173 #define EEPROM_DECLARE(var) __eeprom var
174 #define EEGET(var, adr) __EEGET(var, adr)
175 #define EEPUT(adr, val) __EEPUT(adr, val)
176 
177 #define watchdog_reset() __watchdog_reset()
178 
179 #define SHORTENUM /**/
180 
181 
182 
183 #elif defined(__GNUC__)
184 
185 
186 
187 #include <avr/sleep.h>
188 #include <avr/io.h>
189 #include <avr/interrupt.h>
190 #include <avr/pgmspace.h>
191 //#include <avr/eeprom.h> // Not available for XMEGA.
192 #include <avr/wdt.h>
193 #include <util/delay.h>
194 
195 #define cpu_sleep() sleep_cpu()
196 
197 #define delay_us(us) (_delay_us( us ))
198 
199 #define INLINE static inline
200 
201 #define ASSEMBLY __asm__ __volatile__
202 
203 #define nop() do {__asm__ __volatile__ ("nop");} while (0)
204 
205 #define PROGMEM_LOCATION(var, loc) var __attribute__((section (#loc)))
206 #define PROGMEM_DECLARE(x) x __attribute__((__progmem__))
207 #define PROGMEM_STRING(x) PSTR(x)
208 #define PROGMEM_STRING_T PGM_P
209 #define PROGMEM_T
210 #define PROGMEM_PTR_T *
211 #define PROGMEM_BYTE_ARRAY_T uint8_t*
212 #define PROGMEM_WORD_ARRAY_T uint16_t*
213 #define PROGMEM_READ_BYTE(x) pgm_read_byte(x)
214 #define PROGMEM_READ_WORD(x) pgm_read_word(x)
215 #define PROGMEM_READ_DWORD(x) pgm_read_dword(x)
216 
217 #define PROGMEM_READ_BYTE_FAR(x) pgm_read_byte_far(x)
218 #define PROGMEM_READ_WORD_FAR(x) pgm_read_word_far(x)
219 #define PROGMEM_READ_DWORD_FAR(x) pgm_read_dword_far(x)
220 
221 
222 #define NO_INIT __attribute__ ((section (".noinit")))
223 
224 // Some of these definitions stem from <avr/eeprom.h>.
225 //#define EEPROM_DECLARE(var) var EEMEM
226 #define EEPROM_DECLARE(var) var __attribute__((section(".eeprom")))
227 //#define EEGET(var, addr) (var) = eeprom_read_byte ((uint8_t *)(addr))
228 //#define EEPUT(addr, var) eeprom_write_byte ((uint8_t *)(addr), var)
229 
230 #define watchdog_reset() wdt_reset()
231 
232 #define SHORTENUM __attribute__ ((packed))
233 #else
234 #error Compiler not supported.
235 #endif
236 
237 
238 
239 #endif // COMPILER_AVR_H
240 
241