nv_storage.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_read_write_test(void){
  21. uint8_t data[32];
  22. memset(data, 0x5a, sizeof(data));
  23. AT24CXX_Write(0, data, sizeof(data));
  24. memset(data, 0x0, sizeof(data));
  25. AT24CXX_Read(0, data, sizeof(data));
  26. for (int i = 0; i < sizeof(data); i++){
  27. if (data[i] != 0x5a) {
  28. return 1;
  29. }
  30. }
  31. return 0;
  32. }
  33. int nv_save_sn(uint8_t *sn, int len){
  34. sn_t sn_info;
  35. if (len > sizeof(sn_info.sn)){
  36. return -1;
  37. }
  38. memcpy(sn_info.sn, sn, len);
  39. sn_info.len = len;
  40. sn_info.crc = shark_crc16_update(0, (const u8 *)sn_info.sn, len);
  41. return AT24CXX_Write(SN_ADDR, (uint8_t *)&sn_info, sizeof(sn_info));
  42. }
  43. int nv_read_sn(uint8_t *sn, int len){
  44. sn_t sn_info;
  45. if (len < sizeof(sn_info.sn)){
  46. return -1;
  47. }
  48. if (AT24CXX_Read(SN_ADDR, (uint8_t *)&sn_info, sizeof(sn_info)) < 0) {
  49. return -1;
  50. }
  51. if (sn_info.len > sizeof(sn_info.sn)){
  52. return -1;
  53. }
  54. uint16_t crc = shark_crc16_update(0, (const u8 *)sn_info.sn, sn_info.len);
  55. if (crc != sn_info.crc){
  56. return -1;
  57. }
  58. memcpy(sn, sn_info.sn, sn_info.len);
  59. return sn_info.len;
  60. }
  61. static int _soc_write_error = 0;
  62. static int _soc_write_success = 0;
  63. /* soc 保存,拆分每次保存一个byte,确保e2rom写操作不会占用太长时间 */
  64. void nv_save_soc(void){
  65. #if 0
  66. nv_save_soc_by_backup(0);
  67. shark_timer_post(&_save_backup_timer, 10);
  68. #else
  69. if (soc_write_pending == 0){
  70. soc_write_pending = 1;
  71. memcpy(soc_data, (void *)get_soc(), sizeof(soc_t));
  72. soc_write_backup_index = 0;
  73. soc_write_index = 0;
  74. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc_data, sizeof(soc_t));
  75. shark_encode_u16(soc_data + sizeof(soc_t), crc16);
  76. shark_timer_post(&_save_backup_timer, 0);
  77. }
  78. #endif
  79. }
  80. void nv_storage_log(void) {
  81. state_debug("soc nv write: %d, %d\n", _soc_write_success, _soc_write_error);
  82. }
  83. static void nv_save_soc_task(shark_timer_t *timer){
  84. if (soc_write_pending == 0) {
  85. return;
  86. }
  87. if (soc_write_index < sizeof(soc_data)){
  88. if (AT24CXX_Write(SOC_ADDR + SOC_SIZE * soc_write_backup_index + soc_write_index, soc_data + soc_write_index, 1) == 1) {
  89. soc_write_index ++;
  90. _soc_write_success ++;
  91. }else {
  92. _soc_write_error ++;
  93. }
  94. }else {
  95. soc_write_index = 0;
  96. soc_write_backup_index ++;
  97. if (soc_write_backup_index == 2){
  98. soc_write_pending = 0;
  99. sys_debug("write soc to nv OK\n");
  100. return;
  101. }
  102. }
  103. shark_timer_post(&_save_backup_timer, 0);
  104. }
  105. #if 0
  106. static void backup_timer_hander(shark_timer_t *timer){
  107. nv_save_soc_by_backup(1);
  108. }
  109. #endif
  110. static void nv_save_soc_by_backup(int index){
  111. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  112. soc_t *soc = get_soc();
  113. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t));
  114. AT24CXX_Write(nv_addr ,(uint8_t *)soc, sizeof(soc_t));
  115. AT24CXX_Write(nv_addr + sizeof(soc_t),(uint8_t *)&crc16, sizeof(crc16));
  116. }
  117. void nv_save_all_soc(void){
  118. nv_save_soc_by_backup(0);
  119. nv_save_soc_by_backup(1);
  120. }
  121. void nv_erase_soc(int index){
  122. uint8_t data = 0xFF;
  123. for (int i = 0; i < sizeof(soc_t) + sizeof(uint16_t); i++){
  124. AT24CXX_Write(SOC_ADDR + SOC_SIZE * index + i, &data, 1);
  125. }
  126. }
  127. int nv_restore_soc_by_backup(int index, soc_t *soc){
  128. uint16_t crc_nv;
  129. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  130. AT24CXX_Read(nv_addr , (uint8_t *)soc, sizeof(soc_t));
  131. AT24CXX_Read(nv_addr + sizeof(soc_t), (uint8_t *)&crc_nv, sizeof(crc_nv));
  132. if (shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t)) != crc_nv){
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. int nv_restore_soc(void){
  138. soc_t soc0, soc1;
  139. int success0, success1;
  140. success0 = nv_restore_soc_by_backup(0, &soc0);
  141. success1 = nv_restore_soc_by_backup(1, &soc1);
  142. if (success0 == 0 || success1 == 0){
  143. if (success0 == 0){
  144. *get_soc() = soc0;
  145. }else if (success1 == 0){
  146. *get_soc() = soc1;
  147. }
  148. if (success1 != 0){
  149. nv_save_soc_by_backup(1);
  150. }
  151. if (success0 != 0){
  152. nv_save_soc_by_backup(0);
  153. }
  154. }
  155. sys_debug("soc bk0=%d, bk1=%d\n", success0, success1);
  156. return ((success0 == 0) || (success1 == 0))?0:-1;
  157. }