Contributed by WT_0213, curated by Ai-Thinker
Continuing from the previous post, save the WiFi name and password into storage.
Use easyflash to save the WiFi name and password.
Create the storage-related code.
storage/storage.h
c
#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
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;
}Modify web/mlwip_https.c to add the following:
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();Modify proj.conf to add the following:
html
## easy flash
set(CONFIG_PARTITION 1)
set(CONFIG_BFLB_MTD 1)
set(CONFIG_EASYFLASH4 1)Modify CMakeLists.txt to add the following:
html
...
sdk_add_include_directories(storage)
...
target_sources(app PRIVATE
storage/storage.c
web/cJSON.c
web/mlwip_https.c
)Some comments were added to the flash_prog_cfg.ini configuration file.
html
#***************************************************#
## 固件烧录配置 #
## 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)
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*.binFor easyflash4 related issues, you can refer to:
[FAQ] easyflash4 usage problems and solutions https://bbs.ai-thinker.com/forum.php?mod=viewthread&tid=43763
If you think it's not bad, you can give it a
+ 1.
Have questions?
For other questions, please visit the unified discussion area: Ai-Thinker Discussions

