nv_storage.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <string.h>
  2. #include "nv_storage.h"
  3. #include "bsp/AT24CXX.h"
  4. #include "app/sox/soc.h"
  5. #include "libs/logger.h"
  6. #include "libs/shark_utils.h"
  7. #if 0
  8. static void backup_timer_hander(shark_timer_t *timer);
  9. static void nv_save_soc_by_backup(int index);
  10. #endif
  11. static void nv_save_soc_task(shark_timer_t *timer);
  12. #define SOC_ADDR 0
  13. static shark_timer_t _save_backup_timer = {.handler = nv_save_soc_task};
  14. #define SOC_SIZE (((sizeof(soc_t) + sizeof(uint16_t)) + 0xF)&(0xFFF0)) //需要16字节对齐
  15. #define SN_ADDR (SOC_ADDR + (SOC_SIZE * 2))
  16. static uint8_t soc_write_pending = 0;
  17. static uint8_t soc_write_backup_index = 0;
  18. static uint8_t soc_write_index = 0;
  19. static uint8_t soc_data[(sizeof(soc_t) + sizeof(uint16_t))];
  20. int nv_save_sn(uint8_t *sn, int len){
  21. sn_t sn_info;
  22. if (len > sizeof(sn_info.sn)){
  23. return -1;
  24. }
  25. memcpy(sn_info.sn, sn, len);
  26. sn_info.len = len;
  27. sn_info.crc = shark_crc16_update(0, (const u8 *)sn_info.sn, len);
  28. return AT24CXX_Write(SN_ADDR, (uint8_t *)&sn_info, sizeof(sn_info));
  29. }
  30. int nv_read_sn(uint8_t *sn, int len){
  31. sn_t sn_info;
  32. if (len < sizeof(sn_info.sn)){
  33. return -1;
  34. }
  35. if (AT24CXX_Read(SN_ADDR, (uint8_t *)&sn_info, sizeof(sn_info)) < 0) {
  36. return -1;
  37. }
  38. if (sn_info.len > sizeof(sn_info.sn)){
  39. return -1;
  40. }
  41. uint16_t crc = shark_crc16_update(0, (const u8 *)sn_info.sn, sn_info.len);
  42. if (crc != sn_info.crc){
  43. return -1;
  44. }
  45. memcpy(sn, sn_info.sn, sn_info.len);
  46. return sn_info.len;
  47. }
  48. /* soc 保存,拆分每次保存一个byte,确保e2rom写操作不会占用太长时间 */
  49. void nv_save_soc(void){
  50. #if 0
  51. nv_save_soc_by_backup(0);
  52. shark_timer_post(&_save_backup_timer, 10);
  53. #else
  54. if (soc_write_pending == 0){
  55. soc_write_pending = 1;
  56. memcpy(soc_data, (void *)get_soc(), sizeof(soc_t));
  57. soc_write_backup_index = 0;
  58. soc_write_index = 0;
  59. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc_data, sizeof(soc_t));
  60. shark_encode_u16(soc_data + sizeof(soc_t), crc16);
  61. shark_timer_post(&_save_backup_timer, 0);
  62. }
  63. #endif
  64. }
  65. static void nv_save_soc_task(shark_timer_t *timer){
  66. if (soc_write_pending == 0) {
  67. return;
  68. }
  69. if (soc_write_index < sizeof(soc_data)){
  70. AT24CXX_Write(SOC_ADDR + SOC_SIZE * soc_write_backup_index + soc_write_index, soc_data + soc_write_index, 1);
  71. soc_write_index ++;
  72. }else {
  73. soc_write_index = 0;
  74. soc_write_backup_index ++;
  75. if (soc_write_backup_index == 2){
  76. soc_write_pending = 0;
  77. sys_debug("write soc to nv OK\n");
  78. return;
  79. }
  80. }
  81. shark_timer_post(&_save_backup_timer, 0);
  82. }
  83. #if 0
  84. static void backup_timer_hander(shark_timer_t *timer){
  85. nv_save_soc_by_backup(1);
  86. }
  87. #endif
  88. static void nv_save_soc_by_backup(int index){
  89. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  90. soc_t *soc = get_soc();
  91. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t));
  92. AT24CXX_Write(nv_addr ,(uint8_t *)soc, sizeof(soc_t));
  93. AT24CXX_Write(nv_addr + sizeof(soc_t),(uint8_t *)&crc16, sizeof(crc16));
  94. }
  95. void nv_erase(void){
  96. uint8_t data = 0xFF;
  97. for (int i = 0; i < sizeof(soc_t) + sizeof(uint16_t); i++){
  98. AT24CXX_Write(SOC_ADDR + i, &data, 1);
  99. }
  100. for (int i = 0; i < sizeof(soc_t) + sizeof(uint16_t); i++){
  101. AT24CXX_Write(SOC_ADDR + SOC_SIZE + i, &data, 1);
  102. }
  103. }
  104. int nv_restore_soc_by_backup(int index, soc_t *soc){
  105. uint16_t crc_nv;
  106. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  107. AT24CXX_Read(nv_addr , (uint8_t *)soc, sizeof(soc_t));
  108. AT24CXX_Read(nv_addr + sizeof(soc_t), (uint8_t *)&crc_nv, sizeof(crc_nv));
  109. if (shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t)) != crc_nv){
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. int nv_restore_soc(void){
  115. soc_t soc0, soc1;
  116. int success0, success1;
  117. success0 = nv_restore_soc_by_backup(0, &soc0);
  118. success1 = nv_restore_soc_by_backup(1, &soc1);
  119. if (success0 == 0 || success1 == 0){
  120. if (success1 != 0){
  121. nv_save_soc_by_backup(1);
  122. }
  123. if (success0 != 0){
  124. nv_save_soc_by_backup(0);
  125. }
  126. if (success0 == 0){
  127. *get_soc() = soc0;
  128. }else if (success1 == 0){
  129. *get_soc() = soc1;
  130. }
  131. }
  132. return ((success0 == 0) || (success1 == 0))?0:-1;
  133. }