AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
fuse.h
Go to the documentation of this file.
1/* Copyright (c) 2007, Atmel Corporation
2 All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in
12 the documentation and/or other materials provided with the
13 distribution.
14
15 * Neither the name of the copyright holders nor the names of
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE. */
30
31/* avr/fuse.h - Fuse API */
32
33#ifndef _AVR_FUSE_H_
34#define _AVR_FUSE_H_ 1
35
36/* This file must be explicitly included by <avr/io.h>. */
37#if !defined(_AVR_IO_H_)
38#error "You must #include <avr/io.h> and not <avr/fuse.h> by itself."
39#endif
40
41
42/** \file */
43/** \defgroup avr_fuse <avr/fuse.h>: Fuse Support
44 \code #include <avr/io.h> \endcode
45
46 The <avr/fuse.h> header is included by <avr/io.h>.
47
48 \par Introduction
49
50 The Fuse API allows a user to specify the fuse settings for the specific
51 AVR device they are compiling for. These fuse settings will be placed
52 in a special section in the ELF output file, after linking.
53
54 Programming tools can take advantage of the fuse information embedded in
55 the ELF file, by extracting this information and determining if the fuses
56 need to be programmed before programming the Flash and EEPROM memories.
57 This also allows a single ELF file to contain all the
58 information needed to program an AVR.
59
60 To use the Fuse API, include the <avr/io.h> header file, which in turn
61 automatically includes the individual I/O header file and the <avr/fuse.h>
62 file. These other two files provides everything necessary to set the AVR
63 fuses.
64
65 \par Fuse API
66
67 Each I/O header file must define the \c FUSE_MEMORY_SIZE macro which is
68 defined to the number of fuse bytes that exist in the AVR device.
69
70 A new type, __fuse_t, is defined as a structure. The number of fields in
71 this structure are determined by the number of fuse bytes in the
72 \c FUSE_MEMORY_SIZE macro:
73
74 - \c If FUSE_MEMORY_SIZE == 1, there is only a single field: byte, of type
75 #uint8_t.
76
77 - If \c FUSE_MEMORY_SIZE == 2, there are two fields: low, and high, of type
78 #uint8_t.
79
80 - If FUSE_MEMORY_SIZE == 3, there are three fields: low, high, and extended,
81 of type #uint8_t.
82
83 - If \c FUSE_MEMORY_SIZE > 3, there is a single field: byte, which is an
84 array of #uint8_t with the size of the array being \c FUSE_MEMORY_SIZE.
85
86 A convenience macro, \c #FUSEMEM, is defined as a GCC attribute for a
87 custom-named section of \c ".fuse".
88
89 A convenience macro, \c #FUSES, is defined that declares a variable,
90 \c __fuse, of type \c __fuse_t with the attribute defined by \c #FUSEMEM.
91 This variable allows the end user to easily set the fuse data.
92
93 \note If a device-specific I/O header file has previously defined
94 \c FUSEMEM, then \c FUSEMEM is not redefined. If a device-specific
95 I/O header file has previously defined \c FUSES, then
96 \c FUSES is not redefined.
97
98 Each AVR device I/O header file has a set of defined macros which specify
99 the actual fuse bits available on that device. The AVR fuses have inverted
100 values, logical 1 for an unprogrammed (disabled) bit and logical 0 for a
101 programmed (enabled) bit. The defined macros for each individual fuse
102 bit represent this in their definition by a bit-wise inversion of a mask.
103 For example, the \c FUSE_EESAVE fuse in the ATmega128 is defined as:
104 \code
105 #define FUSE_EESAVE ~_BV(3)
106 \endcode
107 The \c #_BV macro creates a bit mask from a bit number. It is then
108 inverted to represent logical values for a fuse memory byte.
109 To combine the fuse bits macros together to represent a whole fuse byte,
110 use the bitwise AND operator, like so:
111 \code
112 (FUSE_BOOTSZ0 & FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN)
113 \endcode
114
115 \warning Many device headers define fuse macros for <b>not inverted</b>
116 fuse bits, like for example devices from the 0-series, 1-series and
117 2-series. Make sure you are using the right logic operations when
118 using fuse values, or otherwise you can damage a device.
119
120 Each device I/O header file also defines macros that provide default values
121 for each fuse byte that is available. \c LFUSE_DEFAULT is defined for a Low
122 Fuse byte. \c HFUSE_DEFAULT is defined for a High Fuse byte.
123 \c EFUSE_DEFAULT is defined for an Extended Fuse byte.
124
125 If \c FUSE_MEMORY_SIZE > 3, then the I/O header file defines macros that
126 provide default values for each fuse byte like so:
127 \code
128 FUSE0_DEFAULT
129 FUSE1_DEFAULT
130 FUSE2_DEFAULT
131 FUSE3_DEFAULT
132 FUSE4_DEFAULT
133 ...
134 \endcode
135
136 \par API Usage Example
137
138 Putting all of this together is easy. Using C99's designated initializers:
139
140 \code
141 #include <avr/io.h>
142
143 FUSES =
144 {
145 .low = LFUSE_DEFAULT,
146 .high = FUSE_BOOTSZ0 & FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN,
147 .extended = EFUSE_DEFAULT
148 };
149 \endcode
150
151 Or, using the variable directly instead of the FUSES macro,
152
153 \code
154 #include <avr/io.h>
155
156 __fuse_t __fuse FUSEMEM =
157 {
158 .low = LFUSE_DEFAULT,
159 .high = FUSE_BOOTSZ0 & FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN,
160 .extended = EFUSE_DEFAULT
161 };
162 \endcode
163
164 If you are compiling in C++, you cannot use the designated initializers so
165 you must do:
166
167 \code
168 #include <avr/io.h>
169
170 FUSES =
171 {
172 LFUSE_DEFAULT, // .low
173 FUSE_BOOTSZ0 & FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN, // .high
174 EFUSE_DEFAULT // .extended
175 };
176 \endcode
177
178 However there are a number of caveats that you need to be aware of to
179 use this API properly.
180
181 Be sure to include <avr/io.h> to get all of the definitions for the API.
182 The FUSES macro defines a global variable to store the fuse data. This
183 variable is assigned to its own linker section. Assign the desired fuse
184 values immediately in the variable initialization.
185
186 The .fuse section in the ELF file will get its values from the initial
187 variable assignment ONLY. This means that you can NOT assign values to
188 this variable in functions and the new values will not be put into the
189 ELF \c \.fuse section.
190
191 The global variable is declared in the \c FUSES macro has two leading
192 underscores, which means that it is reserved for the "implementation",
193 meaning the library, so it will not conflict with a user-named variable.
194
195 You must initialize ALL fields in the \c __fuse_t structure. This is because
196 the fuse bits in all bytes default to a logical 1, meaning unprogrammed.
197 Normal uninitialized data defaults to all logical zeros. So it is vital that
198 all fuse bytes are initialized, even with default data. If they are not,
199 then the fuse bits may not programmed to the desired settings.
200
201 Be sure to have the <tt>-mmcu=<em>device</em></tt> flag in your
202 compile command line and
203 your linker command line to have the correct device selected and to have
204 the correct I/O header file included when you include <avr/io.h>.
205
206 You can print out the contents of the .fuse section in the ELF file by
207 using this command line:
208 \code
209 avr-objdump -s -j .fuse <ELF file>
210 \endcode
211 The section contents shows the address on the left, then the data going from
212 lower address to a higher address, left to right.
213
214*/
215
216#if !defined(__ASSEMBLER__)
217
218#include <stdint.h>
219
220/** \ingroup avr_fuse */
221#ifndef FUSEMEM
222#define FUSEMEM __attribute__((__used__, __section__ (".fuse")))
223#endif
224
225#ifdef __DOXYGEN__
226/** \ingroup avr_fuse
227 A convenience macro. On Xmega devices, it is defined as
228 \code
229 #define FUSES NVM_FUSES_t __fuse FUSEMEM
230 \endcode
231 Otherwise, the definition is:
232 \code
233 #define FUSES __fuse_t __fuse FUSEMEM
234 \endcode */
235#define FUSES
236#else /* Doxygen */
237
238#if FUSE_MEMORY_SIZE > 3
239
240typedef struct
241{
242 uint8_t byte[FUSE_MEMORY_SIZE];
243} __fuse_t;
244
245
246#elif FUSE_MEMORY_SIZE == 3
247
248typedef struct
249{
250 uint8_t low;
251 uint8_t high;
252 uint8_t extended;
253} __fuse_t;
254
255#elif FUSE_MEMORY_SIZE == 2
256
257typedef struct
258{
259 uint8_t low;
260 uint8_t high;
261} __fuse_t;
262
263#elif FUSE_MEMORY_SIZE == 1
264
265typedef struct
266{
267 uint8_t byte;
268} __fuse_t;
269
270#endif
271
272#if !defined(FUSES)
273 #if defined(__AVR_XMEGA__)
274 #define FUSES NVM_FUSES_t __fuse FUSEMEM
275 #else
276 #define FUSES __fuse_t __fuse FUSEMEM
277 #endif
278#endif
279
280
281#endif /* !Doxygen */
282#endif /* !__ASSEMBLER__ */
283
284#endif /* _AVR_FUSE_H_ */
unsigned char uint8_t
Definition: stdint.h:88