$treeview $search $mathjax $extrastylesheet
avr-libc  2.0.0
$projectbrief
$projectbrief
$searchbox

AVR Libc Home Page

AVRs

AVR Libc Development Pages

Main Page

User Manual

Library Reference

FAQ

Example Projects

eeprom.h

00001 /* Copyright (c) 2002, 2003, 2004, 2007 Marek Michalkiewicz
00002    Copyright (c) 2005, 2006 Bjoern Haase
00003    Copyright (c) 2008 Atmel Corporation
00004    Copyright (c) 2008 Wouter van Gulik
00005    Copyright (c) 2009 Dmitry Xmelkov
00006    All rights reserved.
00007 
00008    Redistribution and use in source and binary forms, with or without
00009    modification, are permitted provided that the following conditions are met:
00010 
00011    * Redistributions of source code must retain the above copyright
00012      notice, this list of conditions and the following disclaimer.
00013    * Redistributions in binary form must reproduce the above copyright
00014      notice, this list of conditions and the following disclaimer in
00015      the documentation and/or other materials provided with the
00016      distribution.
00017    * Neither the name of the copyright holders nor the names of
00018      contributors may be used to endorse or promote products derived
00019      from this software without specific prior written permission.
00020 
00021   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00025   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00026   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00027   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00028   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00029   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00030   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031   POSSIBILITY OF SUCH DAMAGE. */
00032 
00033 /* $Id$ */
00034 
00035 #ifndef _AVR_EEPROM_H_
00036 #define _AVR_EEPROM_H_ 1
00037 
00038 #include <avr/io.h>
00039 
00040 #if !E2END && !defined(__DOXYGEN__) && !defined(__COMPILING_AVR_LIBC__)
00041 # warning "Device does not have EEPROM available."
00042 #else
00043 
00044 #if defined (EEAR) && !defined (EEARL) && !defined (EEARH)
00045 #define EEARL EEAR
00046 #endif
00047 
00048 #ifndef __ASSEMBLER__
00049 
00050 #include <stddef.h> /* size_t */
00051 #include <stdint.h>
00052 
00053 /** \defgroup avr_eeprom <avr/eeprom.h>: EEPROM handling
00054     \code #include <avr/eeprom.h> \endcode
00055 
00056     This header file declares the interface to some simple library
00057     routines suitable for handling the data EEPROM contained in the
00058     AVR microcontrollers.  The implementation uses a simple polled
00059     mode interface.  Applications that require interrupt-controlled
00060     EEPROM access to ensure that no time will be wasted in spinloops
00061     will have to deploy their own implementation.
00062 
00063     \par Notes:
00064 
00065     - In addition to the write functions there is a set of update ones.
00066     This functions read each byte first and skip the burning if the
00067     old value is the same with new.  The scaning direction is from
00068     high address to low, to obtain quick return in common cases.
00069 
00070     - All of the read/write functions first make sure the EEPROM is
00071     ready to be accessed.  Since this may cause long delays if a
00072     write operation is still pending, time-critical applications
00073     should first poll the EEPROM e. g. using eeprom_is_ready() before
00074     attempting any actual I/O.  But this functions are not wait until
00075     SELFPRGEN in SPMCSR becomes zero.  Do this manually, if your
00076     softwate contains the Flash burning.
00077 
00078     - As these functions modify IO registers, they are known to be
00079     non-reentrant.  If any of these functions are used from both,
00080     standard and interrupt context, the applications must ensure
00081     proper protection (e.g. by disabling interrupts before accessing
00082     them).
00083 
00084     - All write functions force erase_and_write programming mode.
00085     
00086     - For Xmega the EEPROM start address is 0, like other architectures.
00087     The reading functions add the 0x2000 value to use EEPROM mapping into
00088     data space.
00089  */
00090 
00091 #ifdef __cplusplus
00092 extern "C" {
00093 #endif
00094 
00095 #ifndef __ATTR_PURE__
00096 # ifdef  __DOXYGEN__
00097 #  define __ATTR_PURE__
00098 # else
00099 #  define __ATTR_PURE__  __attribute__((__pure__))
00100 # endif
00101 #endif
00102 
00103 /** \def EEMEM
00104     \ingroup avr_eeprom
00105     Attribute expression causing a variable to be allocated within the
00106     .eeprom section.    */
00107 #define EEMEM __attribute__((section(".eeprom")))
00108 
00109 /** \def eeprom_is_ready
00110     \ingroup avr_eeprom
00111     \returns 1 if EEPROM is ready for a new read/write operation, 0 if not.
00112  */
00113 #if defined (__DOXYGEN__)
00114 # define eeprom_is_ready()
00115 #elif   defined (NVM_STATUS)
00116 # define eeprom_is_ready()  bit_is_clear (NVM_STATUS, NVM_NVMBUSY_bp)
00117 #elif   defined (NVMCTRL_STATUS)
00118 # define eeprom_is_ready()  bit_is_clear (NVMCTRL_STATUS, NVMCTRL_EEBUSY_bp)
00119 #elif   defined (DEECR)
00120 # define eeprom_is_ready()  bit_is_clear (DEECR, BSY)
00121 #elif   defined (EEPE)
00122 # define eeprom_is_ready()  bit_is_clear (EECR, EEPE)
00123 #else
00124 # define eeprom_is_ready()  bit_is_clear (EECR, EEWE)
00125 #endif
00126 
00127 
00128 /** \def eeprom_busy_wait
00129     \ingroup avr_eeprom
00130     Loops until the eeprom is no longer busy.
00131     \returns Nothing.
00132  */      
00133 #define eeprom_busy_wait() do {} while (!eeprom_is_ready())
00134 
00135 
00136 /** \ingroup avr_eeprom
00137     Read one byte from EEPROM address \a __p.
00138  */
00139 uint8_t eeprom_read_byte (const uint8_t *__p) __ATTR_PURE__;
00140 
00141 /** \ingroup avr_eeprom
00142     Read one 16-bit word (little endian) from EEPROM address \a __p.
00143  */
00144 uint16_t eeprom_read_word (const uint16_t *__p) __ATTR_PURE__;
00145 
00146 /** \ingroup avr_eeprom
00147     Read one 32-bit double word (little endian) from EEPROM address \a __p.
00148  */
00149 uint32_t eeprom_read_dword (const uint32_t *__p) __ATTR_PURE__;
00150 
00151 /** \ingroup avr_eeprom
00152     Read one float value (little endian) from EEPROM address \a __p.
00153  */
00154 float eeprom_read_float (const float *__p) __ATTR_PURE__;
00155 
00156 /** \ingroup avr_eeprom
00157     Read a block of \a __n bytes from EEPROM address \a __src to SRAM
00158     \a __dst.
00159  */
00160 void eeprom_read_block (void *__dst, const void *__src, size_t __n);
00161 
00162 
00163 /** \ingroup avr_eeprom
00164     Write a byte \a __value to EEPROM address \a __p.
00165  */
00166 void eeprom_write_byte (uint8_t *__p, uint8_t __value);
00167 
00168 /** \ingroup avr_eeprom
00169     Write a word \a __value to EEPROM address \a __p.
00170  */
00171 void eeprom_write_word (uint16_t *__p, uint16_t __value);
00172 
00173 /** \ingroup avr_eeprom
00174     Write a 32-bit double word \a __value to EEPROM address \a __p.
00175  */
00176 void eeprom_write_dword (uint32_t *__p, uint32_t __value);
00177 
00178 /** \ingroup avr_eeprom
00179     Write a float \a __value to EEPROM address \a __p.
00180  */
00181 void eeprom_write_float (float *__p, float __value);
00182 
00183 /** \ingroup avr_eeprom
00184     Write a block of \a __n bytes to EEPROM address \a __dst from \a __src.
00185     \note The argument order is mismatch with common functions like strcpy().
00186  */
00187 void eeprom_write_block (const void *__src, void *__dst, size_t __n);
00188 
00189 
00190 /** \ingroup avr_eeprom
00191     Update a byte \a __value to EEPROM address \a __p.
00192  */
00193 void eeprom_update_byte (uint8_t *__p, uint8_t __value);
00194 
00195 /** \ingroup avr_eeprom
00196     Update a word \a __value to EEPROM address \a __p.
00197  */
00198 void eeprom_update_word (uint16_t *__p, uint16_t __value);
00199 
00200 /** \ingroup avr_eeprom
00201     Update a 32-bit double word \a __value to EEPROM address \a __p.
00202  */
00203 void eeprom_update_dword (uint32_t *__p, uint32_t __value);
00204 
00205 /** \ingroup avr_eeprom
00206     Update a float \a __value to EEPROM address \a __p.
00207  */
00208 void eeprom_update_float (float *__p, float __value);
00209 
00210 /** \ingroup avr_eeprom
00211     Update a block of \a __n bytes to EEPROM address \a __dst from \a __src.
00212     \note The argument order is mismatch with common functions like strcpy().
00213  */
00214 void eeprom_update_block (const void *__src, void *__dst, size_t __n);
00215 
00216 
00217 /** \name IAR C compatibility defines   */
00218 /*@{*/
00219 
00220 /** \def _EEPUT
00221     \ingroup avr_eeprom
00222     Write a byte to EEPROM. Compatibility define for IAR C. */
00223 #define _EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
00224 
00225 /** \def __EEPUT
00226     \ingroup avr_eeprom
00227     Write a byte to EEPROM. Compatibility define for IAR C. */
00228 #define __EEPUT(addr, val) eeprom_write_byte ((uint8_t *)(addr), (uint8_t)(val))
00229 
00230 /** \def _EEGET
00231     \ingroup avr_eeprom
00232     Read a byte from EEPROM. Compatibility define for IAR C.    */
00233 #define _EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
00234 
00235 /** \def __EEGET
00236     \ingroup avr_eeprom
00237     Read a byte from EEPROM. Compatibility define for IAR C.    */
00238 #define __EEGET(var, addr) (var) = eeprom_read_byte ((const uint8_t *)(addr))
00239 
00240 /*@}*/
00241 
00242 #ifdef __cplusplus
00243 }
00244 #endif
00245 
00246 #endif  /* !__ASSEMBLER__ */
00247 #endif  /* E2END || defined(__DOXYGEN__) || defined(__COMPILING_AVR_LIBC__) */
00248 #endif  /* !_AVR_EEPROM_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Defines