1、平台基础
1、软件:esp-idf-v3.1.3
2、硬件:ESP32
3、官方链接:
3.1 :链接1
3.2 :下载2 链接3
2、如何使用
2.1 csv flash分区表,分区情况
esp32flash相当于一张表,可以官方定好的格式,去定义自己想要的nvs的大小
格式:# Name, Type, SubType, Offset, Size, Flags
两个ota可以设置一样大小总共 大小为4M
# Name, Type, SubType, Offset, Size, Flags
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 0x140000,
ota_0, app, ota_0, , 0x140000,
ota_1, app, ota_1, , 0x140000,
user_save,data,nvs, , 8k, // 用户用数据
2.2 .h文件
用户需要保存的数据放在自己定义好的 结构体中,需要保存的就放在里面进行保存
#define in_flash_data_h_
#include <stdio.h>
#include <string.h>
typedef struct
{
uint8_t ble_adv_name[30];
uint8_t compay_name[30]; // s
} STU_USER;
extern STU_USER user_para;
void user_read_flash(void);
void delay_ms(uint32_t set_ms);
void task_write_user_data(void);
#endif
2.3 .c文件
主要函数有 读函数、写函数,
#include <assert.h>
#include "esp_partition.h"
#include "esp_log.h"
#include "in_flash_data.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
static const char *TAG = "user_flash";
// 用户数据------------------------
STU_USER user_para;
static uint8_t save_user_data_enbale =0;
// 读取flash 数据----------
void user_read_flash(void)
{
// Find the partition map in the partition table
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "user_save");
assert(partition != NULL);
static uint8_t read_data[sizeof(STU_USER)+2];
memset(read_data, 0xFF, sizeof(read_data));
uint8_t err =0xff;
// 1: read 读函数
err=esp_partition_read(partition, 0, read_data, sizeof(read_data));
if(err!=0)
{
ESP_LOGI(TAG, "user flash read ----1111%d",err);
}
// 2:读取存入数据头部----为了判断是否曾写过flash
if((read_data[0]==0xAA)&&(read_data[1]==0xAA))
{
esp_log_buffer_hex(TAG, read_data,sizeof(read_data));
memcpy(user_para.ble_adv_name,read_data+2,sizeof(read_data)-2);
}
else
{
//
save_user_data_enbale =1;
memcpy(user_para.ble_adv_name,"MK113",strlen("Snifx"));
memcpy(user_para.compay_name,"MOKO TECHNOLOGY LTD.",strlen("MOKO TECHNOLOGY LTD."));
}
}
/*********************************/
// 写函数 可以直接放在任务中
void task_write_user_data(void)
{
uint8_t save_head[2]={0xAA,0XAA};
uint8_t buf[sizeof(STU_USER)];
uint8_t err=0xff;
if(save_user_data_enbale==0) return;
save_user_data_enbale =0;
// step 1: 寻找用户flash id
const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "user_save");
//
assert(partition != NULL);
// step 2: 擦除flash
err=esp_partition_erase_range(partition, 0, partition->size);
if(err!=0)
{
ESP_LOGI(TAG, "user flash erase range ----%d",err);
}
// step 3:获得需要保存的用户数据
memcpy(buf,user_para.ble_adv_name,sizeof(STU_USER));
// step 4:write save head 写flash函数,先写入头部,为了表明曾写过flash
err = esp_partition_write(partition, 0,save_head,sizeof(save_head));
if(err!=0)
{
ESP_LOGI(TAG, "user flash write header ----1111%d",err);
}
err = esp_partition_write(partition, 2,buf,sizeof(STU_USER));
if(err!=0)
{
ESP_LOGI(TAG, "user flash write datas ----2222%d",err);
}
ESP_LOGI(TAG, "user flash write");
}
void delay_ms(uint32_t set_ms)
{
vTaskDelay(set_ms / portTICK_PERIOD_MS);
}
可以加入QQ群:687360507
与大伙沟通交流,技术在于分享而进步