device_message.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "device_message.h"
  2. #include "gd32f10x.h"
  3. #include "w25q32.h"
  4. static CONFIG_PARAMS g_config_params = {0};
  5. void write_struct_to_flash(uint32_t *data, int size);
  6. /*
  7. * 函数名:save_config_params(CONFIG_PARAMS *params)
  8. * 输入参数:无
  9. * 输出参数:无
  10. * 返回值:无
  11. * 函数作用:加载http或者上位机配置参数
  12. */
  13. int save_config_params(CONFIG_PARAMS *params)
  14. {
  15. if(params == NULL)
  16. return -1;
  17. CONFIG_PARAMS_UNION config_params_union;
  18. memset(&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION));
  19. memcpy(&config_params_union.config_params, params, sizeof(CONFIG_PARAMS));
  20. clear_gateway_config_block();
  21. write_struct_to_flash((uint32_t *)&config_params_union, sizeof(CONFIG_PARAMS_UNION));
  22. return 0;
  23. }
  24. /*
  25. * 函数名:load_config_params()
  26. * 输入参数:无
  27. * 输出参数:无
  28. * 返回值:无
  29. * 函数作用:加载http或者上位机配置参数
  30. */
  31. int load_config_params()
  32. {
  33. CONFIG_PARAMS_UNION config_params_union;
  34. memset(&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION));//清空原先数据
  35. //读出w25q32内部的结构体信息
  36. W25Q32_Read((uint8_t *)&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION)); //从W25Q32中读取结构体数据
  37. memcpy(&g_config_params, &config_params_union.config_params, sizeof(CONFIG_PARAMS));
  38. uint8_t *ptr=(uint8_t *)&g_config_params;
  39. //判断第一位数据是否有效
  40. if(*ptr == 0xF1)
  41. {
  42. return 0;
  43. }
  44. return -1;
  45. }
  46. /*
  47. * 函数名:CONFIG_PARAMS *get_config_params()
  48. * 输入参数:无
  49. * 输出参数:无
  50. * 返回值:无
  51. * 函数作用:获取配置参数
  52. */
  53. CONFIG_PARAMS *get_config_params()
  54. {
  55. return &g_config_params;
  56. }
  57. /*
  58. * 函数名:void write_struct_to_flash(uint32_t *data, int size)
  59. * 输入参数:*data,size、data为往w25q32内写入的数据、size为写入数据大小
  60. * 输出参数:无
  61. * 返回值:无
  62. * 函数作用:往w25q32内写入的数据、一次最多写入一页数据、长度超过时要切换页
  63. */
  64. void write_struct_to_flash(uint32_t *data, int size)
  65. {
  66. uint8_t *pdata = (uint8_t *)data;
  67. //将数据按页写入一次写入256个字节
  68. for (int i = 0; i <= size / 256; i++) {
  69. W25Q32_PageWrite(pdata + i * 256, i);
  70. }
  71. }
  72. /*
  73. * 函数名:clear_gateway_config_block(void)
  74. * 输入参数:无
  75. * 输出参数:无
  76. * 返回值:无
  77. * 函数作用:清除http获取的参数配置
  78. */
  79. void clear_gateway_config_block(void)
  80. {
  81. W25Q32_Erase64K(0);//擦除一块区域的大小
  82. }