nvmem.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file dev/nvmem.c
  35. * \brief Non-volatile memory access.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.8 2009/01/19 10:39:33 haraldkipp
  41. * Support for AT45D SPI bus driver added.
  42. *
  43. * Revision 1.7 2009/01/17 11:26:46 haraldkipp
  44. * Getting rid of two remaining BSD types in favor of stdint.
  45. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  46. *
  47. * Revision 1.6 2006/09/29 12:41:55 haraldkipp
  48. * Added support for AT45 serial DataFlash memory chips. Currently limited
  49. * to AT91 builds.
  50. *
  51. * Revision 1.5 2006/07/27 07:15:34 haraldkipp
  52. * Final SAM7X support added.
  53. *
  54. * Revision 1.4 2006/07/05 07:52:31 haraldkipp
  55. * Added Daidai's version of using the AT91SAM7X
  56. * on-chip flash for storing configuration data.
  57. *
  58. * Revision 1.3 2006/05/25 09:09:57 haraldkipp
  59. * API documentation updated and corrected.
  60. *
  61. * Revision 1.2 2006/04/07 12:29:42 haraldkipp
  62. * AT49BV flash memory support added. A single sector may be used to
  63. * store system configurations in case there is no EEPROM available.
  64. *
  65. * Revision 1.1 2006/01/23 17:26:19 haraldkipp
  66. * Platform independant routines added, which provide generic access to
  67. * non-volatile memory.
  68. *
  69. *
  70. * \endverbatim
  71. */
  72. #include <dev/nvmem.h>
  73. #if defined(NUT_CONFIG_X12RTC)
  74. #include <dev/x12rtc.h>
  75. #elif defined(NUT_CONFIG_AT45D)
  76. #include <dev/nvmem_at45d.h>
  77. #elif defined(NUT_CONFIG_AT45DB)
  78. #include <dev/at45db.h>
  79. #elif defined(NUT_CONFIG_AT49BV)
  80. #include <dev/at49bv.h>
  81. #elif defined(NUT_CONFIG_AT91EFC)
  82. #include <arch/arm/atmel/at91_efc.h>
  83. #elif defined(NUT_CONFIG_STM32_IAP)
  84. #include <arch/cm3/stm/stm32_flash.h>
  85. #elif defined(NUT_CONFIG_STM32L1_EEPROM)
  86. #include <arch/cm3/stm/stm32l1_eeprom.h>
  87. #elif defined(NUT_CONFIG_LPC177x_8x_EEPROM)
  88. #include <arch/cm3/nxp/lpc177x_8x_eeprom.h>
  89. #elif defined(NUT_CONFIG_LPC17xx_IAP)
  90. #include <arch/cm3/nxp/lpc17xx_iap.h>
  91. #elif defined(NUT_CONFIG_AT24)
  92. #include <dev/eeprom.h>
  93. #endif
  94. /*!
  95. * \addtogroup xgNutNvMem
  96. */
  97. /*@{*/
  98. /*!
  99. * \brief Read data from non-volatile memory.
  100. *
  101. * This routine provides platform independent access to non-volatile
  102. * configuration data.
  103. *
  104. * \param addr Location to read from.
  105. * \param buff Pointer to a buffer that receives the data.
  106. * \param siz Number of bytes to read.
  107. *
  108. * \return 0 on success, -1 otherwise.
  109. */
  110. int NutNvMemLoad(unsigned int addr, void *buff, size_t siz)
  111. {
  112. #if defined(NUT_CONFIG_X12RTC)
  113. return X12EepromRead(addr, buff, siz);
  114. #elif defined(NUT_CONFIG_AT45D)
  115. return SpiAt45dConfigRead(addr, buff, siz);
  116. #elif defined(NUT_CONFIG_AT45DB)
  117. return At45dbParamRead(addr, buff, siz);
  118. #elif defined(NUT_CONFIG_AT49BV)
  119. return At49bvParamRead(addr, buff, siz);
  120. #elif defined(__AVR__)
  121. return OnChipNvMemLoad(addr, buff, siz);
  122. #elif defined(NUT_CONFIG_AT91EFC)
  123. return At91EfcParamRead(addr, buff, siz);
  124. #elif defined(NUT_CONFIG_STM32_IAP)
  125. return Stm32FlashParamRead(addr, buff, siz);
  126. #elif defined(NUT_CONFIG_STM32L1_EEPROM)
  127. return Stm32l1_EepromRead(addr, buff, siz);
  128. #elif defined(NUT_CONFIG_LPC177x_8x_EEPROM)
  129. return Lpc177x_8x_EepromRead(addr, buff, siz);
  130. #elif defined(NUT_CONFIG_LPC17xx_IAP)
  131. return Lpc17xxIapParamRead(addr, buff, siz);
  132. #elif defined(NUT_CONFIG_AT24)
  133. EEInit();
  134. return EEReadData( addr, buff, siz);
  135. #else
  136. return -1;
  137. #endif
  138. }
  139. /*!
  140. * \brief Save data in non-volatile memory.
  141. *
  142. * This routine provides platform independent access to non-volatile
  143. * configuration data.
  144. *
  145. * \param addr Location to write to.
  146. * \param buff Pointer to a buffer that contains the data.
  147. * \param len Number of bytes to write.
  148. *
  149. * \return 0 on success, -1 otherwise.
  150. */
  151. int NutNvMemSave(unsigned int addr, const void *buff, size_t len)
  152. {
  153. #if defined(NUT_CONFIG_X12RTC)
  154. return X12EepromWrite(addr, buff, len);
  155. #elif defined(NUT_CONFIG_AT45D)
  156. return SpiAt45dConfigWrite(addr, buff, len);
  157. #elif defined(NUT_CONFIG_AT45DB)
  158. return At45dbParamWrite(addr, buff, len);
  159. #elif defined(NUT_CONFIG_AT49BV)
  160. return At49bvParamWrite(addr, buff, len);
  161. #elif defined(__AVR__)
  162. return OnChipNvMemSave(addr, buff, len);
  163. #elif defined(NUT_CONFIG_AT91EFC)
  164. return At91EfcParamWrite(addr, buff, len);
  165. #elif defined(NUT_CONFIG_STM32_IAP)
  166. return Stm32FlashParamWrite(addr, buff, len);
  167. #elif defined(NUT_CONFIG_STM32L1_EEPROM)
  168. return Stm32l1_EepromWrite(addr, buff, len);
  169. #elif defined(NUT_CONFIG_LPC177x_8x_EEPROM)
  170. return Lpc177x_8x_EepromWrite(addr, buff, len);
  171. #elif defined(NUT_CONFIG_LPC17xx_IAP)
  172. return Lpc17xxIapParamWrite(addr, buff, len);
  173. #elif defined(NUT_CONFIG_AT24)
  174. return EEWriteData( addr, buff, len);
  175. #else
  176. return -1;
  177. #endif
  178. }
  179. /*@}*/