1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "device_message.h"
- #include "gd32f10x.h"
- #include "w25q32.h"
- static CONFIG_PARAMS g_config_params = {0};
- void write_struct_to_flash(uint32_t *data, int size);
- /*
- * 函数名:save_config_params(CONFIG_PARAMS *params)
- * 输入参数:无
- * 输出参数:无
- * 返回值:无
- * 函数作用:加载http或者上位机配置参数
- */
- int save_config_params(CONFIG_PARAMS *params)
- {
- if(params == NULL)
- return -1;
-
- CONFIG_PARAMS_UNION config_params_union;
- memset(&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION));
- memcpy(&config_params_union.config_params, params, sizeof(CONFIG_PARAMS));
- clear_gateway_config_block();
- write_struct_to_flash((uint32_t *)&config_params_union, sizeof(CONFIG_PARAMS_UNION));
- return 0;
- }
- /*
- * 函数名:load_config_params()
- * 输入参数:无
- * 输出参数:无
- * 返回值:无
- * 函数作用:加载http或者上位机配置参数
- */
- int load_config_params()
- {
- CONFIG_PARAMS_UNION config_params_union;
- memset(&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION));//清空原先数据
- //读出w25q32内部的结构体信息
- W25Q32_Read((uint8_t *)&config_params_union, 0, sizeof(CONFIG_PARAMS_UNION)); //从W25Q32中读取结构体数据
- memcpy(&g_config_params, &config_params_union.config_params, sizeof(CONFIG_PARAMS));
- uint8_t *ptr=(uint8_t *)&g_config_params;
- //判断第一位数据是否有效
- if(*ptr == 0xF1)
- {
- return 0;
- }
- return -1;
- }
- /*
- * 函数名:CONFIG_PARAMS *get_config_params()
- * 输入参数:无
- * 输出参数:无
- * 返回值:无
- * 函数作用:获取配置参数
- */
- CONFIG_PARAMS *get_config_params()
- {
- return &g_config_params;
- }
- /*
- * 函数名:void write_struct_to_flash(uint32_t *data, int size)
- * 输入参数:*data,size、data为往w25q32内写入的数据、size为写入数据大小
- * 输出参数:无
- * 返回值:无
- * 函数作用:往w25q32内写入的数据、一次最多写入一页数据、长度超过时要切换页
- */
- void write_struct_to_flash(uint32_t *data, int size)
- {
- uint8_t *pdata = (uint8_t *)data;
- //将数据按页写入一次写入256个字节
- for (int i = 0; i <= size / 256; i++) {
- W25Q32_PageWrite(pdata + i * 256, i);
- }
- }
- /*
- * 函数名:clear_gateway_config_block(void)
- * 输入参数:无
- * 输出参数:无
- * 返回值:无
- * 函数作用:清除http获取的参数配置
- */
- void clear_gateway_config_block(void)
- {
- W25Q32_Erase64K(0);//擦除一块区域的大小
- }
|