flash.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "flash.h"
  5. #include "stm32f1xx_hal.h"
  6. #include <stdint.h>
  7. /************************************************************************************************
  8. * Config *
  9. ************************************************************************************************/
  10. /************************************************************************************************
  11. * Data structs *
  12. ************************************************************************************************/
  13. /************************************************************************************************
  14. * implements *
  15. ************************************************************************************************/
  16. FLASH_ProcessTypeDef p_flash;
  17. // 解除写保护,清除fmc特定寄存器
  18. void fmc_clear_flag_star(void)
  19. {
  20. HAL_FLASH_Unlock(); // 解锁
  21. HAL_FLASH_OB_Unlock(); // FLASH擦除期间,必须禁止数据缓存
  22. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  23. }
  24. // 设置写保护,清除fmc特定寄存器
  25. void fmc_clear_flag_end(void)
  26. {
  27. HAL_FLASH_OB_Lock(); // FLASH擦除结束,开启数据缓存
  28. HAL_FLASH_Lock(); // 上锁
  29. }
  30. // 读取一字节, 读取无需考虑字节对齐
  31. uint8_t fmc_read_byte(uint32_t read_addr)
  32. {
  33. return *(__IO uint8_t *)read_addr;
  34. }
  35. // 读取两字节, 读取需考虑字节对齐
  36. uint16_t fmc_read_half_word(uint32_t read_addr)
  37. {
  38. return *(__IO uint16_t *)read_addr;
  39. }
  40. // 读取N字节, 读取必须需考虑字节对齐
  41. uint8_t fmc_read_n_half_word(uint32_t read_addr, uint16_t *buff, uint16_t count)
  42. {
  43. uint16_t i = 0;
  44. __IO uint16_t *addr = (__IO uint16_t *)read_addr;
  45. if (read_addr % 2 != 0)
  46. return 1; // 错误返回
  47. for (i = 0; i < count; i++)
  48. {
  49. *buff++ = *addr++;
  50. }
  51. return 0;
  52. }
  53. // 读取1页, 地址必须对齐页首
  54. uint8_t fmc_read_one_page(uint32_t read_addr, uint16_t *buff)
  55. {
  56. uint16_t i = 0;
  57. __IO uint16_t *addr = (__IO uint16_t *)read_addr;
  58. if (read_addr % FMC_PAGE_SIZE != 0)
  59. return 1; // 错误返回
  60. for (i = 0; i < 1024; i++)
  61. {
  62. *buff++ = *addr++;
  63. }
  64. return 0;
  65. }
  66. // 擦出N页, 地址必须对齐页首
  67. uint8_t fmc_erase_pages(uint32_t erase_addr, uint16_t len)
  68. {
  69. uint16_t i = 0, j = 0;
  70. FLASH_EraseInitTypeDef p_erase_init;
  71. uint32_t page_error = 0;
  72. p_erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
  73. p_erase_init.Banks = FLASH_BANK_1;
  74. p_erase_init.NbPages = 1;
  75. uint32_t *addr = (uint32_t *)erase_addr;
  76. if (erase_addr % FMC_PAGE_SIZE != 0)
  77. return 1; // 错误返回
  78. for (i = 0; i < len; i++)
  79. {
  80. p_erase_init.PageAddress = erase_addr + FMC_PAGE_SIZE * i;
  81. if (HAL_FLASHEx_Erase(&p_erase_init, &page_error) != HAL_OK)
  82. return 1;
  83. for (j = 0; j < 512; j++)
  84. {
  85. if (*addr != 0xFFFFFFFF)
  86. return 1;
  87. else
  88. addr++;
  89. }
  90. }
  91. return 0;
  92. }
  93. // 只写入,写入前需擦除,地址必须两字节对齐
  94. uint8_t fmc_write_n_half_word(uint32_t write_addr, uint16_t *buff, uint16_t len)
  95. {
  96. uint32_t end_addr = write_addr + len * 2;
  97. if (write_addr % 2 != 0)
  98. return 1;
  99. while (write_addr < end_addr)
  100. {
  101. HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, write_addr, *buff); // 写入数据
  102. if (*(__IO uint16_t *)write_addr != *buff)
  103. {
  104. return 1;
  105. }
  106. write_addr += 2;
  107. buff++;
  108. }
  109. return 0;
  110. }
  111. uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t *pbuff, uint32_t len)
  112. {
  113. uint16_t temp = 0;
  114. uint16_t crc = init_value;
  115. for (uint32_t i = 0; i < len; i++)
  116. {
  117. for (uint32_t j = 0; j < 8; j++)
  118. {
  119. temp = ((pbuff[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8);
  120. crc <<= 1;
  121. if (temp != 0)
  122. {
  123. crc ^= 0x1021;
  124. }
  125. }
  126. }
  127. return crc;
  128. }