nv_storage.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. soc_write_backup_index = 0;
  100. sys_debug("write soc to nv OK\n");
  101. return;
  102. }
  103. }
  104. shark_timer_post(&_save_backup_timer, 0);
  105. }
  106. #if 0
  107. static void backup_timer_hander(shark_timer_t *timer){
  108. nv_save_soc_by_backup(1);
  109. }
  110. #endif
  111. static void nv_save_soc_by_backup(int index){
  112. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  113. soc_t *soc = get_soc();
  114. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t));
  115. AT24CXX_Write(nv_addr ,(uint8_t *)soc, sizeof(soc_t));
  116. AT24CXX_Write(nv_addr + sizeof(soc_t),(uint8_t *)&crc16, sizeof(crc16));
  117. }
  118. static void stop_writer(void) {
  119. shark_timer_cancel(&_save_backup_timer);
  120. soc_write_index = 0;
  121. soc_write_backup_index = 0;
  122. }
  123. void nv_save_all_soc(void){
  124. stop_writer();
  125. nv_save_soc_by_backup(0);
  126. nv_save_soc_by_backup(1);
  127. }
  128. void nv_erase_soc(int index){
  129. uint8_t data = 0xFF;
  130. stop_writer();
  131. for (int i = 0; i < sizeof(soc_t) + sizeof(uint16_t); i++){
  132. AT24CXX_Write(SOC_ADDR + SOC_SIZE * index + i, &data, 1);
  133. }
  134. }
  135. int nv_restore_soc_by_backup(int index, soc_t *soc){
  136. uint16_t crc_nv;
  137. uint16_t nv_addr = SOC_ADDR + SOC_SIZE * index;
  138. AT24CXX_Read(nv_addr , (uint8_t *)soc, sizeof(soc_t));
  139. AT24CXX_Read(nv_addr + sizeof(soc_t), (uint8_t *)&crc_nv, sizeof(crc_nv));
  140. if (shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t)) != crc_nv){
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. int nv_restore_soc(void){
  146. soc_t soc0, soc1;
  147. int success0, success1;
  148. success0 = nv_restore_soc_by_backup(0, &soc0);
  149. success1 = nv_restore_soc_by_backup(1, &soc1);
  150. if (success0 == 0 || success1 == 0){
  151. if (success0 == 0){
  152. *get_soc() = soc0;
  153. }else if (success1 == 0){
  154. *get_soc() = soc1;
  155. }
  156. if (success1 != 0){
  157. nv_save_soc_by_backup(1);
  158. }
  159. if (success0 != 0){
  160. nv_save_soc_by_backup(0);
  161. }
  162. }
  163. sys_debug("soc bk0=%d, bk1=%d\n", success0, success1);
  164. return ((success0 == 0) || (success1 == 0))?0:-1;
  165. }