gd_ota_flash.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "gd_ota_flash.h"
  2. #include "gd32f10x_fmc.h"
  3. #include "systick.h"
  4. #include "string.h"
  5. void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum);
  6. void GD32_EraseFlash(uint16_t start, uint16_t num);
  7. //读取soft版本信息
  8. int read_soft_version()
  9. {
  10. }
  11. void write_soft_version(BOOT_MESSAGE *save_message)
  12. {
  13. GD32_EraseFlash(FMC_Page,1);
  14. GD32_WriteFlash(FMC_WRITE_START_ADDR,(uint32_t *)save_message,sizeof(BOOT_MESSAGE));
  15. }
  16. /*-------------------------------------------------*/
  17. /*函数名:擦除FLASH */
  18. /*参 数:start:擦除起始扇区 num:擦几个扇区 */
  19. /*返回值:无 */
  20. /*-------------------------------------------------*/
  21. void GD32_EraseFlash(uint16_t start, uint16_t num)
  22. {
  23. uint16_t i;
  24. fmc_unlock();
  25. for (i = 0; i < num; i++)
  26. {
  27. fmc_page_erase((0x08000000 + start * 1024) + (1024 * i));
  28. }
  29. fmc_lock();
  30. }
  31. /*---------------------------------------------------------------------*/
  32. /*函数名:写入FLASH */
  33. /*参 数:saddr:写入地址 wdata:写入数据指针 wnum:写入多少个字节 */
  34. /*返回值:无 */
  35. /*---------------------------------------------------------------------*/
  36. void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum)
  37. {
  38. fmc_unlock();
  39. while (wnum > 0)
  40. {
  41. fmc_word_program(saddr, *wdata);
  42. wnum -= 4;
  43. saddr += 4;
  44. wdata++;
  45. }
  46. fmc_lock();
  47. }
  48. //读取GD32flash
  49. void GD32_READ_OTA(BOOT_MESSAGE *READ)
  50. {
  51. uint32_t data=FMC_READ(FMC_WRITE_START_ADDR);
  52. delay_1ms(10);
  53. memcpy(READ,&data,4);
  54. }
  55. //对系统存储版本信息进行初始化
  56. void soft_init()
  57. {
  58. BOOT_MESSAGE *currentBoot=malloc(sizeof(BOOT_MESSAGE));
  59. GD32_READ_OTA(currentBoot);
  60. if(currentBoot->VERSION_H ==0 && currentBoot->VERSION_L==0 && currentBoot->VERSION_M==0 )
  61. {
  62. currentBoot->VERSION_H=1;currentBoot->VERSION_L=0;currentBoot->VERSION_M=0;currentBoot->UP_FLAG=0;
  63. }
  64. write_soft_version(currentBoot);
  65. free(currentBoot);
  66. }