ESP8266

ESP8266读取用户分区数据

用户分区数据(暂定放置fctry【aliyun例子分区表】)

key,type,encoding,value
user-info,namespace,,
Name,data,string,xuhong
Age,data,u8,18
Csdn,data,string,https://blog.csdn.net/xh870189248
GitHub,data,string,https://github.com/xuhongv

转换成bin文件

$IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py --input userInfo.csv --output my_single_mfg.bin --size 0x4000

分区表如下:

nvs,      data, nvs,     0x9000,  0x4000
otadata,  data, ota,     0xd000,  0x2000
phy_init, data, phy,     0xf000,  0x1000
ota_0,    0,    ota_0,   0x10000, 0xF0000
fctry,    data, nvs,     0x100000,0x4000
ota_1,    0,    ota_1,   0x110000,0xF0000

烧录用户信息:

$IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp8266 --port COM5 --baud 115200 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 40m --flash_size 4MB 0x100000 my_single_mfg.bin
(参数是用make app后提示的信息修改而来)

读取例子(来源:这里):

//这个宏的数值是从 自定义分区表获取
#define MFG_PARTITION_NAME "fctry"
//这个宏的数值是从 csv文件定义获取
#define NVS_PRODUCT "user-info"

/**
 * @description:  读取数据进去nvs里面的任务
 * @param {type} null
 * @return: 
 */
static void Task_NVS_Read(void *pvParameters)
{

    ESP_LOGI(TAG, "------------------ Start Task_NVS_Read  --------------------------");
    nvs_handle mHandleNvsRead;
    uint8_t nvs_i8 = 0;
    esp_err_t err;
    //初始化分区
    if ((err = nvs_flash_init_partition(MFG_PARTITION_NAME)) != ESP_OK)
    {
        ESP_LOGE(TAG, "NVS Flash init %s failed, Please check that you have flashed fctry partition!!!", MFG_PARTITION_NAME);
        vTaskDelete(NULL);
    }
    //NVS操作的句柄,类似于 rtos系统的任务创建返回的句柄!
    err = nvs_open_from_partition(MFG_PARTITION_NAME, NVS_PRODUCT, NVS_READONLY, &mHandleNvsRead);
    //打开数据库,打开一个数据库就相当于会返回一个句柄
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "Open NVS Table fail");
        vTaskDelete(NULL);
    }
    else
    {
        ESP_LOGI(TAG, "Open NVS Table ok.");
    }

    //读取 字符串 --> 名字
    char data[65];
    uint32_t len = sizeof(data);
    err = nvs_get_str(mHandleNvsRead, "Name", data, &len);

    if (err == ESP_OK)
        ESP_LOGI(TAG, "get str data = %s ", data);
    else
        ESP_LOGI(TAG, "get str data error");

    //读取 u8 年龄
    err = nvs_get_u8(mHandleNvsRead, "Age", &nvs_i8);
    if (err == ESP_OK)
        ESP_LOGI(TAG, "get nvs_i8 = %d ", nvs_i8);
    else
        ESP_LOGI(TAG, "get nvs_i8 error");

    //读取 字符串 --> Csdn博客地址
    char Csdn[65];
    uint32_t lenCsdn = sizeof(Csdn);
    err = nvs_get_str(mHandleNvsRead, "Csdn", Csdn, &lenCsdn);

    if (err == ESP_OK)
        ESP_LOGI(TAG, "get str data = %s ", Csdn);
    else
        ESP_LOGI(TAG, "get Csdn data error");

    //读取 字符串 --> GitHub地址
    char GitHub[65];
    uint32_t lenGitHub = sizeof(GitHub);
    err = nvs_get_str(mHandleNvsRead, "GitHub", GitHub, &lenGitHub);

    if (err == ESP_OK)
        ESP_LOGI(TAG, "get GitHub data = %s ", GitHub);
    else
        ESP_LOGI(TAG, "get GitHub data error");

    //关闭数据库,关闭面板!
    nvs_close(mHandleNvsRead);

    ESP_LOGI(TAG, "------------------ End Task_NVS_Read  --------------------------");
    vTaskDelete(NULL);
}