flash.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /************************************************************************************************
  2. * Include *
  3. ************************************************************************************************/
  4. #include "flash.h"
  5. #include "stm32f4xx.h"
  6. /************************************************************************************************
  7. * Config *
  8. ************************************************************************************************/
  9. /************************************************************************************************
  10. * Data structs *
  11. ************************************************************************************************/
  12. /************************************************************************************************
  13. * implements *
  14. ************************************************************************************************/
  15. uint16_t fmc_get_flash_sector(u32 addr)
  16. {
  17. if (addr < ADDR_FLASH_SECTOR_1)
  18. return FLASH_Sector_0;
  19. else if (addr < ADDR_FLASH_SECTOR_2)
  20. return FLASH_Sector_1;
  21. else if (addr < ADDR_FLASH_SECTOR_3)
  22. return FLASH_Sector_2;
  23. else if (addr < ADDR_FLASH_SECTOR_4)
  24. return FLASH_Sector_3;
  25. else if (addr < ADDR_FLASH_SECTOR_5)
  26. return FLASH_Sector_4;
  27. else if (addr < ADDR_FLASH_SECTOR_6)
  28. return FLASH_Sector_5;
  29. else if (addr < ADDR_FLASH_SECTOR_7)
  30. return FLASH_Sector_6;
  31. else if (addr < ADDR_FLASH_SECTOR_8)
  32. return FLASH_Sector_7;
  33. else if (addr < ADDR_FLASH_SECTOR_9)
  34. return FLASH_Sector_8;
  35. else if (addr < ADDR_FLASH_SECTOR_10)
  36. return FLASH_Sector_9;
  37. else if (addr < ADDR_FLASH_SECTOR_11)
  38. return FLASH_Sector_10;
  39. return FLASH_Sector_11;
  40. }
  41. // 解除写保护,清除fmc特定寄存器
  42. void fmc_clear_flag_star(void)
  43. {
  44. FLASH_Unlock(); // 解锁
  45. FLASH_DataCacheCmd(DISABLE); // FLASH擦除期间,必须禁止数据缓存
  46. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  47. }
  48. // 设置写保护,清除fmc特定寄存器
  49. void fmc_clear_flag_end(void)
  50. {
  51. FLASH_DataCacheCmd(ENABLE); // FLASH擦除结束,开启数据缓存
  52. FLASH_Lock(); // 上锁
  53. }
  54. // 读取一字节, 读取无需考虑字节对齐
  55. uint8_t fmc_read_byte(uint32_t read_addr)
  56. {
  57. return *(vu8 *)read_addr;
  58. }
  59. // 读取两字节, 读取需考虑字节对齐
  60. uint16_t fmc_read_half_word(uint32_t read_addr)
  61. {
  62. return *(vu16 *)read_addr;
  63. }
  64. // 读取N字节, 读取必须需考虑字节对齐
  65. uint8_t fmc_read_n_half_word(uint32_t read_addr, uint16_t *buff, uint16_t count)
  66. {
  67. uint16_t i = 0;
  68. vu16 *addr = (vu16 *)read_addr;
  69. if (read_addr % 2 != 0)
  70. return 1; // 错误返回
  71. for (i = 0; i < count; i++)
  72. {
  73. *buff++ = *addr++;
  74. }
  75. return 0;
  76. }
  77. // 读取1页, 地址必须对齐页首
  78. uint8_t fmc_read_one_page(uint32_t read_addr, uint16_t *buff)
  79. {
  80. uint16_t i = 0;
  81. vu16 *addr = (vu16 *)read_addr;
  82. if (read_addr % FMC_PAGE_SIZE != 0)
  83. return 1; // 错误返回
  84. for (i = 0; i < 1024; i++)
  85. {
  86. *buff++ = *addr++;
  87. }
  88. return 0;
  89. }
  90. // 擦出N页, 地址必须对齐页首
  91. uint8_t fmc_erase_pages(uint32_t erase_addr, uint16_t len)
  92. {
  93. uint8_t status = 0;
  94. uint8_t i = 0, j = 0;
  95. for (i = 0; i < len; i++)
  96. {
  97. status = FLASH_EraseSector(fmc_get_flash_sector(erase_addr), VoltageRange_3);
  98. if (status != FLASH_COMPLETE)
  99. {
  100. return 1;
  101. }
  102. if ((erase_addr >= ADDR_FLASH_SECTOR_0) && (erase_addr < ADDR_FLASH_SECTOR_4))
  103. {
  104. erase_addr += (i * FMC_PAGE_SIZE_16K);
  105. }
  106. else if ((erase_addr >= ADDR_FLASH_SECTOR_4) && (erase_addr < ADDR_FLASH_SECTOR_5))
  107. {
  108. erase_addr += (i * FMC_PAGE_SIZE_64K);
  109. }
  110. else if ((erase_addr >= ADDR_FLASH_SECTOR_4) && (erase_addr < ADDR_FLASH_SECTOR_END))
  111. {
  112. erase_addr += (i * FMC_PAGE_SIZE_128K);
  113. }
  114. else
  115. {
  116. return 0;
  117. }
  118. }
  119. return 0;
  120. // uint16_t i = 0, j = 0;
  121. // uint32_t *addr = (uint32_t *)erase_addr;
  122. // if (erase_addr % FMC_PAGE_SIZE != 0)
  123. // return 1; // 错误返回
  124. // for (i = 0; i < len; i++)
  125. // {
  126. // FLASH_EraseSector(fmc_get_flash_sector(erase_addr), VoltageRange_3);
  127. // for (j = 0; j < 512; j++)
  128. // {
  129. // if (*addr != 0xFFFFFFFF)
  130. // return 1;
  131. // else
  132. // addr++;
  133. // }
  134. // }
  135. // return 0;
  136. }
  137. // 只写入,写入前需擦除,地址必须两字节对齐
  138. uint8_t fmc_write_n_half_word(uint32_t write_addr, uint16_t *buff, uint16_t len)
  139. {
  140. uint32_t end_addr = write_addr + len * 2;
  141. if (write_addr % 2 != 0)
  142. return 1;
  143. while (write_addr < end_addr)
  144. {
  145. FLASH_ProgramHalfWord(write_addr, *buff); // 写入数据
  146. if (*(vu16 *)write_addr != *buff)
  147. {
  148. return 1;
  149. }
  150. write_addr += 2;
  151. buff++;
  152. }
  153. return 0;
  154. }
  155. // uint8_t stm_flash_write(uint32_t write_addr, uint16_t *buff, uint32_t len)
  156. // {
  157. // uint8_t res = 1;
  158. // uint32_t index = 0, temp_addr = write_addr;
  159. // FLASH_Status status = FLASH_COMPLETE;
  160. // for (index = 0; index < len;)
  161. // {
  162. // if (fmc_read_half_word(temp_addr) != 0xFFFF)
  163. // {
  164. // status = FLASH_EraseSector(fmc_get_flash_sector(temp_addr), VoltageRange_3);
  165. // if (status != FLASH_COMPLETE)
  166. // {
  167. // res = 0;
  168. // break;
  169. // }
  170. // }
  171. // else
  172. // {
  173. // index++;
  174. // temp_addr += 2;
  175. // }
  176. // }
  177. // if (status == FLASH_COMPLETE)
  178. // {
  179. // for (index = 0; index < len; index++)
  180. // {
  181. // if (FLASH_ProgramHalfWord(write_addr, buff[index]) != FLASH_COMPLETE) // 写入数据
  182. // {
  183. // res = 0;
  184. // // 写入异常
  185. // break;
  186. // }
  187. // write_addr += 2;
  188. // }
  189. // }
  190. // return res;
  191. // }
  192. uint16_t firmware_crc16_ccitt_false(uint16_t init_value, uint8_t *pbuff, uint32_t len)
  193. {
  194. uint16_t temp = 0;
  195. uint16_t crc = init_value;
  196. for (int i = 0; i < len; i++)
  197. {
  198. for (int j = 0; j < 8; j++)
  199. {
  200. temp = ((pbuff[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8);
  201. crc <<= 1;
  202. if (temp != 0)
  203. {
  204. crc ^= 0x1021;
  205. }
  206. }
  207. }
  208. return crc;
  209. }