nv_storage.c 1014 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. void nv_erase(void){
  16. uint8_t data = 0xFF;
  17. for (int i = 0; i < sizeof(soc_t) + sizeof(uint16_t); i++){
  18. AT24CXX_Write(SOC_ADDR + i, &data, 1);
  19. }
  20. }
  21. int nv_restore_soc(void){
  22. soc_t soc;
  23. AT24CXX_Read(SOC_ADDR , (uint8_t *)&soc, sizeof(soc_t));
  24. uint16_t crc16 = shark_crc16_update(0, (const u8 *)&soc, sizeof(soc_t));
  25. uint16_t crc_nv;
  26. AT24CXX_Read(SOC_ADDR + sizeof(soc_t), (uint8_t *)&crc_nv, sizeof(crc_nv));
  27. if (crc_nv != crc16) {
  28. return -1;
  29. }
  30. *get_soc() = soc;
  31. return 0;
  32. }