nv_storage.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "app/nv_storage.h"
  2. #include "bsp/fmc_flash.h"
  3. #include "libs/crc16.h"
  4. static void mc_config_default(void);
  5. static mc_config_t g_config;
  6. void config_set_hall_offset(s16 offset) {
  7. g_config.hall_offset = offset;
  8. }
  9. mc_config_t *mc_config_get(void) {
  10. return &g_config;
  11. }
  12. void store_hall_table(u16 *hall_table) {
  13. memcpy((char *)g_config.hall_table, (char *)hall_table, sizeof(g_config.hall_table));
  14. store_config();
  15. }
  16. void store_hall_offset(s16 offset) {
  17. g_config.hall_offset = offset;
  18. store_config();
  19. }
  20. void store_config(void) {
  21. u16 crc = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
  22. g_config.crc16 = crc;
  23. fmc_write_data(0, (u8 *)&g_config, sizeof(g_config));
  24. fmc_write_data(1, (u8 *)&g_config, sizeof(g_config));
  25. }
  26. void restore_config(void) {
  27. fmc_read_data(0, (u8 *)&g_config, sizeof(g_config));
  28. u16 crc0 = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
  29. if (crc0 != g_config.crc16) {
  30. fmc_read_data(1, (u8 *)&g_config, sizeof(g_config));
  31. crc0 = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
  32. if (crc0 != g_config.crc16) {
  33. mc_config_default();
  34. return;
  35. }
  36. fmc_write_data(0, (u8 *)&g_config, sizeof(g_config));
  37. }else {
  38. fmc_write_data(1, (u8 *)&g_config, sizeof(g_config));
  39. }
  40. }
  41. static void mc_config_default(void) {
  42. g_config.hall_offset = 0;
  43. for (int i = 0; i < 3; i++) {
  44. g_config.phase_op[i].op_i_k = 1;
  45. g_config.phase_op[i].op_i_offset = 0;;
  46. }
  47. }