STC8G_EEPROM_IAP_ATY.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @file STC8G_EEPROM_IAP_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 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 functions of EEPROM_IAP for STC51
  20. *
  21. * @version
  22. * - 1_01_230114 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __STC8G_EEPROM_IAP_ATY_C
  27. #define __STC8G_EEPROM_IAP_ATY_C
  28. #include "STC8G_EEPROM_IAP_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. /**
  32. * @brief close IAP
  33. */
  34. void IAP_Idle(void)
  35. {
  36. IAP_CONTR = 0; // close IAP
  37. IAP_CMD = 0; // clear command reg
  38. IAP_TRIG = 0; // clear trig reg
  39. IAP_ADDRH = 0x80; // set addr to none IAP area
  40. IAP_ADDRL = 0;
  41. }
  42. /**
  43. * @brief erase IAP
  44. * @param addr page start address to erase
  45. */
  46. void IAP_Erase(uint16_t addr)
  47. {
  48. IAP_CONTR = 0x80; // enable IAP
  49. IAP_TPS = 24; // set waitting param 24MHz
  50. /* 11: Erase the EEPROM. Erase the 1 page (1 sector /512 bytes) where the
  51. target address is located. Note: The erase operation erases one sector (512
  52. bytes) at a time, turning the entire sector contents into FFH. */
  53. IAP_CMD = 3; // erase IAP
  54. IAP_ADDRL = addr; // set IAP low addr
  55. IAP_ADDRH = addr >> 8; // set IAP high addr
  56. IAP_TRIG = 0x5a; // write trigger command(0x5a)
  57. IAP_TRIG = 0xa5; // write trigger command(0xa5)
  58. __NOP_ATY;
  59. IAP_Idle(); // close IAP
  60. }
  61. /**
  62. * @brief read byte from IAP
  63. * @param addr address to read
  64. * @return read byte data
  65. */
  66. uint8_t IAP_ReadByte(uint16_t addr)
  67. {
  68. uint8_t temp_uint8;
  69. IAP_CONTR = 0x80; // enable IAP
  70. IAP_TPS = 24; // set waitting param 24MHz
  71. IAP_CMD = 1; // set IAP read
  72. IAP_ADDRL = addr; // set IAP low addr
  73. IAP_ADDRH = addr >> 8; // set IAP high addr
  74. IAP_TRIG = 0x5a; // write trigger command(0x5a)
  75. IAP_TRIG = 0xa5; // write trigger command(0xa5)
  76. __NOP_ATY;
  77. temp_uint8 = IAP_DATA; // read IAP data
  78. IAP_Idle(); // close IAP
  79. return temp_uint8;
  80. }
  81. #ifdef EEPROM_USE_MOVC
  82. uint8_t IAP_ReadByte(uint16_t addr)
  83. {
  84. #define IAP_OFFSET 0x2000 //STC8G1K08
  85. addr += IAP_OFFSET;
  86. return *(uint8_t code*)(addr); // read daa use MOVC
  87. }
  88. #endif /* EEPROM_USE_MOVC */
  89. /**
  90. * @brief write byte to IAP
  91. * @param addr address to write
  92. * @param dataByte data to write
  93. */
  94. void IAP_WriteByte(uint16_t addr, uint8_t dataByte)
  95. {
  96. IAP_CONTR = 0x80; // enable IAP
  97. IAP_TPS = 24; // set waitting param 24MHz
  98. IAP_CMD = 2; // set IAP write
  99. IAP_ADDRL = addr; // set IAP low addr
  100. IAP_ADDRH = addr >> 8; // set IAP high addr
  101. IAP_DATA = dataByte; // write IAP data
  102. IAP_TRIG = 0x5a; // write trigger command(0x5a)
  103. IAP_TRIG = 0xa5; // write trigger command(0xa5)
  104. __NOP_ATY;
  105. IAP_Idle(); // close IAP
  106. }
  107. /**
  108. * @brief read 4 byte float from IAP
  109. * @param addr address to read
  110. * @return read float data
  111. */
  112. float IAP_ReadFloat(uint16_t addr)
  113. {
  114. union
  115. {
  116. float f_t;
  117. uint8_t u8_t[4];
  118. }tempUnion;
  119. tempUnion.u8_t[0] = IAP_ReadByte(addr);
  120. tempUnion.u8_t[1] = IAP_ReadByte(addr + 1);
  121. tempUnion.u8_t[2] = IAP_ReadByte(addr + 2);
  122. tempUnion.u8_t[3] = IAP_ReadByte(addr + 3);
  123. return tempUnion.f_t;
  124. }
  125. /**
  126. * @brief write 4 byte float to IAP
  127. * @param addr address to write
  128. * @param dataFloat data to write
  129. */
  130. void IAP_WriteFloat(uint16_t addr, float dataFloat)
  131. {
  132. union
  133. {
  134. float f_t;
  135. uint8_t u8_t[4];
  136. }tempUnion;
  137. tempUnion.f_t = dataFloat;
  138. IAP_WriteByte(addr, tempUnion.u8_t[0]);
  139. IAP_WriteByte(addr + 1, tempUnion.u8_t[1]);
  140. IAP_WriteByte(addr + 2, tempUnion.u8_t[2]);
  141. IAP_WriteByte(addr + 3, tempUnion.u8_t[3]);
  142. }
  143. #ifdef __DEBUG_STC8G_EEPROM_IAP_ATY
  144. /**
  145. * @brief test IAP
  146. * @note put at init or long cycle, do not put at fast cycle loop
  147. */
  148. void IAP_Test(void)
  149. {
  150. if(IAP_ReadByte(0x0002) == 0xAA)
  151. UartSendStr("\r\n\r\nEE OK\r\n");
  152. else
  153. {
  154. IAP_Erase(0x0000);
  155. IAP_WriteByte(0x0002, 0xAA);
  156. }
  157. {
  158. uint8_t count_t = 0;
  159. count_t = IAP_ReadByte(0x0003);
  160. IAP_Erase(0x0000);
  161. IAP_WriteByte(0x0002, 0xAA);
  162. IAP_WriteByte(0x0003, count_t + 1);
  163. UartSendStr("EE read\r\n");
  164. UartSendByte((count_t / 100) + '0');
  165. UartSendByte(((count_t % 100) / 10) + '0');
  166. UartSendByte((count_t % 10) + '0');
  167. }
  168. }
  169. #endif /* __DEBUG_STC8G_EEPROM_IAP_ATY */
  170. #endif /* __STC8G_EEPROM_IAP_ATY_C */
  171. /******************************** End Of File *********************************/