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.

80 lines
2.5 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "flash_map.hpp"
  4. #include "main.h"
  5. #include "project_configs.h"
  6. static uint32_t m_sector_num = 0;
  7. namespace iflytop {
  8. uint32_t* zsimple_flash_get_add(int sector_num) {
  9. switch (sector_num) {
  10. case 0:
  11. return (uint32_t*)Z_FLASH_SECTOR_00_ADDR;
  12. case 1:
  13. return (uint32_t*)Z_FLASH_SECTOR_01_ADDR;
  14. case 2:
  15. return (uint32_t*)Z_FLASH_SECTOR_02_ADDR;
  16. case 3:
  17. return (uint32_t*)Z_FLASH_SECTOR_03_ADDR;
  18. case 4:
  19. return (uint32_t*)Z_FLASH_SECTOR_04_ADDR;
  20. case 5:
  21. return (uint32_t*)Z_FLASH_SECTOR_05_ADDR;
  22. case 6:
  23. return (uint32_t*)Z_FLASH_SECTOR_06_ADDR;
  24. case 7:
  25. return (uint32_t*)Z_FLASH_SECTOR_07_ADDR;
  26. case 8:
  27. return (uint32_t*)Z_FLASH_SECTOR_08_ADDR;
  28. case 9:
  29. return (uint32_t*)Z_FLASH_SECTOR_09_ADDR;
  30. case 10:
  31. return (uint32_t*)Z_FLASH_SECTOR_10_ADDR;
  32. case 11:
  33. return (uint32_t*)Z_FLASH_SECTOR_11_ADDR;
  34. default:
  35. assert_param(0);
  36. }
  37. return 0;
  38. }
  39. static HAL_StatusTypeDef pri_earse_sector() {
  40. HAL_StatusTypeDef status;
  41. uint32_t sector_error_point;
  42. FLASH_EraseInitTypeDef flash_erase_structer = {
  43. //
  44. .TypeErase = FLASH_TYPEERASE_SECTORS, //
  45. .Sector = m_sector_num, //
  46. .NbSectors = 1, //
  47. .VoltageRange = FLASH_VOLTAGE_RANGE_3 //
  48. };
  49. HAL_FLASH_Unlock(); // ����
  50. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); // ����һЩ������־
  51. status = HAL_FLASHEx_Erase(&flash_erase_structer, &sector_error_point);
  52. HAL_FLASH_Lock(); // ����
  53. return status;
  54. }
  55. void zsimple_flash_init(uint32_t sector_num) { m_sector_num = sector_num; }
  56. void zsimple_flash_read(uint8_t* data, size_t len) { memcpy(data, zsimple_flash_get_add(m_sector_num), len); }
  57. void zsimple_flash_write(const uint8_t* data, size_t len) {
  58. pri_earse_sector();
  59. uint32_t* from = (uint32_t*)data;
  60. uint32_t* to = (uint32_t*)zsimple_flash_get_add(m_sector_num);
  61. uint32_t writesize = len / 4;
  62. //HAL_StatusTypeDef status;
  63. HAL_FLASH_Unlock(); // ����
  64. for (size_t i = 0; i < writesize; i++) {
  65. //status =
  66. HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)to, *from);
  67. from++;
  68. to++;
  69. }
  70. HAL_FLASH_Lock(); // ����
  71. }
  72. } // namespace iflytop