涉及到具体项目开发的时候,用户参数的保存与修改是最基本的需求;
RTOS3.3是用的nvs操作用户参数,为了方便操作,直接使用结构体
定义参数结构体
/******************************************************************************
* 程序版本
******************************************************************************/
typedef struct{
u8_t softVer[20];
u8_t hardVer[20];
}version_cfg;
/******************************************************************************
* WIFI相关参数
******************************************************************************/
typedef struct
{
wifi_mode_t wifimode;
u8_t dhcpEn;
wifi_ap_config_t ap_config;
wifi_sta_config_t sta_config;
}wifi_param;
/******************************************************************************
* 串口参数 uart.h
******************************************************************************/
/******************************************************************************
# 网络参数
******************************************************************************/
typedef struct{
u8_t server[32];
u_int16_t port;
u_int16_t localport;
u8_t maxconfigConn;
u8_t maxdataConn;
u_int16_t serviceTimeout;
}server_cfg;
/******************************************************************************
# 协议参数
******************************************************************************/
typedef struct{
u8_t id[12];
u8_t netType;//网络类型TCP/UDP
u8_t protocol;//协议类型
u_int16_t heartBeat;//心跳周期
u8_t maxReconnect;//最大重连次数
u_int16_t maxserialLen;//串口最大包长
u8_t serialTimeout;//串口超时
}protocol_cfg;
/******************************************************************************
# 参数
******************************************************************************/
typedef struct{
version_cfg Version;
server_cfg Server;
uart_config_t Uart;
protocol_cfg Protocol;
wifi_param WifiParam;
}Global_SYS_Cfg;
参数初始化处理
读取参数,不存在时加载并保存默认参数
static const char *TAG = "Parameter";
Global_SYS_Cfg params;
/******************************************************************************
# 加载默认参数
******************************************************************************/
int32_t DefaultParams(void){
uart_config_t defaultUart = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
version_cfg defaultVer ={
.hardVer = HARDVER,
.softVer = SOFTVER
};
server_cfg defaultServer = {
.localport = *,
.server = "*.*.*.* ",
.port = *,
.maxconfigConn = 4,
.maxdataConn = 8,
.serviceTimeout = 600
};
protocol_cfg defaultProtocol = {
.id = "*",
.heartBeat = 120,
.netType = 1,
.protocol = 1,
.maxReconnect = 30,
.maxserialLen = 1024,
.serialTimeout = 50
};
wifi_ap_config_t ap_config = {
.ssid = "esprtos",
.ssid_len = strlen("esprtos"),
.password = "12345678",
.max_connection = 4,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
};
wifi_sta_config_t sta_config = {
.ssid = "*",
.password = "*"
};
wifi_param defaultwifi = {
.sta_config = sta_config,
.ap_config = ap_config,
.wifimode = WIFI_MODE_APSTA,
.dhcpEn = 1
};
params.Version = defaultVer;
params.Uart = defaultUart;
params.Server = defaultServer;
params.Protocol = defaultProtocol;
params.WifiParam = defaultwifi;
return SetParam(1,params);
}
/******************************************************************************
# 初始化参数
******************************************************************************/
int32_t InitParam(void){
int32_t ret=GetParam(1);
if(ret==ESP_ERR_NVS_NOT_FOUND){
ret=DefaultParams();
}
return ret;
}
/******************************************************************************
# 读取参数
******************************************************************************/
int32_t GetParam(u8_t index){
nvs_handle mHandleNvsRead;
esp_err_t err = nvs_open("userconfig", NVS_READWRITE, &mHandleNvsRead);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Open NVS fail:%d",err);
vTaskDelete(NULL);
return err;
}
ESP_LOGI(TAG, "Open NVS ok.");
//读取结构体
memset(¶ms, 0x0, sizeof(params));
uint32_t length = sizeof(params);
err = nvs_get_blob(mHandleNvsRead, "userconfig", ¶ms, &length);
nvs_close(mHandleNvsRead);
return err;
}
/******************************************************************************
# 设置参数
******************************************************************************/
int32_t SetParam(u8_t index,Global_SYS_Cfg param){
nvs_handle mHandleNvs;
esp_err_t err = nvs_open("userconfig", NVS_READWRITE, &mHandleNvs);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Open NVS fail");
}
err = nvs_set_blob(mHandleNvs, "userconfig", ¶ms, sizeof(params));
if (err != ESP_OK){
ESP_LOGE(TAG, "Save Params Fail !! ");
}
else{
ESP_LOGI(TAG, "Save Params Ok !! ");
}
//提交
nvs_commit(mHandleNvs);
//关闭
nvs_close(mHandleNvs);
return err;
}
使用
extern Global_SYS_Cfg params;
wifi_config_t wifi_config_user;
void app_main()
{
ESP_ERROR_CHECK(nvs_flash_init());
esp_err_t err = InitParam();
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Open NVS fail:%d",err);
//后续处理
......
}
uart_set_baudrate(UART_NUM_0,params.Uart.baud_rate);
wifi_config_user.ap = params.WifiParam.ap_config;
wifi_config_user.sta =params.WifiParam.sta_config;
printf("Hello world!
");
//程序代码
......
}