cfg.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "cfg.h"
  2. #include "uart.h"
  3. #include "flash.h"
  4. #include <string.h>
  5. static uint64_t reflectionBuff[(sizeof(Config_type)>>3)+1];
  6. Config_type *config = (Config_type *)reflectionBuff;
  7. static void Factory_reset(void)
  8. {
  9. config->magic = CONFIG_MAGIC;
  10. config->addr = DEFAULT_ADDR;
  11. config->br_index = BaudRate_115200;
  12. //config->App2Size = 0;
  13. //config->App2Crc = 0;
  14. //config->IapFlag = 0;
  15. }
  16. int Config_Init(void)
  17. {
  18. //EFLASH_StatusType ret = EFLASH_STATUS_SUCCESS;
  19. //memcpy(reflectionBuff, (void *)CONFIG_ADDRESS, sizeof(Config_type));
  20. FLASH_DRV_Read(&g_Flash_Config,CONFIG_ADDRESS, (uint8_t*)reflectionBuff, sizeof(reflectionBuff));
  21. if (config->magic != CONFIG_MAGIC)
  22. {/* initiliaze config */
  23. memset(reflectionBuff, 0xFF, sizeof(reflectionBuff));
  24. //config->magic = CONFIG_MAGIC;
  25. Factory_reset();
  26. //设备内参, 不加入出厂设置
  27. config->xaxis_threshold = 0.015;
  28. config->yaxis_threshold = 0.015;
  29. config->zaxis_threshold = 0.015;
  30. //
  31. config->IapFlag = Startup_Normal;
  32. config->hw_version = 0x0000;
  33. config->devicetype = 0x0000;
  34. config->deviceid = 0x00;
  35. SaveConfig();
  36. }
  37. //Factory_reset();
  38. return 0;
  39. }
  40. int SaveConfig(void)
  41. {
  42. status_t ret = STATUS_SUCCESS;
  43. ret = FLASH_DRV_UnlockCtrl();
  44. if(STATUS_SUCCESS != ret) return -1;
  45. ret = FLASH_DRV_EraseSector(&g_Flash_Config, CONFIG_ADDRESS, EFLASH_PAGE_SIZE_INIT);
  46. if(STATUS_SUCCESS != ret) return -1;
  47. ret = FLASH_DRV_VerifySection(&g_Flash_Config,CONFIG_ADDRESS, EFLASH_PAGE_SIZE_INIT/8);
  48. if(STATUS_SUCCESS != ret) return -1;
  49. ret = FLASH_DRV_Program(&g_Flash_Config, CONFIG_ADDRESS, sizeof(reflectionBuff), (uint8_t*)reflectionBuff);
  50. FLASH_DRV_LockCtrl();
  51. return 0;
  52. }
  53. int ResetConfig(void)
  54. {
  55. Factory_reset();
  56. //SaveConfig();
  57. return 0;
  58. }