eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  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 ETH ZURICH 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 ETH ZURICH
  21. * 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. * unix_eeprom.c - avr eeprom functions for unix emulation
  35. *
  36. * 2004.08.05 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  37. *
  38. */
  39. /**
  40. \brief Provides the avr eeprom api. It uses the file 'btnode_eerpom.bin' to store
  41. its content.
  42. */
  43. /* avoid stdio nut wrapper */
  44. #define NO_STDIO_NUT_WRAPPER
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. // eeprom image
  48. static FILE *eepromFile = 0;
  49. // asserts eepromFile open and positions read/write pointer
  50. void unix_eeprom_acces(const void *addr)
  51. {
  52. if (eepromFile == 0) {
  53. // create file if it doenst exist
  54. eepromFile = fopen("eeprom.bin", "a");
  55. if (eepromFile)
  56. fclose(eepromFile);
  57. // open file for read & write
  58. eepromFile = fopen("eeprom.bin", "r+");
  59. if (eepromFile == 0) {
  60. printf("ERROR: Open 'eeprom.bin' failed\n\r");
  61. }
  62. }
  63. if (eepromFile) {
  64. fseek(eepromFile, (long) addr, SEEK_SET);
  65. }
  66. }
  67. /** \ingroup unix_eeprom
  68. write a byte \c val to EEPROM address \c addr */
  69. void eeprom_write_byte(unsigned char *addr, unsigned char val)
  70. {
  71. unix_eeprom_acces(addr);
  72. fwrite(&val, 1, 1, eepromFile);
  73. fflush(eepromFile);
  74. }
  75. /** \ingroup unix_eeprom
  76. read a block of \c n bytes from EEPROM address \c addr to
  77. \c buf */
  78. void eeprom_read_block(void *buf, const void *addr, size_t n)
  79. {
  80. uint16_t count;
  81. unix_eeprom_acces(addr);
  82. count = fread(buf, 1, n, eepromFile);
  83. // fill missing values
  84. while (count < n) {
  85. ((uint8_t *) buf)[count++] = 0xff;
  86. }
  87. }
  88. /** \ingroup unix_eeprom
  89. read one byte from EEPROM address \c addr */
  90. uint8_t eeprom_read_byte(const unsigned char *addr)
  91. {
  92. uint8_t result;
  93. uint16_t count;
  94. unix_eeprom_acces(addr);
  95. count = fread(&result, 1, 1, eepromFile);
  96. if (count != 1)
  97. return -1;
  98. return result;
  99. }
  100. /** \ingroup unix_eeprom
  101. read one 16-bit word (little endian) from EEPROM address \c addr */
  102. uint16_t eeprom_read_word(const unsigned short *addr)
  103. {
  104. uint16_t result;
  105. uint16_t count;
  106. unix_eeprom_acces(addr);
  107. count = fread(&result, 1, 2, eepromFile);
  108. if (count != 2)
  109. return -1;
  110. return result;
  111. }
  112. /*!
  113. * \brief Load data from AVR EEPROM.
  114. *
  115. * \return Allways 0.
  116. */
  117. int NutNvMemLoad(unsigned int addr, void *buff, size_t siz)
  118. {
  119. eeprom_read_block (buff, (void *)addr, siz);
  120. return 0;
  121. }
  122. /*!
  123. * \brief Save data in AVR EEPROM.
  124. *
  125. * \return Allways 0.
  126. */
  127. int NutNvMemSave(unsigned int addr, const void *buff, size_t len)
  128. {
  129. uint8_t *cp;
  130. size_t i;
  131. for (cp = (uint8_t *) buff, i = 0; i < len; cp++, i++) {
  132. if (eeprom_read_byte((uint8_t *) (addr + i)) != *cp) {
  133. eeprom_write_byte((uint8_t *) (addr + i), *cp);
  134. }
  135. }
  136. return 0;
  137. }