at24c.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. * Copyright (C) 2008 by egnite GmbH. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. #include <cfg/os.h>
  35. #include <cfg/eeprom.h>
  36. #include <sys/timer.h>
  37. #include <sys/event.h>
  38. #include <stdlib.h>
  39. #include <dev/twif.h>
  40. #include <dev/at24c.h>
  41. //#define AT24C_AT91
  42. //#define AT24C_DEBUG
  43. #ifdef AT24C_DEBUG
  44. #include <stdio.h>
  45. #endif
  46. /*!
  47. * \brief Helper routine to write to EEPROM with ACK-Polling support
  48. *
  49. * \param at24cs Device descriptor.
  50. * \param buffer Buffer to transfer into EEPROM.
  51. * \param len Number of bytes to write.
  52. * \param addr Address in EEPROM to place data.
  53. *
  54. * \return -1 in case of an error, -2 in case of timeout, else number of bytes written.
  55. */
  56. static int lld_at24_write( struct at24c *at24cs, uint8_t *buffer, uint16_t len, uint16_t addr)
  57. {
  58. uint8_t retry = at24cs->Timeout;
  59. // int tme;
  60. int rc;
  61. #ifdef AT24C_DEBUG
  62. printf("MRW %u\n", len);
  63. #endif
  64. do {
  65. /* Successfull write returns with number of bytes transmitted */
  66. rc = TwMasterRegWrite( at24cs->SlaveAddress, addr, at24cs->IAddrW, buffer, len, at24cs->Timeout);
  67. #ifdef AT24C_DEBUG
  68. printf(" rc:%d re:%u\n", rc, retry);
  69. #endif
  70. if( rc > 0)
  71. return rc;
  72. /* there was an error */
  73. // tme = TwMasterError();
  74. #if 0
  75. if( tme == TWERR_OK)
  76. return 0;
  77. #endif
  78. // if( tme & TWERR_SLA_NACK) {
  79. /* slave might be busy so we retry (ACK-Polling) */
  80. --retry;
  81. NutSleep(1);
  82. // }
  83. // else {
  84. /* it was something else than a NACK */
  85. // return -1;
  86. // }
  87. }
  88. while( retry);
  89. return -2; // we get here if we ran out of delays for ACK of slave address
  90. }
  91. /*!
  92. * \brief Helper routine to read from EEPROM with ACK-Polling support
  93. *
  94. * \param at24cs Device descriptor.
  95. * \param buffer Buffer to transfer to from EEPROM.
  96. * \param len Number of bytes to read.
  97. * \param addr Address in EEPROM to read data from.
  98. *
  99. * \return -1 in case of an error, -2 in case of timeout else number of bytes read.
  100. */
  101. static int lld_at24_read( struct at24c *at24cs, uint8_t *buffer, uint16_t len, uint16_t addr)
  102. {
  103. uint8_t retry = at24cs->Timeout;
  104. uint32_t wtmo = len*retry/at24cs->PageSize;
  105. // int tme;
  106. int rc;
  107. #ifdef AT24C_DEBUG
  108. printf("MRD %u\n", len);
  109. #endif
  110. do {
  111. /* Successfull read returns with number of bytes received */
  112. rc = TwMasterRegRead( at24cs->SlaveAddress, addr, at24cs->IAddrW, buffer, len, wtmo);
  113. if( rc > 0)
  114. return rc;
  115. // tme = TwMasterError();
  116. #ifdef AT24C_DEBUG
  117. printf(" rc:%d re:%u\n", rc, retry);
  118. #endif
  119. // if( tme == TWERR_OK)
  120. // return 0;
  121. /* there was an error */
  122. // if( tme == TWERR_SLA_NACK) {
  123. --retry;
  124. NutSleep(1);
  125. // }
  126. // else
  127. // return -2;
  128. } while( retry);
  129. return -1;
  130. }
  131. /*!
  132. * \brief Read data from EEPROM with ACK-Polling support
  133. *
  134. * \param at24cs Device descriptor.
  135. * \param buffer Buffer to transfer to from EEPROM.
  136. * \param len Number of bytes to read.
  137. * \param addr Address in EEPROM where to read from.
  138. *
  139. * \return 0 on success or -1 in case of an error.
  140. */
  141. /****************************************************************************/
  142. int At24cRead( struct at24c *at24cs, uint8_t *buffer, uint16_t len, uint16_t addr )
  143. /****************************************************************************/
  144. {
  145. int rc = 0;
  146. /* Check if EEPROM is blocked by previous operation */
  147. rc = NutEventWait( &at24cs->ee_mutex, at24cs->Timeout*(len/at24cs->PageSize));
  148. if( rc) return rc;
  149. #ifdef AT24C_BLOCK_ADDR
  150. if( at24cs->BlInSla) {
  151. /* Or blockk address into chip address */
  152. at24cs->SlaveAddress |= (uint8_t)((addr>>8)&0x7);
  153. addr &= 0xFF;
  154. }
  155. #endif
  156. #ifdef AT24C_DEBUG
  157. printf(" sa:%x da:%x l:%u\n", at24cs->SlaveAddress, addr, len);
  158. #endif
  159. /* No, on read we definately do not have to wait for internal programming finished! */
  160. rc = lld_at24_read( at24cs, buffer, len, addr);
  161. #ifdef AT24C_BLOCK_ADDR
  162. if( at24cs->BlInSla) {
  163. /* Reset address to chip without block */
  164. at24cs->SlaveAddress &= ~0x7;
  165. }
  166. #endif
  167. NutEventPost( &at24cs->ee_mutex);
  168. return (rc >= 0) ? 0 : -1;
  169. }
  170. /*!
  171. * \brief Write data into eeprom memory.
  172. *
  173. * \param at24cs Device descriptor.
  174. * \param buffer Buffer to transfer to EEPROM.
  175. * \param len Number of bytes to write.
  176. * \param addr Address in EEPROM to place data.
  177. *
  178. * \return 0 on success or -1 in case of an error.
  179. */
  180. /****************************************************************************/
  181. int At24cWrite( struct at24c *at24cs, uint8_t *buffer, uint16_t len, uint16_t addr)
  182. /****************************************************************************/
  183. {
  184. int rc = 0;
  185. uint8_t *ptr = buffer;
  186. uint32_t bulk;
  187. uint32_t wl = 0;
  188. #ifdef AT24C_BLOCK_ADDR
  189. if( at24cs->BlInSla) {
  190. /* Or blockk address into chip address */
  191. at24cs->SlaveAddress |= (uint8_t)((addr>>8)&0x7);
  192. addr &= 0xff;
  193. }
  194. #endif
  195. #ifdef AT24C_DEBUG
  196. printf(" sa:%x da:%x l:%u\n", at24cs->SlaveAddress, addr, len);
  197. #endif
  198. /* Check if EEPROM is blocked by previous operation */
  199. rc = NutEventWait( &at24cs->ee_mutex, at24cs->Timeout*(len/at24cs->PageSize));
  200. if( rc) return rc;
  201. /* get first bulk of data till page end */
  202. while( len>0)
  203. {
  204. bulk = at24cs->PageSize-(addr%at24cs->PageSize);
  205. if( bulk > len) bulk = len;
  206. rc = lld_at24_write( at24cs, ptr, bulk, addr );
  207. if (rc>=0) wl+=rc; else break;
  208. ptr+=bulk; addr+=bulk; len-=bulk;
  209. }
  210. #ifdef AT24C_BLOCK_ADDR
  211. if( at24cs->BlInSla) {
  212. /* Reset address to chip without block */
  213. at24cs->SlaveAddress &= ~0x7;
  214. }
  215. #endif
  216. if (rc>0) rc = wl;
  217. NutEventPost( &at24cs->ee_mutex);
  218. return rc;
  219. }