本帖最后由 WT_0213 于 2023-12-7 11:27 编辑
书接上回,将WIFI名称和密码保存到存储当中。
使用easyflash保存WIFI名称和密码
创建 存储相关代码
storage/storage.h
#ifndef __CUSTOM_H_
#define __CUSTOM_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define SSID_KEY "SSID"
#define PASS_KEY "PASS"
#ifndef USER_FLASH_H
#define USER_FLASH_H
void flash_init(void);
void flash_erase_set(char* key, char* value);
char* flash_get_data(char* key, int len);
#endif
#ifdef __cplusplus
}
#endif
#endif /* EVENT_CB_H_ */
storage/storage.c
/**
* @file user_flash.c
* @author your name ([email]you@domain.com[/email])
* @brief
* @version 0.1
* @date 2023-08-07
*
* @copyright Copyright (c) 2023
*
*/
#include "stdio.h"
#include "string.h"
#include "storage.h"
#include "easyflash.h"
#include "log.h"
void flash_init(void)
{
//init easyflash
bflb_mtd_init();
easyflash_init();
}
/**
* @brief
*
* @param key
* @param value
*/
void flash_erase_set(char* key, char* value)
{
size_t len = 0;
int value_len = strlen(value);
ef_set_and_save_env(key, value);
// bflb_flash_read(key, flash_data, strlen(value));
// printf("writer data:%s\r\n", flash_data);
memset(value, 0, strlen(value));
ef_get_env_blob(key, value, value_len, &len);
LOG_W("flash_erase_set %s: %s", key, value);
}
/**
* @brief
*
* @param key
* @return char*
*/
char* flash_get_data(char* key, int len)
{
static char* flash_data = NULL;
flash_data = pvPortMalloc(len);
memset(flash_data, 0, len);
ef_get_env_blob(key, flash_data, len, (size_t)&len);
LOG_W("flash_get_data %s: %s, len:%d\r\n", key, flash_data, strlen(flash_data));
return flash_data;
}
修改web/mlwip_https.c增加如下内容:
#include "storage.h"
#include "wifi_event.h"
#include "log.h"
....
// 将 wifi 信息写入 存储
flash_erase_set(SSID_KEY, ssidValue);
flash_erase_set(PASS_KEY, pwdValue);
// 开启 sta 模式,连接WIFI
// 重启设备
LOG_W("system 2s reset ");
vTaskDelay(2000/portTICK_PERIOD_MS);
GLB_SW_System_Reset();
修改proj.conf增加如下内容:
# easy flash
set(CONFIG_PARTITION 1)
set(CONFIG_BFLB_MTD 1)
set(CONFIG_EASYFLASH4 1)
修改CMakeLists.txt增加如下内容:
...
sdk_add_include_directories(storage)
...
target_sources(app PRIVATE
storage/storage.c
web/cJSON.c
web/mlwip_https.c
)
对 flash_prog_cfg.ini 配置文件进行了一些注释
#***************************************************#
# 固件烧录配置 #
# Firmware burning configuration #
#***************************************************#
[cfg]
# 0:无擦除,1:程序化截面擦除,2:芯片擦除
# 0: no erase, 1:programmed section erase, 2: chip erase
erase = 1
# skip mode set first para is skip addr, second para is skip len, multi-segment region with ; separated
skip_mode = 0x0, 0x0
# 复位下载功能使能(Reset download function enable)
# 0: not use isp mode, #1: isp mode
boot2_isp_mode = 1
# 配置boot2固件,否则无法使用复位烧录功能(Configure boot2 firmware, otherwise the reset burn function cannot be used)
[boot2]
filedir = ./build/build_out/boot2_*.bin
address = 0x000000
# 配置partition固件,这是必要的(Configuring partition firmware is necessary)
[partition]
filedir = ./build/build_out/partition*.bin
address = 0xE000
# 配置应用程序固件地址,需要新建工程时需要修改“Project_basic” 为新工程的名字,否则可能会导致烧录失败
#(To configure the application firmware address, when creating a new project, it is necessary to modify "Project_Basic" to the name of the new project, otherwise it may cause burning failure)
[FW]
filedir = ./build/build_out/smart_config_$(CHIPNAME).bin
address = @partition
address = 0x10000
# [mfg]
# filedir = ./build/build_out/mfg*.bin
easyflash4相关问题可以参考。
[答疑] easyflash4 使用问题及解决方法
https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=43763
如果觉得还不错的话,可以 给个 + 1 哦。