ota_message.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "ota_message.h"
  2. #include "gd32f10x.h"
  3. #include "w25q32.h"
  4. #include <stdio.h>
  5. static OTA_MESSAGE ota_message = {0};
  6. void clear_ota_message_config_block(void);
  7. void write_ota_message_to_flash(uint32_t *data, int size){
  8. uint8_t *pdata = (uint8_t *)data;
  9. W25Q32_PageWrite(pdata,256*OTA_EVENT_BLOCK);//写在第二扇区的第一页中
  10. }
  11. /*
  12. * 函数名:save_ota_message_config_params(OTA_MESSAGE *params)
  13. * 输入参数:无
  14. * 输出参数:无
  15. * 返回值:无
  16. * 函数作用:保存ota事件的信息
  17. */
  18. int save_ota_message_config_params(OTA_MESSAGE *params)
  19. {
  20. if(params == NULL)
  21. return -1;
  22. memset(&ota_message, 0, sizeof(OTA_MESSAGE));
  23. memcpy(&ota_message, params, sizeof(OTA_MESSAGE));
  24. clear_ota_message_config_block();
  25. write_ota_message_to_flash((uint32_t *)&ota_message,sizeof(OTA_MESSAGE));
  26. return 0;
  27. }
  28. /*
  29. * 函数名:load_config_params(CONFIG_PARAMS *params)
  30. * 输入参数:无
  31. * 输出参数:无
  32. * 返回值:ota升级标志是否有效 0有效 -1无效
  33. * 函数作用:从w25q32中加载ota信息
  34. */
  35. int load_ota_message_config_params()
  36. {
  37. memset(&ota_message, 0, sizeof(OTA_MESSAGE));//清空原先数据
  38. // W25Q32_Read((uint8_t *)&ota_message, OTA_EVENT_BLOCK*64*1024, sizeof(OTA_MESSAGE)); //从W25Q32中读取结构体数据
  39. FLASH_Read(OTA_EVENT_START_ADDR,&ota_message,sizeof(OTA_MESSAGE));
  40. uint32_t *ptr=(uint32_t *)&ota_message;
  41. if(*ptr == 1U)
  42. {
  43. return 0;
  44. }
  45. else return -1;
  46. }
  47. OTA_MESSAGE *get_config_params()
  48. {
  49. return &ota_message;
  50. }
  51. /*
  52. * 函数名:void clear_ota_message_config_block(void)
  53. * 输入参数:无
  54. * 输出参数:无
  55. * 返回值:无
  56. * 函数作用:清除block内包含的ota事件信息
  57. */
  58. void clear_ota_message_config_block(void)
  59. {
  60. GD32_EraseFlash(210,1);//擦除一块区域的大小
  61. }