app_eeprom.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "app_eeprom.h"
  2. #include <string.h>
  3. uint8_t app_eeprom_read_buf[520];
  4. uint8_t app_eeprom_write_buf[520];
  5. app_eeprom_read_cache_t app_eeprom_read_cache = {
  6. .buffer = app_eeprom_read_buf,
  7. .addr = 0,
  8. .len = 0,
  9. .cur = 0,
  10. .pfun = 0,
  11. .used = 0,
  12. };
  13. app_eeprom_write_cache_t app_eeprom_write_cache = {
  14. .buffer = app_eeprom_write_buf,
  15. .addr = 0,
  16. .len = 0,
  17. .cur = 0,
  18. .count = 0,
  19. .pfun = 0,
  20. .used = 0,
  21. };
  22. void app_eeprom_read_task(void)
  23. {
  24. uint16_t read_len;
  25. uint16_t read_cur;
  26. uint8_t err_code = 0;
  27. uint16_t tmp_read_len;
  28. uint16_t mod;
  29. if (app_eeprom_read_cache.used == 1)
  30. {
  31. mod = (app_eeprom_read_cache.addr + app_eeprom_read_cache.cur + APP_EEPROM_READ_STEP) % APP_EEPROM_READ_STEP;
  32. if (mod)
  33. {
  34. tmp_read_len = APP_EEPROM_READ_STEP - mod;
  35. }
  36. else
  37. {
  38. tmp_read_len = APP_EEPROM_READ_STEP;
  39. }
  40. if (app_eeprom_read_cache.cur + tmp_read_len < app_eeprom_read_cache.len)
  41. {
  42. read_len = tmp_read_len;
  43. }
  44. else
  45. {
  46. read_len = app_eeprom_read_cache.len - app_eeprom_read_cache.cur;
  47. }
  48. read_cur = app_eeprom_read_cache.cur;
  49. dev_at24cxx_read_bytes(app_eeprom_read_cache.addr + read_cur, &app_eeprom_read_cache.buffer[read_cur], read_len);
  50. app_eeprom_read_cache.cur += read_len;
  51. if (app_eeprom_read_cache.cur == app_eeprom_read_cache.len)
  52. {
  53. // if (app_eeprom_read_cache.buffer[app_eeprom_read_cache.len-1]!=)
  54. if (app_eeprom_read_cache.pfun != NULL)
  55. {
  56. // app_eeprom_read_cache.pfun(app_eeprom_read_cache)
  57. }
  58. app_eeprom_read_cache.used = 0;
  59. }
  60. }
  61. }
  62. void app_eeprom_write_task(void)
  63. {
  64. uint16_t write_len;
  65. uint16_t write_cur;
  66. uint16_t tmp_write_len;
  67. uint16_t mod;
  68. if (app_eeprom_write_cache.used == 1)
  69. {
  70. mod = (app_eeprom_write_cache.addr + app_eeprom_write_cache.cur + APP_EEPROM_WRITE_STEP) % APP_EEPROM_WRITE_STEP;
  71. if (mod)
  72. {
  73. tmp_write_len = APP_EEPROM_WRITE_STEP - mod;
  74. }
  75. else
  76. {
  77. tmp_write_len = APP_EEPROM_WRITE_STEP;
  78. }
  79. if (app_eeprom_write_cache.cur + tmp_write_len < app_eeprom_write_cache.len)
  80. {
  81. write_len = tmp_write_len;
  82. }
  83. else
  84. {
  85. write_len = app_eeprom_write_cache.len - app_eeprom_write_cache.cur;
  86. }
  87. write_cur = app_eeprom_write_cache.cur;
  88. dev_at24cxx_write_bytes(app_eeprom_write_cache.addr + write_cur, &app_eeprom_write_cache.buffer[write_cur], write_len);
  89. app_eeprom_write_cache.cur += write_len;
  90. if (app_eeprom_write_cache.cur == app_eeprom_write_cache.len)
  91. { // 更新记录的数量
  92. if (app_eeprom_write_cache.count != 0)
  93. {
  94. // dev_at24cxx_write_bytes(app_eeprom_write_cache.addr + write_cur, &app_eeprom_write_cache.buffer[write_cur], write_len);
  95. }
  96. if (app_eeprom_write_cache.pfun != NULL)
  97. {
  98. // app_eeprom_write_cache.pfun(app_eeprom_write_cache)
  99. }
  100. app_eeprom_write_cache.used = 0;
  101. }
  102. }
  103. }
  104. uint8_t read_buf[50];
  105. uint8_t app_eeprom_read_50_zhuzu(void)
  106. {
  107. if (app_eeprom_read_cache.used == 1)
  108. {
  109. return 1;
  110. }
  111. memcpy(app_eeprom_read_cache.buffer, read_buf, 200);
  112. app_eeprom_read_cache.addr = 0;
  113. app_eeprom_read_cache.len = 50;
  114. app_eeprom_read_cache.cur = 0;
  115. app_eeprom_read_cache.used = 1;
  116. return 0;
  117. }
  118. uint8_t app_eeprom_write_50_zhuzu(void)
  119. {
  120. uint8_t write_buf[50];
  121. for (uint8_t i = 0; i < 50; i++)
  122. {
  123. write_buf[i] = i;
  124. }
  125. if (app_eeprom_write_cache.used == 1)
  126. {
  127. return 1;
  128. }
  129. memcpy(app_eeprom_write_cache.buffer, write_buf, 50);
  130. app_eeprom_write_cache.addr = 0;
  131. app_eeprom_write_cache.len = 50;
  132. app_eeprom_write_cache.cur = 0;
  133. app_eeprom_write_cache.used = 1;
  134. return 0;
  135. }