gd32_flash.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "gd32f30x.h"
  2. #include "gd32_flash.h"
  3. #include "string.h"
  4. #include "FreeRTOS.h"
  5. #include "task.h"
  6. /*
  7. *gd32f307RCT6的flash内存总共有256K的空间现在要对齐进行相应的分区
  8. *前30K空间用于BOOTLOADER区
  9. *后30K空间用于json字符串数据保存,其中前4字节保存数据长度
  10. *剩下空间用于正常程序
  11. */
  12. #define FMC_READ(addrx) (*(volatile uint32_t *)(uint32_t)(addrx))
  13. void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum);
  14. /*-------------------------------------------------*/
  15. /*函数名:保存数据到flash中 */
  16. /*参 数:params保存的字符串数据 */
  17. /*返回值:无 */
  18. /*前4个字节用于保存字符串长度后面全是字符串数据 TODO:最大长度的限制 */
  19. /*-------------------------------------------------*/
  20. int save_config_params(char *params)
  21. {
  22. uint32_t paramsLen = strlen(params);
  23. volatile uint32_t flashDataLen = FMC_READ(FMC_WRITE_START_ADDR);
  24. if (paramsLen % 4 != 0)
  25. {
  26. paramsLen = (paramsLen / 4 + 1) * 4;
  27. }
  28. if (flashDataLen != paramsLen)
  29. {
  30. GD32_WriteFlash(FMC_WRITE_START_ADDR, (uint32_t *)&paramsLen, 4);
  31. GD32_WriteFlash(FMC_WRITE_START_ADDR + 4, (uint32_t *)params, paramsLen);
  32. return 1;
  33. }
  34. for (int i = 1; i < paramsLen / 4; i++)
  35. {
  36. uint32_t flashWord = FMC_READ(FMC_WRITE_START_ADDR + i * 4);
  37. uint32_t paramsWord = *(uint32_t *)(params + (i - 1) * 4);
  38. vTaskDelay(100); // 解决flash速度缓慢判断错误问题
  39. if (flashWord != paramsWord)
  40. {
  41. GD32_WriteFlash(FMC_WRITE_START_ADDR + i * 4, &paramsWord, 4);
  42. }
  43. }
  44. }
  45. /*-------------------------------------------------*/
  46. /*函数名:从flash中读出字符串数据 */
  47. /*参 数:params保存的字符串数据 */
  48. /*返回值:无 */
  49. /*前4个字节用于保存字符串长度后面全是字符串数据 */
  50. /*-------------------------------------------------*/
  51. int read_data_from_flash(void *buffer)
  52. {
  53. uint32_t length = FMC_READ(FMC_WRITE_START_ADDR);
  54. vTaskDelay(100);
  55. if (length == 0xFFFFFFFF)
  56. {
  57. return -1;
  58. }
  59. for (uint32_t i = 0; i < length; i += 4)
  60. {
  61. *(uint32_t *)((uint8_t *)buffer + i) = FMC_READ(FMC_WRITE_START_ADDR + 4 + i);
  62. }
  63. return 0;
  64. }
  65. /*-------------------------------------------------*/
  66. /*函数名:擦除FLASH */
  67. /*参 数:start:擦除起始扇区 num:擦几个扇区 */
  68. /*返回值:无 */
  69. /*-------------------------------------------------*/
  70. void GD32_EraseFlash(uint16_t start, uint16_t num)
  71. {
  72. uint16_t i;
  73. fmc_unlock();
  74. for (i = 0; i < num; i++)
  75. {
  76. fmc_page_erase((0x08000000 + start * 1024) + (1024 * i));
  77. }
  78. fmc_lock();
  79. }
  80. /*---------------------------------------------------------------------*/
  81. /*函数名:写入FLASH */
  82. /*参 数:saddr:写入地址 wdata:写入数据指针 wnum:写入多少个字节 */
  83. /*返回值:无 */
  84. /*---------------------------------------------------------------------*/
  85. void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum)
  86. {
  87. fmc_unlock();
  88. while (wnum > 0)
  89. {
  90. fmc_word_program(saddr, *wdata);
  91. wnum -= 4;
  92. saddr += 4;
  93. wdata++;
  94. }
  95. fmc_lock();
  96. }
  97. void gd32_flash_test()
  98. {
  99. char *p = "hello,word/r/n";
  100. save_config_params(p);
  101. }