You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

3 months ago
  1. #include "zflash.h"
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "zbase.h"
  6. #include "zlog.h"
  7. static uint32_t* _rawstartadd;
  8. static uint32_t* _defaultdata;
  9. static uint32_t _rawsize;
  10. static bool _is_first_run = false;
  11. static uint32_t* _flashadd;
  12. static uint32_t _flashSector;
  13. static bool _xs_check_raw_data() {
  14. uint32_t checksum = 0;
  15. if (_rawstartadd[0] != FLASH_MASK_VAL) {
  16. return false;
  17. }
  18. for (uint32_t i = 0; i < _rawsize - 1; i++) {
  19. checksum += _rawstartadd[i];
  20. }
  21. if (checksum != _rawstartadd[_rawsize - 1]) {
  22. return false;
  23. }
  24. return true;
  25. }
  26. static HAL_StatusTypeDef _flash_erase(void) {
  27. HAL_StatusTypeDef status;
  28. uint32_t sector_error_point;
  29. FLASH_EraseInitTypeDef flash_erase_structer = {
  30. //
  31. .TypeErase = FLASH_TYPEERASE_SECTORS, //
  32. .Sector = _flashSector, //
  33. .NbSectors = 1, //
  34. .VoltageRange = FLASH_VOLTAGE_RANGE_3 //
  35. };
  36. HAL_FLASH_Unlock(); // 解锁
  37. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); // 清除一些错误标志
  38. status = HAL_FLASHEx_Erase(&flash_erase_structer, &sector_error_point);
  39. if (status != HAL_OK) {
  40. ZLOGE("flash", "erase error");
  41. }
  42. HAL_FLASH_Lock(); //
  43. return status;
  44. }
  45. /*******************************************************************************
  46. * EXTERN *
  47. *******************************************************************************/
  48. void zflash_init(uint32_t* flashadd, uint32_t flashSector, uint32_t* rawstartadd, uint32_t rawsize) {
  49. _flashadd = flashadd;
  50. _flashSector = flashSector;
  51. _rawstartadd = rawstartadd;
  52. _defaultdata = NULL;
  53. _rawsize = rawsize;
  54. // 读取flash数据
  55. memcpy(_rawstartadd, (uint32_t*)(_flashadd), _rawsize * 4);
  56. #if 0
  57. // 校验数据
  58. if (_xs_check_raw_data()) {
  59. return;
  60. }
  61. _is_first_run = true;
  62. zflash_factory_reset();
  63. #endif
  64. }
  65. bool zflash_check(void) { return _xs_check_raw_data(); }
  66. bool zflash_set_default_data(uint32_t* defaultdata) {
  67. _defaultdata = defaultdata;
  68. return true;
  69. }
  70. bool zflash_is_first_run(void) { return _is_first_run; }
  71. uint32_t zcompute_checksum(uint32_t* data, uint32_t size) {
  72. uint32_t checksum = 0;
  73. for (uint32_t i = 0; i < size; i++) {
  74. checksum += data[i];
  75. }
  76. return checksum;
  77. }
  78. bool zflash_factory_reset(void) {
  79. ZLOGI("flash", "factory reset");
  80. memcpy(_rawstartadd, _defaultdata, _rawsize * 4);
  81. zflash_flush();
  82. return true;
  83. }
  84. bool zflash_flush(void) {
  85. _rawstartadd[0] = FLASH_MASK_VAL;
  86. _rawstartadd[_rawsize - 1] = zcompute_checksum(_rawstartadd, _rawsize - 1);
  87. _flash_erase();
  88. HAL_FLASH_Unlock(); //
  89. HAL_StatusTypeDef status;
  90. for (uint32_t i = 0; i < _rawsize; i++) {
  91. status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, _flashadd + i * 4, _rawstartadd[i]);
  92. if (status != HAL_OK) {
  93. ZLOGE("flash", "write error");
  94. }
  95. }
  96. HAL_FLASH_Lock(); //
  97. return true;
  98. }