drv_flash.c 6.0 KB

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