avr-libc  2.1.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

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