|
|
@ -12,7 +12,7 @@ |
|
|
|
|
|
|
|
#define TAG "CONFIG" |
|
|
|
|
|
|
|
#define flash_write_lenght(type) ((sizeof(type) / 4) + ((sizeof(type) % 4) > 0 ? 1 : 0)) |
|
|
|
#define CONFIG_SIZE ((sizeof(config_t) / 4) + ((sizeof(config_t) % 4) > 0 ? 1 : 0)) |
|
|
|
|
|
|
|
static config_t s_config; |
|
|
|
|
|
|
@ -34,38 +34,7 @@ const uint8_t g_text_buf[] = {"STM32 FLASH TEST"}; |
|
|
|
#define FLASH_SAVE_ADDR \ |
|
|
|
0x08010000 /* 设置FLASH 保存地址(必须为4的整数倍,且其值要大于本代码所占用FLASH的大小 + 0X08000000) */ |
|
|
|
|
|
|
|
// #define INIT_CONFIG(type, name, markname) \ |
|
|
|
// type name = 0; \ |
|
|
|
// err = nvs_get_##type(s_nvs_handle, markname, &name); \ |
|
|
|
// if (err == ESP_OK && !refresh_config) { \ |
|
|
|
// s_config.name = name; \ |
|
|
|
// } else { \ |
|
|
|
// s_is_first_init = true; \ |
|
|
|
// ESP_LOGI(TAG, "reconfig config %s", #name); \ |
|
|
|
// err = nvs_set_##type(s_nvs_handle, markname, s_config.name); \ |
|
|
|
// if (err != ESP_OK) { \ |
|
|
|
// ESP_LOGE(TAG, "nvs set %s fail,error %d", #name, err); \ |
|
|
|
// } \ |
|
|
|
// } |
|
|
|
|
|
|
|
// #define UPDATE_CONFIG(type, name, markname) \ |
|
|
|
// type name = 0; \ |
|
|
|
// err = nvs_get_##type(s_nvs_handle, markname, &name); \ |
|
|
|
// if (err == ESP_OK) { \ |
|
|
|
// if (s_config.name != name) { \ |
|
|
|
// err = nvs_set_##type(s_nvs_handle, markname, s_config.name); \ |
|
|
|
// if (err != ESP_OK) { \ |
|
|
|
// ESP_LOGE(TAG, "nvs set %s fail,error %d", #name, err); \ |
|
|
|
// } else { \ |
|
|
|
// ESP_LOGI(TAG, "update config %s -> %d", #name, s_config.name); \ |
|
|
|
// } \ |
|
|
|
// } \ |
|
|
|
// } else { \ |
|
|
|
// ESP_LOGE(TAG, "nvs_get %s fail %d", #name, err); \ |
|
|
|
// } |
|
|
|
|
|
|
|
void config_init() { |
|
|
|
uint8_t g_text_rx_buf[sizeof(config_t)]; |
|
|
|
#if 1 |
|
|
|
IP4_ADDR((ip4_addr_t *)&s_config.ip, 192, 168, 1, 10); |
|
|
|
IP4_ADDR((ip4_addr_t *)&s_config.gw, 192, 168, 1, 1); |
|
|
@ -75,50 +44,14 @@ void config_init() { |
|
|
|
s_config.mask = 1; // |
|
|
|
|
|
|
|
#endif |
|
|
|
stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)&s_config, (sizeof(config_t) / 4)); |
|
|
|
|
|
|
|
// stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)g_text_buf, SIZE); |
|
|
|
stmflash_read(FLASH_SAVE_ADDR, (uint32_t *)g_text_rx_buf, (sizeof(config_t) / 4)); |
|
|
|
ZLOGI(TAG, "ip:%d\r\n", ((config_t *)g_text_rx_buf)->ip); |
|
|
|
ZLOGI(TAG, "gw:%d\r\n", ((config_t *)g_text_rx_buf)->gw); |
|
|
|
ZLOGI(TAG, "netmask:%d\r\n", ((config_t *)g_text_rx_buf)->netmask); |
|
|
|
ZLOGI(TAG, "localport:%d\r\n", ((config_t *)g_text_rx_buf)->localport); |
|
|
|
ZLOGI(TAG, "obtaining_ip_mode:%d\r\n", ((config_t *)g_text_rx_buf)->obtaining_ip_mode); |
|
|
|
ZLOGI(TAG, "mask:%d\r\n", ((config_t *)g_text_rx_buf)->mask); |
|
|
|
// printf("%s\r\n", g_text_rx_buf); |
|
|
|
|
|
|
|
// if (stmflash_write_config(STM32_FLASH_BASE, &s_config, flash_write_lenght(config_t)) < 0) { |
|
|
|
// ZLOGE(TAG, "flash write config error\r\n"); |
|
|
|
// } |
|
|
|
|
|
|
|
// esp_err_t err = nvs_flash_init(); |
|
|
|
|
|
|
|
// if (err == ESP_ERR_NVS_NO_FREE_PAGES) { |
|
|
|
// ESP_ERROR_CHECK(nvs_flash_erase()); |
|
|
|
// err = nvs_flash_init(); |
|
|
|
// } |
|
|
|
|
|
|
|
// if (err != ESP_ERR_NVS_NO_FREE_PAGES) { |
|
|
|
// esp_err_t err = nvs_open("app_config", NVS_READWRITE, &s_nvs_handle); |
|
|
|
// if (err == ESP_OK) { |
|
|
|
// bool refresh_config = false; |
|
|
|
// INIT_CONFIG(u32, ip, "ip"); |
|
|
|
// INIT_CONFIG(u32, gw, "gw"); |
|
|
|
// INIT_CONFIG(u32, netmask, "netmask"); |
|
|
|
// INIT_CONFIG(u32, localport, "localport"); |
|
|
|
// INIT_CONFIG(u32, sound_report_times, "srt"); |
|
|
|
// INIT_CONFIG(u32, obtaining_ip_mode, "oim"); |
|
|
|
// INIT_CONFIG(u32, sound_magnification_factors, "smf"); |
|
|
|
// } else { |
|
|
|
// } |
|
|
|
|
|
|
|
// } else { |
|
|
|
// ESP_LOGE(TAG, "nvs flash init fail %d", err); |
|
|
|
// } |
|
|
|
// nvs_close(s_nvs_handle); |
|
|
|
if (stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)&s_config, CONFIG_SIZE) < 0) { |
|
|
|
ZLOGE(TAG, "flash write config error\r\n"); |
|
|
|
} |
|
|
|
|
|
|
|
// config_flash_read(); |
|
|
|
|
|
|
|
/*dumpconfig*/ |
|
|
|
// config_dump_config(); |
|
|
|
config_dump_config(); |
|
|
|
} |
|
|
|
bool config_is_first_init() { return s_is_first_init; } |
|
|
|
void config_dump_config() { |
|
|
@ -129,22 +62,17 @@ void config_dump_config() { |
|
|
|
ZLOGI(TAG, "= config netmask: %s", inet_ntoa(s_config.netmask)); |
|
|
|
ZLOGI(TAG, "= config localport: %u", s_config.localport); |
|
|
|
} |
|
|
|
// void config_update() { |
|
|
|
// esp_err_t err = nvs_open("app_config", NVS_READWRITE, &s_nvs_handle); |
|
|
|
// if (err == ESP_OK) { |
|
|
|
// UPDATE_CONFIG(u32, ip, "ip"); |
|
|
|
// UPDATE_CONFIG(u32, gw, "gw"); |
|
|
|
// UPDATE_CONFIG(u32, netmask, "netmask"); |
|
|
|
// UPDATE_CONFIG(u32, localport, "localport"); |
|
|
|
// UPDATE_CONFIG(u32, sound_report_times, "srt"); |
|
|
|
// UPDATE_CONFIG(u32, obtaining_ip_mode, "oim"); |
|
|
|
// UPDATE_CONFIG(u32, sound_magnification_factors, "smf"); |
|
|
|
|
|
|
|
// } else { |
|
|
|
// } |
|
|
|
|
|
|
|
// nvs_close(s_nvs_handle); |
|
|
|
|
|
|
|
// config_dump_config(); |
|
|
|
// } |
|
|
|
|
|
|
|
bool config_update() { |
|
|
|
if (stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)&s_config, CONFIG_SIZE) < 0) { |
|
|
|
ZLOGE(TAG, "flash write config error\r\n"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
config_dump_config(); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
void config_flash_read() { stmflash_read(FLASH_SAVE_ADDR, (uint32_t *)&s_config, CONFIG_SIZE); } |
|
|
|
|
|
|
|
config_t *config_get() { return &s_config; } |