ota_message.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. uint32_t *ptr=(uint32_t *)&ota_message;
  40. if(*ptr == 1U)
  41. {
  42. return 0;
  43. }
  44. else return -1;
  45. }
  46. OTA_MESSAGE *get_config_params()
  47. {
  48. return &ota_message;
  49. }
  50. /*
  51. * 函数名:void clear_ota_message_config_block(void)
  52. * 输入参数:无
  53. * 输出参数:无
  54. * 返回值:无
  55. * 函数作用:清除block内包含的ota事件信息
  56. */
  57. void clear_ota_message_config_block(void)
  58. {
  59. W25Q32_Erase64K(OTA_EVENT_BLOCK);//擦除一块区域的大小
  60. }