nv_storage.c 863 B

12345678910111213141516171819202122232425262728293031323334
  1. #include "nv_storage.h"
  2. #include "bsp/AT24CXX.h"
  3. #include "app/sox/soc.h"
  4. #include "libs/logger.h"
  5. #include "libs/shark_utils.h"
  6. #define SOC_ADDR 0
  7. void nv_save_soc(void){
  8. soc_t *soc = get_soc();
  9. uint16_t crc16 = shark_crc16_update(0, (const u8 *)soc, sizeof(soc_t));
  10. u64 start_time = shark_get_mseconds();
  11. AT24CXX_Write(SOC_ADDR ,(uint8_t *)soc, sizeof(soc_t));
  12. AT24CXX_Write(SOC_ADDR + sizeof(soc_t),(uint8_t *)&crc16, sizeof(crc16));
  13. printf("write e2rom time %lld\n", (shark_get_mseconds() - start_time));
  14. }
  15. int nv_restore_soc(void){
  16. soc_t soc;
  17. AT24CXX_Read(SOC_ADDR , (uint8_t *)&soc, sizeof(soc_t));
  18. uint16_t crc16 = shark_crc16_update(0, (const u8 *)&soc, sizeof(soc_t));
  19. uint16_t crc_nv;
  20. AT24CXX_Read(SOC_ADDR + sizeof(soc_t), (uint8_t *)&crc_nv, sizeof(crc_nv));
  21. if (crc_nv != crc16) {
  22. return -1;
  23. }
  24. *get_soc() = soc;
  25. return 0;
  26. }