avr-libc  2.0.0
Standard C library for AVR-GCC
 

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

Loading...
Searching...
No Matches
eeprom.h
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/* $Id$ */
34
35#ifndef _AVR_EEPROM_H_
36#define _AVR_EEPROM_H_ 1
37
38#include <avr/io.h>
39
40#if !E2END && !defined(__DOXYGEN__) && !defined(__COMPILING_AVR_LIBC__)
41# warning "Device does not have EEPROM available."
42#else
43
44#if defined (EEAR) && !defined (EEARL) && !defined (EEARH)
45#define EEARL EEAR
46#endif
47
48#if (EEPROM_PAGE_SIZE == 1)
49/* True for Dx devices right now, can write directly
50 to mapped EEPROM mem by just setting mode to
51 EEERWR, writing bytes and then clearing mode. */
52#define __EEPROM_NON_PAGE_WRITE_AVAILABLE__ 1
53#endif
54
55#ifndef __ASSEMBLER__
56
57#include <stddef.h> /* size_t */
58#include <stdint.h>
59
60/** \defgroup avr_eeprom <avr/eeprom.h>: EEPROM handling
61 \code #include <avr/eeprom.h> \endcode
62
63 This header file declares the interface to some simple library
64 routines suitable for handling the data EEPROM contained in the
65 AVR microcontrollers. The implementation uses a simple polled
66 mode interface. Applications that require interrupt-controlled
67 EEPROM access to ensure that no time will be wasted in spinloops
68 will have to deploy their own implementation.
69
70 \par Notes:
71
72 - In addition to the write functions there is a set of update ones.
73 This functions read each byte first and skip the burning if the
74 old value is the same with new. The scaning direction is from
75 high address to low, to obtain quick return in common cases.
76
77 - All of the read/write functions first make sure the EEPROM is
78 ready to be accessed. Since this may cause long delays if a
79 write operation is still pending, time-critical applications
80 should first poll the EEPROM e. g. using eeprom_is_ready() before
81 attempting any actual I/O. But this functions are not wait until
82 SELFPRGEN in SPMCSR becomes zero. Do this manually, if your
83 softwate contains the Flash burning.
84
85 - As these functions modify IO registers, they are known to be
86 non-reentrant. If any of these functions are used from both,
87 standard and interrupt context, the applications must ensure
88 proper protection (e.g. by disabling interrupts before accessing
89 them).
90
91 - All write functions force erase_and_write programming mode.
92
93 - For Xmega the EEPROM start address is 0, like other architectures.
94 The reading functions add the 0x2000 value to use EEPROM mapping into
95 data space.
96 */
97
98#ifdef __cplusplus
99extern "C" {
100#endif
101
102#ifndef __ATTR_PURE__
103# ifdef __DOXYGEN__
104# define __ATTR_PURE__
105# else
106# define __ATTR_PURE__ __attribute__((__pure__))
107# endif
108#endif
109
110/** \def EEMEM
111 \ingroup avr_eeprom
112 Attribute expression causing a variable to be allocated within the
113 .eeprom section. */
114#define EEMEM __attribute__((section(".eeprom")))
115
116/** \def eeprom_is_ready
117 \ingroup avr_eeprom
118 \returns 1 if EEPROM is ready for a new read/write operation, 0 if not.
119 */
120#if defined (__DOXYGEN__)
121# define eeprom_is_ready()
122#elif defined (NVM_STATUS)
123# define eeprom_is_ready() bit_is_clear (NVM_STATUS, NVM_NVMBUSY_bp)
124#elif defined (NVMCTRL_STATUS)
125# define eeprom_is_ready() bit_is_clear (NVMCTRL_STATUS, NVMCTRL_EEBUSY_bp)
126#elif defined (DEECR)
127# define eeprom_is_ready() bit_is_clear (DEECR, BSY)
128#elif defined (EEPE)
129# define eeprom_is_ready() bit_is_clear (EECR, EEPE)
130#else
131# define eeprom_is_ready() bit_is_clear (EECR, EEWE)
132#endif
133
134
135/** \def eeprom_busy_wait
136 \ingroup avr_eeprom
137 Loops until the eeprom is no longer busy.
138 \returns Nothing.
139 */
140#define eeprom_busy_wait() do {} while (!eeprom_is_ready())
141
142
143/** \ingroup avr_eeprom
144 Read one byte from EEPROM address \a __p.
145 */
146uint8_t eeprom_read_byte (const uint8_t *__p) __ATTR_PURE__;
147
148/** \ingroup avr_eeprom
149 Read one 16-bit word (little endian) from EEPROM address \a __p.
150 */
151uint16_t eeprom_read_word (const uint16_t *__p) __ATTR_PURE__;
152
153/** \ingroup avr_eeprom
154 Read one 32-bit double word (little endian) from EEPROM address \a __p.
155 */
156uint32_t eeprom_read_dword (const uint32_t *__p) __ATTR_PURE__;
157
158/** \ingroup avr_eeprom
159 Read one float value (little endian) from EEPROM address \a __p.
160 */
161float eeprom_read_float (const float *__p) __ATTR_PURE__;
162
163/** \ingroup avr_eeprom
164 Read a block of \a __n bytes from EEPROM address \a __src to SRAM
165 \a __dst.
166 */
167void eeprom_read_block (void *__dst, const void *__src, size_t __n);
168
169
170/** \ingroup avr_eeprom
171 Write a byte \a __value to EEPROM address \a __p.
172 */
173void eeprom_write_byte (uint8_t *__p, uint8_t __value);
174
175/** \ingroup avr_eeprom
176 Write a word \a __value to EEPROM address \a __p.
177 */
179
180/** \ingroup avr_eeprom
181 Write a 32-bit double word \a __value to EEPROM address \a __p.
182 */
184
185/** \ingroup avr_eeprom
186 Write a float \a __value to EEPROM address \a __p.
187 */
188void eeprom_write_float (float *__p, float __value);
189
190/** \ingroup avr_eeprom
191 Write a block of \a __n bytes to EEPROM address \a __dst from \a __src.
192 \note The argument order is mismatch with common functions like strcpy().
193 */
194void eeprom_write_block (const void *__src, void *__dst, size_t __n);
195
196
197/** \ingroup avr_eeprom
198 Update a byte \a __value to EEPROM address \a __p.
199 */
200void eeprom_update_byte (uint8_t *__p, uint8_t __value);
201
202/** \ingroup avr_eeprom
203 Update a word \a __value to EEPROM address \a __p.
204 */
206
207/** \ingroup avr_eeprom
208 Update a 32-bit double word \a __value to EEPROM address \a __p.
209 */
211
212/** \ingroup avr_eeprom
213 Update a float \a __value to EEPROM address \a __p.
214 */
215void eeprom_update_float (float *__p, float __value);
216
217/** \ingroup avr_eeprom
218 Update a block of \a __n bytes to EEPROM address \a __dst from \a __src.
219 \note The argument order is mismatch with common functions like strcpy().
220 */
221void eeprom_update_block (const void *__src, void *__dst, size_t __n);
222
223
224/** \name IAR C compatibility defines */
225/*@{*/
226
227/** \def _EEPUT
228 \ingroup avr_eeprom
229 Write a byte to EEPROM. Compatibility define for IAR C. */
230#define _EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
231
232/** \def __EEPUT
233 \ingroup avr_eeprom
234 Write a byte to EEPROM. Compatibility define for IAR C. */
235#define __EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
236
237/** \def _EEGET
238 \ingroup avr_eeprom
239 Read a byte from EEPROM. Compatibility define for IAR C. */
240#define _EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
241
242/** \def __EEGET
243 \ingroup avr_eeprom
244 Read a byte from EEPROM. Compatibility define for IAR C. */
245#define __EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
246
247/*@}*/
248
249#ifdef __cplusplus
250}
251#endif
252
253#endif /* !__ASSEMBLER__ */
254#endif /* E2END || defined(__DOXYGEN__) || defined(__COMPILING_AVR_LIBC__) */
255#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)
uint8_t eeprom_read_byte(const uint8_t *__p) __ATTR_PURE__
void eeprom_write_word(uint16_t *__p, uint16_t __value)
void eeprom_update_byte(uint8_t *__p, uint8_t __value)
void eeprom_write_byte(uint8_t *__p, uint8_t __value)
float eeprom_read_float(const float *__p) __ATTR_PURE__
uint32_t eeprom_read_dword(const uint32_t *__p) __ATTR_PURE__
void eeprom_update_block(const void *__src, void *__dst, size_t __n)
void eeprom_update_dword(uint32_t *__p, uint32_t __value)
uint16_t eeprom_read_word(const uint16_t *__p) __ATTR_PURE__
void eeprom_write_block(const void *__src, void *__dst, size_t __n)
void eeprom_write_float(float *__p, float __value)
unsigned int uint16_t
Definition stdint.h:93
unsigned long int uint32_t
Definition stdint.h:103
unsigned char uint8_t
Definition stdint.h:83