ESP32笔记(2) flash使用

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
与大伙沟通交流,技术在于分享而进步

  • 3
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ESP32 arduino可以通过使用外部SPI Flash来扩展其Flash存储容量。以下是使用外部SPI Flash的步骤: 1. 确认你的ESP32板支持外部SPI Flash,并且已经连接到相应的引脚上。 2. 在Arduino IDE中,打开你的ESP32项目,并选择正确的板和端口。 3. 打开“Tools”菜单,选择“Flash Size”选项,并选择你的外部SPI Flash大小。 4. 在代码中,使用SPIFFS库来访问外部SPI Flash。你可以使用以下代码来挂载SPIFFS: ```C++ #include <SPIFFS.h> void setup() { if(!SPIFFS.begin(true)){ Serial.println("An Error has occurred while mounting SPIFFS"); return; } } ``` 5. 使用SPIFFS库中的API来读写外部SPI Flash中的文件。例如,以下代码可以将一个字符串写入到名为“test.txt”的文件中: ```C++ File file = SPIFFS.open("/test.txt", FILE_WRITE); if(!file){ Serial.println("Failed to open file for writing"); return; } file.println("Hello world"); file.close(); ``` 6. 使用SPIFFS库中的API来读取外部SPI Flash中的文件。例如,以下代码可以读取名为“test.txt”的文件中的内容: ```C++ File file = SPIFFS.open("/test.txt"); if(!file){ Serial.println("Failed to open file for reading"); return; } while(file.available()){ Serial.write(file.read()); } file.close(); ``` 注意,在使用外部SPI Flash时,需要确保在代码中正确地引用文件路径。例如,如果你的文件名为“test.txt”,则应该在代码中引用为“/test.txt”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值