AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

Loading...
Searching...
No Matches
eeprom.h
Go to the documentation of this file.
1/* Copyright (c) 2002, 2003, 2004, 2007 Marek Michalkiewicz
2 Copyright (c) 2005, 2006 Bjoern Haase
3 Copyright (c) 2008 Atmel Corporation
4 Copyright (c) 2008 Wouter van Gulik
5 Copyright (c) 2009 Dmitry Xmelkov
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in
15 the documentation and/or other materials provided with the
16 distribution.
17 * Neither the name of the copyright holders nor the names of
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
32
33#ifndef _AVR_EEPROM_H_
34#define _AVR_EEPROM_H_ 1
35
36#include <avr/io.h>
37
38#if !E2END && !defined(__DOXYGEN__) && !defined(__COMPILING_AVR_LIBC__)
39# warning "Device does not have EEPROM available."
40#else
41
42#if defined (EEAR) && !defined (EEARL) && !defined (EEARH)
43#define EEARL EEAR
44#endif
45
46#ifndef __ASSEMBLER__
47
48#include <stddef.h> /* size_t */
49#include <stdint.h>
50
51/** \file */
52/** \defgroup avr_eeprom <avr/eeprom.h>: EEPROM handling
53 \code #include <avr/eeprom.h> \endcode
54
55 This header file declares the interface to some simple library
56 routines suitable for handling the data EEPROM contained in the
57 AVR microcontrollers. The implementation uses a simple polled
58 mode interface. Applications that require interrupt-controlled
59 EEPROM access to ensure that no time will be wasted in spinloops
60 will have to deploy their own implementation.
61
62 \par Notes:
63
64 - In addition to the write functions there is a set of update ones.
65 This functions read each byte first and skip the burning if the
66 old value is the same with new. The scanning direction is from
67 high address to low, to obtain quick return in common cases.
68
69 - All of the read/write functions first make sure the EEPROM is
70 ready to be accessed. Since this may cause long delays if a
71 write operation is still pending, time-critical applications
72 should first poll the EEPROM e. g. using eeprom_is_ready() before
73 attempting any actual I/O. But this functions does not wait until
74 SELFPRGEN in SPMCSR becomes zero. Do this manually, if your
75 software contains the Flash burning.
76
77 - As these functions modify IO registers, they are known to be
78 non-reentrant. If any of these functions are used from both,
79 standard and interrupt context, the applications must ensure
80 proper protection (e.g. by disabling interrupts before accessing
81 them).
82
83 - All write functions force erase_and_write programming mode.
84
85 - For Xmega the EEPROM start address is 0, like other architectures.
86 The reading functions add the 0x2000 value to use EEPROM mapping into
87 data space.
88 */
89
90#ifdef __cplusplus
91extern "C" {
92#endif
93
94#include <bits/attribs.h>
95
96/** \def EEMEM
97 \ingroup avr_eeprom
98 Attribute expression causing a variable to be allocated within the
99 .eeprom section. */
100#define EEMEM __attribute__((__section__(".eeprom")))
101
102/** \def eeprom_is_ready
103 \ingroup avr_eeprom
104 \returns 1 if EEPROM is ready for a new read/write operation, 0 if not.
105 */
106#if defined (__DOXYGEN__)
107# define eeprom_is_ready()
108#elif defined (NVM_STATUS)
109# define eeprom_is_ready() bit_is_clear (NVM_STATUS, NVM_NVMBUSY_bp)
110#elif defined (NVMCTRL_STATUS)
111# define eeprom_is_ready() bit_is_clear (NVMCTRL_STATUS, NVMCTRL_EEBUSY_bp)
112#elif defined (DEECR)
113# define eeprom_is_ready() bit_is_clear (DEECR, BSY)
114#elif defined (EEPE)
115# define eeprom_is_ready() bit_is_clear (EECR, EEPE)
116#else
117# define eeprom_is_ready() bit_is_clear (EECR, EEWE)
118#endif
119
120
121/** \def eeprom_busy_wait
122 \ingroup avr_eeprom
123 Loops until the eeprom is no longer busy.
124 \returns Nothing.
125 */
126#define eeprom_busy_wait() do {} while (!eeprom_is_ready())
127
128
129/** \ingroup avr_eeprom
130 Read one byte from EEPROM address \a __p.
131 */
132uint8_t eeprom_read_byte (const uint8_t *__p) __ATTR_PURE__;
133
134/** \ingroup avr_eeprom
135 Read one 16-bit word (little endian) from EEPROM address \a __p.
136 */
137uint16_t eeprom_read_word (const uint16_t *__p) __ATTR_PURE__;
138
139/** \ingroup avr_eeprom
140 Read one 32-bit double word (little endian) from EEPROM address \a __p.
141 */
142uint32_t eeprom_read_dword (const uint32_t *__p) __ATTR_PURE__;
143
144/** \ingroup avr_eeprom
145 Read one 64-bit quad word (little endian) from EEPROM address \a __p.
146 */
147#if defined(__DOXYGEN__) || __SIZEOF_LONG_LONG__ == 8
148uint64_t eeprom_read_qword (const uint64_t *__p) __ATTR_PURE__;
149#endif
150
151/** \ingroup avr_eeprom
152 Read one float value (little endian) from EEPROM address \a __p.
153 */
154float eeprom_read_float (const float *__p) __ATTR_PURE__;
155
156/** \ingroup avr_eeprom
157 Read one double value (little endian) from EEPROM address \a __p.
158 */
159#if defined(__DOXYGEN__)
160double eeprom_read_double (const double *__p);
161#elif __SIZEOF_DOUBLE__ == 4
162double eeprom_read_double (const double *__p) __asm("eeprom_read_dword");
163#elif __SIZEOF_DOUBLE__ == 8
164double eeprom_read_double (const double *__p) __asm("eeprom_read_qword");
165#endif
166
167/** \ingroup avr_eeprom
168 Read one long double value (little endian) from EEPROM address \a __p.
169 */
170#if defined(__DOXYGEN__)
171long double eeprom_read_long_double (const long double *__p);
172#elif __SIZEOF_LONG_DOUBLE__ == 4
173long double eeprom_read_long_double (const long double *__p) __asm("eeprom_read_dword");
174#elif __SIZEOF_LONG_DOUBLE__ == 8
175long double eeprom_read_long_double (const long double *__p) __asm("eeprom_read_qword");
176#endif
177
178/** \ingroup avr_eeprom
179 Read a block of \a __n bytes from EEPROM address \a __src to SRAM
180 \a __dst.
181 */
182void eeprom_read_block (void *__dst, const void *__src, size_t __n);
183
184
185/** \ingroup avr_eeprom
186 Write a byte \a __value to EEPROM address \a __p.
187 */
188void eeprom_write_byte (uint8_t *__p, uint8_t __value);
189
190/** \ingroup avr_eeprom
191 Write a word \a __value to EEPROM address \a __p.
192 */
194
195/** \ingroup avr_eeprom
196 Write a 32-bit double word \a __value to EEPROM address \a __p.
197 */
199
200/** \ingroup avr_eeprom
201 Write a 64-bit quad word \a __value to EEPROM address \a __p.
202 */
203#if defined(__DOXYGEN__) || __SIZEOF_LONG_LONG__ == 8
205#endif
206
207/** \ingroup avr_eeprom
208 Write a float \a __value to EEPROM address \a __p.
209 */
210void eeprom_write_float (float *__p, float __value);
211
212/** \ingroup avr_eeprom
213 Write a double \a __value to EEPROM address \a __p.
214 */
215#if defined(__DOXYGEN__)
216void eeprom_write_double (double *__p, double __value);
217#elif __SIZEOF_DOUBLE__ == 4
218void eeprom_write_double (double *__p, double __value) __asm("eeprom_write_dword");
219#elif __SIZEOF_DOUBLE__ == 8
220void eeprom_write_double (double *__p, double __value) __asm("eeprom_write_qword");
221#endif
222
223/** \ingroup avr_eeprom
224 Write a long double \a __value to EEPROM address \a __p.
225 */
226#if defined(__DOXYGEN__)
227void eeprom_write_long_double (long double *__p, long double __value);
228#elif __SIZEOF_LONG_DOUBLE__ == 4
229void eeprom_write_long_double (long double *__p, long double __value) __asm("eeprom_write_dword");
230#elif __SIZEOF_LONG_DOUBLE__ == 8
231void eeprom_write_long_double (long double *__p, long double __value) __asm("eeprom_write_qword");
232#endif
233
234/** \ingroup avr_eeprom
235 Write a block of \a __n bytes to EEPROM address \a __dst from \a __src.
236 \note The argument order is mismatch with common functions like strcpy().
237 */
238void eeprom_write_block (const void *__src, void *__dst, size_t __n);
239
240
241/** \ingroup avr_eeprom
242 Update a byte \a __value at EEPROM address \a __p.
243 */
244void eeprom_update_byte (uint8_t *__p, uint8_t __value);
245
246/** \ingroup avr_eeprom
247 Update a word \a __value at EEPROM address \a __p.
248 */
250
251/** \ingroup avr_eeprom
252 Update a 32-bit double word \a __value at EEPROM address \a __p.
253 */
255
256/** \ingroup avr_eeprom
257 Update a 64-bit quad word \a __value at EEPROM address \a __p.
258 */
259#if defined(__DOXYGEN__) || __SIZEOF_LONG_LONG__ == 8
261#endif
262
263/** \ingroup avr_eeprom
264 Update a float \a __value at EEPROM address \a __p.
265 */
266void eeprom_update_float (float *__p, float __value);
267
268/** \ingroup avr_eeprom
269 Update a double \a __value at EEPROM address \a __p.
270 */
271#if defined(__DOXYGEN__)
272void eeprom_update_double (double *__p, double __value);
273#elif __SIZEOF_DOUBLE__ == 4
274void eeprom_update_double (double *__p, double __value) __asm("eeprom_update_dword");
275#elif __SIZEOF_DOUBLE__ == 8
276void eeprom_update_double (double *__p, double __value) __asm("eeprom_update_qword");
277#endif
278
279/** \ingroup avr_eeprom
280 Update a long double \a __value at EEPROM address \a __p.
281 */
282#if defined(__DOXYGEN__)
283void eeprom_update_long_double (long double *__p, long double __value);
284#elif __SIZEOF_LONG_DOUBLE__ == 4
285void eeprom_update_long_double (long double *__p, long double __value) __asm("eeprom_update_dword");
286#elif __SIZEOF_LONG_DOUBLE__ == 8
287void eeprom_update_long_double (long double *__p, long double __value) __asm("eeprom_update_qword");
288#endif
289
290/** \ingroup avr_eeprom
291 Update a block of \a __n bytes at EEPROM address \a __dst from \a __src.
292 \note The argument order is mismatch with common functions like strcpy().
293 */
294void eeprom_update_block (const void *__src, void *__dst, size_t __n);
295
296
297/** \name IAR C compatibility defines */
298/**@{*/
299
300/** \def _EEPUT
301 \ingroup avr_eeprom
302 Write a byte to EEPROM. Compatibility define for IAR C. */
303#define _EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
304
305/** \def __EEPUT
306 \ingroup avr_eeprom
307 Write a byte to EEPROM. Compatibility define for IAR C. */
308#define __EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
309
310/** \def _EEGET
311 \ingroup avr_eeprom
312 Read a byte from EEPROM. Compatibility define for IAR C. */
313#define _EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
314
315/** \def __EEGET
316 \ingroup avr_eeprom
317 Read a byte from EEPROM. Compatibility define for IAR C. */
318#define __EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
319
320/**@}*/
321
322#ifdef __cplusplus
323}
324#endif
325
326#endif /* !__ASSEMBLER__ */
327#endif /* E2END || defined(__DOXYGEN__) || defined(__COMPILING_AVR_LIBC__) */
328#endif /* !_AVR_EEPROM_H_ */
void eeprom_write_dword(uint32_t *__p, uint32_t __value)
void eeprom_read_block(void *__dst, const void *__src, size_t __n)
void eeprom_update_float(float *__p, float __value)
void eeprom_update_word(uint16_t *__p, uint16_t __value)
void eeprom_write_long_double(long double *__p, long double __value)
void eeprom_write_word(uint16_t *__p, uint16_t __value)
uint8_t eeprom_read_byte(const uint8_t *__p)
double eeprom_read_double(const double *__p)
long double eeprom_read_long_double(const long double *__p)
uint16_t eeprom_read_word(const uint16_t *__p)
void eeprom_update_byte(uint8_t *__p, uint8_t __value)
void eeprom_write_byte(uint8_t *__p, uint8_t __value)
void eeprom_update_double(double *__p, double __value)
void eeprom_update_long_double(long double *__p, long double __value)
void eeprom_update_qword(uint64_t *__p, uint64_t __value)
void eeprom_update_block(const void *__src, void *__dst, size_t __n)
void eeprom_update_dword(uint32_t *__p, uint32_t __value)
void eeprom_write_qword(uint64_t *__p, uint64_t __value)
float eeprom_read_float(const float *__p)
void eeprom_write_block(const void *__src, void *__dst, size_t __n)
void eeprom_write_float(float *__p, float __value)
void eeprom_write_double(double *__p, double __value)
uint64_t eeprom_read_qword(const uint64_t *__p)
uint32_t eeprom_read_dword(const uint32_t *__p)
unsigned int uint16_t
Definition: stdint.h:91
unsigned long int uint32_t
Definition: stdint.h:101
unsigned char uint8_t
Definition: stdint.h:81
unsigned long long int uint64_t
Definition: stdint.h:115