app_eeprom.c 4.0 KB

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