AT24CXX_ATY.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file AT24CXX_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of AT24CXX for all embedded device
  20. *
  21. * @version
  22. * - 1_01_220602 > ATY
  23. * -# Preliminary version, first Release
  24. * - 1_01_221212 > ATY
  25. * -# Change to general I2C function
  26. * -# Change and remove several functions
  27. ********************************************************************************
  28. */
  29. #ifndef __AT24CXX_ATY_C
  30. #define __AT24CXX_ATY_C
  31. #include "AT24CXX_ATY.h"
  32. /******************************* For user *************************************/
  33. /******************************************************************************/
  34. /**
  35. * @brief Read one byte from assign address
  36. * @param addr address to read data
  37. * @return uint8 data at addr
  38. */
  39. uint8_t AT24CXX_ReadByte(uint16_t addr)
  40. {
  41. uint8_t temp_uint8 = 0, addrG[2] = {addr >> 8, addr % 256};
  42. if(AT24CXX_TYPE > AT24C16)
  43. I2C_Write_NoStop(AT24CXX_ADDRESS, addrG, 2);
  44. else // 04/08/16
  45. I2C_Write_NoStop(AT24CXX_ADDRESS + ((addr / 256) << 1), addrG + 1, 1);
  46. I2C_Read(AT24CXX_ADDRESS, &temp_uint8, 1);
  47. return temp_uint8;
  48. }
  49. /**
  50. * @brief Write one byte to assign address
  51. * @param addr address to write data
  52. * @param data_t data to write
  53. */
  54. void AT24CXX_WriteByte(uint16_t addr, uint8_t data_t)
  55. {
  56. uint8_t addrG[3] = {addr >> 8, addr % 256, data_t};
  57. if(AT24CXX_TYPE > AT24C16)
  58. I2C_Write(AT24CXX_ADDRESS, addrG, 3);
  59. else // 04/08/16
  60. I2C_Write(AT24CXX_ADDRESS + ((addr / 256) << 1), addrG + 1, 2);
  61. DelayMs(AT24CXX_WRITE_DELAY);
  62. }
  63. /**
  64. * @brief Read series data from assign address
  65. * @param addr address to start read data
  66. * @param pbuf data group to save read data
  67. * @param len data length to read
  68. */
  69. void AT24CXX_Read(uint16_t addr, uint8_t* pbuf, uint16_t len)
  70. {
  71. uint8_t addrG[2] = {addr >> 8, addr % 256};
  72. if(AT24CXX_TYPE > AT24C16)
  73. I2C_Write_NoStop(AT24CXX_ADDRESS, addrG, 2);
  74. else // 04/08/16
  75. I2C_Write_NoStop(AT24CXX_ADDRESS + ((addr / 256) << 1), addrG + 1, 1);
  76. I2C_Read(AT24CXX_ADDRESS, pbuf, len);
  77. }
  78. /**
  79. * @brief Write series data to assign address
  80. * @param addr address to start read data
  81. * @param pbuf data group to write
  82. * @param len data length to write
  83. */
  84. void AT24CXX_Write(uint16_t addr, uint8_t* pbuf, uint16_t len)
  85. {
  86. uint8_t* addrG = (uint8_t*)malloc(sizeof(uint8_t) * (len + 2));
  87. addrG[0] = addr >> 8;
  88. addrG[1] = addr % 256;
  89. for(uint16_t i = 0; i < len; i++)
  90. addrG[2 + i] = pbuf[i];
  91. if(AT24CXX_TYPE > AT24C16)
  92. I2C_Write(AT24CXX_ADDRESS, addrG, len + 2);
  93. else // 04/08/16
  94. I2C_Write(AT24CXX_ADDRESS + ((addr / 256) << 1), addrG + 1, len + 1);
  95. free(addrG);
  96. DelayMs(AT24CXX_WRITE_DELAY);
  97. }
  98. /**
  99. * @brief Write random data to assign address
  100. * @param addr address to start read data
  101. * @param pbuf data group to save read data
  102. * @param len data length to write
  103. */
  104. void AT24CXX_WriteRandom(uint16_t addr, uint8_t* pbuf, uint16_t len)
  105. {
  106. uint16_t fullPageCount = 0; // whole page count
  107. uint16_t lastPageSize = 0;
  108. uint16_t firstPageSize = 0;
  109. if(addr + len > AT24CXX_TYPE)
  110. len = AT24CXX_TYPE - addr;
  111. if(addr % AT24CXX_PAGE_SIZE == 0) // addr is the page start
  112. firstPageSize = 0;
  113. else
  114. {
  115. firstPageSize = AT24CXX_PAGE_SIZE - (addr % AT24CXX_PAGE_SIZE);
  116. AT24CXX_Write(addr, pbuf, firstPageSize);
  117. }
  118. if((addr + len) % AT24CXX_PAGE_SIZE == 0) // location addr will be wrote too
  119. lastPageSize = 0;
  120. else
  121. {
  122. lastPageSize = (addr + len) % AT24CXX_PAGE_SIZE;
  123. AT24CXX_Write(addr + len - lastPageSize, pbuf + len - lastPageSize, lastPageSize);
  124. }
  125. fullPageCount = (len - firstPageSize - lastPageSize) / AT24CXX_PAGE_SIZE;
  126. for(uint8_t i = 0; i < fullPageCount; i++)
  127. AT24CXX_Write(addr + firstPageSize + i * AT24CXX_PAGE_SIZE,
  128. pbuf + firstPageSize + i * AT24CXX_PAGE_SIZE, AT24CXX_PAGE_SIZE);
  129. DelayMs(AT24CXX_WRITE_DELAY);
  130. }
  131. /**
  132. * @brief Check device state
  133. * @return errCode, 0: device good, !0: device has problem
  134. */
  135. uint8_t AT24CXX_Check(void)
  136. {
  137. uint8_t temp_uint8;
  138. // avoid writing every boot up
  139. temp_uint8 = AT24CXX_ReadByte(0xFF);
  140. if(temp_uint8 == 0X68)
  141. return 0;
  142. else
  143. {
  144. // may be the first init
  145. AT24CXX_WriteByte(0xFF, 0x68);
  146. temp_uint8 = AT24CXX_ReadByte(0xFF);
  147. if(temp_uint8 == 0x68)
  148. return 0;
  149. }
  150. return 1;
  151. }
  152. /**
  153. * @brief Init device
  154. * @note not useful if i2c functions good
  155. */
  156. void AT24CXX_Init(void)
  157. {
  158. }
  159. #ifdef __DEBUG_AT24CXX_ATY
  160. /**
  161. * @brief Test funtion(can be instead by Check function)
  162. * @return errCode, 0: success, !0: err
  163. */
  164. uint8_t AT24CXX_Test(void)
  165. {
  166. // AT24CXX_Init();
  167. // W/R one byte
  168. uint8_t startupCounte = 0;
  169. startupCounte = AT24CXX_ReadByte(0xFF);
  170. AT24CXX_WriteByte(0xFF, startupCounte + 1);
  171. AT24CXX_ReadByte(0xFF);
  172. // W/R bytes
  173. uint8_t tempTestR_uint8[6] = {0, 0, 0, 0, 0, 0};
  174. uint8_t tempTestW_uint8[5] = {1, 2, 3, 4, 5};
  175. AT24CXX_Write(0xFA, tempTestR_uint8, 0xFF - 0xFA);
  176. AT24CXX_Read(0xFA, tempTestR_uint8, 0x100 - 0xFA);
  177. AT24CXX_Write(0xFA, tempTestW_uint8, 0xFF - 0xFA);
  178. AT24CXX_Read(0xFA, tempTestR_uint8, 0x100 - 0xFA);
  179. // W/R random
  180. uint8_t tempBuf[50];
  181. const char* str = "abcdefghijklmnopqrstuvwxyz0123456789";
  182. AT24CXX_WriteRandom(100, (uint8_t*)str, strlen(str));
  183. AT24CXX_Read(100, tempBuf, strlen(str));
  184. printf("\r\nAT24CXX read data from addr %d at num %d: %s\r\n",
  185. 100, startupCounte, (char*)tempBuf);
  186. if(memcmp(str, tempBuf, strlen(str)) == 0)
  187. return 0;
  188. return 1;
  189. }
  190. #endif /* __DEBUG_AT24CXX_ATY */
  191. #endif /* __AT24CXX_ATY_C */
  192. /******************************** End Of File *********************************/