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