首页 > 其他分享 >ESP32-S3 FLASH 操作

ESP32-S3 FLASH 操作

时间:2023-04-24 22:01:19浏览次数:34  
标签:partition err ESP S3 ESP32 FLASH esp read data

FLASH 读取操作

整个分区的读、写、擦除

# ESP-IDF Partition Table
# Name,    Type,  SubType, Offset,  Size, Flags
nvs,       data,  nvs,     0x9000,  0x4000,
otadata,   data,  ota,     ,        0x2000,
phy_init,  data,  phy,     ,        0x1000,
key_data,  0x40,  0,       ,        256k,
factory,   app,   factory, ,        3M,
const esp_partition_t *partition = esp_partition_find_first(0x40,  ESP_PARTITION_SUBTYPE_ANY,"key_data");

    // const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");
    assert(partition != NULL);

    static char store_data[] = "Test the read, write, and erase operations of the entire partition.";
    static char read_data[sizeof(store_data)];
    memset(read_data, 0xFF, sizeof(read_data));

    if(sizeof(store_data) >  partition->size)
    {
        ESP_LOGE(TAG,"The data exceeds the partition size.");
        return ;
    }

    // // 先擦除 分区
    ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));

    // // 开始写入数据
    ESP_ERROR_CHECK(esp_partition_write(partition, 0, store_data, sizeof(store_data)));
    ESP_LOGI(TAG, "Written data: %s", store_data);

    // 读取数据,同时检查对比和写入数据的完整性
    ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, sizeof(read_data)));
    // assert(memcmp(store_data, read_data, sizeof(read_data)) == 0);
    ESP_LOGI(TAG, "Read data: %s", read_data);

扇区的读、写、擦除

  1. esp32-s3 flash 的最小写入单位是字节,最大写入单位是页,但是超过一个扇区时就需要注意容易被误擦除。
  2. esp32-s3 flash 的最小读取单位是字节。
  3. esp32-s3 flash 的最小擦除单位是扇区。打印 erase_size就可以知道最小擦除的对齐是多少了。
typedef struct {
    esp_flash_t* flash_chip;            /*!< SPI flash chip on which the partition resides */
    esp_partition_type_t type;          /*!< partition type (app/data) */
    esp_partition_subtype_t subtype;    /*!< partition subtype */
    uint32_t address;                   /*!< starting address of the partition in flash */
    uint32_t size;                      /*!< size of the partition, in bytes */
    uint32_t erase_size;                /*!< size the erase operation should be aligned to */
    char label[17];                     /*!< partition label, zero-terminated ASCII string */
    bool encrypted;                     /*!< flag is set to true if partition is encrypted */
} esp_partition_t;

4.这个是单独操作扇区的示例。

#define  FLASH_SECTOR_SIZE  0x1000  //4K

void app_main(void)
{

    static char store_data[] = "Test the read, write, and erase operations of the entire partition.\n\
                                Test the read, write, and erase operations of the entire partition.\n\
                                Test the read, write, and erase operations of the entire partition.\n\
                                Test the read, write, and erase operations of the entire partition.\n\
                                Test the read, write, and erase operations of the entire partition.\n\
                                Test the read, write, and erase operations of the entire partition.";
    static char read_data[sizeof(store_data)];
    memset(read_data, 0xFF, sizeof(read_data));

    const esp_partition_t *partition = esp_partition_find_first(0x40, 0, "key_data");

    printf("erase_size %x size %x\r\n",partition->erase_size,partition->size);
    // // 先擦除 分区
    ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, FLASH_SECTOR_SIZE));

    // // 开始写入数据
    ESP_ERROR_CHECK(esp_partition_write(partition, 0, store_data, sizeof(store_data)));
    ESP_LOGI(TAG, "Written data: %s", store_data);

    // 读取数据,同时检查对比和写入数据的完整性
    ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, sizeof(read_data)));
    assert(memcmp(store_data, read_data, sizeof(read_data)) == 0);
    
    ESP_LOGI(TAG, "read data: %s", read_data);

    printf("Restarting now.\n");
    fflush(stdout);

    // esp_restart();
}

封装两个 FLASH 读写函数

  1. 这两个读写函数,每次操作限制在 4K 之内。
#define KEY_DATA_SECTOR_SIZE 0X1000 //Sector size 4096/4K

static esp_err_t key_data_flash_read(void * buffer, uint32_t offset, uint32_t length)
{
    esp_err_t err;
    if(buffer ==NULL || (length > KEY_DATA_SECTOR_SIZE))
    {
        ESP_LOGE(KEY_DATA_TAG, "ESP_ERR_INVALID_ARG");
        return ESP_ERR_INVALID_ARG;
    }

    const esp_partition_t *key_data_partition = esp_partition_find_first(0x40,0x00,"key_data");
    if(key_data_partition == NULL)
    {
        ESP_LOGE(KEY_DATA_TAG, "Flash partition not found.");
        return ESP_FAIL;
    }

    err = esp_partition_read(key_data_partition, offset, buffer,length);
    if(err != ESP_OK)
    {
        ESP_LOGE(KEY_DATA_TAG, "Flash read failed.");
        return err;
    }
    return err;
}

static esp_err_t key_data_flash_write(void * buffer, uint32_t offset, uint32_t length)
{
    esp_err_t err;
    if(buffer ==NULL || (length > KEY_DATA_SECTOR_SIZE))
    {
        ESP_LOGE(KEY_DATA_TAG, "ESP_ERR_INVALID_ARG");
        return ESP_ERR_INVALID_ARG;
    }

    const esp_partition_t *key_data_partition = esp_partition_find_first(0x40,0x00,"key_data");
    if(key_data_partition == NULL)
    {
        ESP_LOGE(KEY_DATA_TAG, "Flash partition not found.");
        return ESP_FAIL;
    }

    err = esp_partition_erase_range(key_data_partition, offset, KEY_DATA_SECTOR_SIZE);
    if(err != ESP_OK)
    {
        ESP_LOGE(KEY_DATA_TAG, "Flash erase failed.");
        return err;
    }
    
    err = esp_partition_write(key_data_partition, offset, buffer, length);
    if(err != ESP_OK)
    {
        ESP_LOGE(KEY_DATA_TAG, "Flash write failed.");
        return err;
    }

    return err;
}

标签:partition,err,ESP,S3,ESP32,FLASH,esp,read,data
From: https://www.cnblogs.com/Spin-jump/p/17238234.html

相关文章

  • 使用FlashFxp sftp无法连接Linux处理
    sftp无法连接[18:32:41]FlashFXP5.4.0(build3970)[18:32:41]SupportForumshttps://www.flashfxp.com/forum/[18:32:41]...[18:33:03][R]SSH错误:协商认证模式失败[18:33:03][R]SSH连接已关闭[18:33:03][R]连接失败处理:/etc/ssh/sshd_config文件夹的一......
  • CSS3: 利用分层动画让元素沿弧形路径运动
    译者注:部分代码示例中可以看效果(作者写在博文里面了…),我偷懒把它做成Gif图了。 CSS的animations(动画)和transitions(变换)擅于实现从点A到点B的直线运动,运动轨迹是直线路径。给一个元素添加了animation或者transition以后,无论你如何调整贝塞尔曲线,都无法让它沿着弧形路......
  • FlashFXP命令行
    FlashFXP是一个非常好用的FTP工具,同时也是可以使用命令行进行操作。格式:flashfxp.exe-uploadftp://user:pass@ip:port-localpath="本地路径"-remotepath="远程FTP上的路径"上传使用FlashFXP上传文件(夹)的命令行语法:flashfxp.exe-uploadftp://user:pass@ip:port-localpa......
  • mac安装并配置nexus3.5.1版本
    一、安装nexus前置条件:已经安装了JDK1:下载nexus(http://www.sonatype.com/download-oss-sonatype)最新版本(我的是3.5.1).2:解压下载文件,我的放在了/Users/xxx/myApp下3:配置环境变量:打开/etc目录,在profile文件中加入:NEXUS_HOME="/Users/xxx/myApp/nexus-3.5.1-02-mac/nexus-......
  • FLASH不够用——非零等待FLASH搬到RAM运行
    本文主要实现将FLASH中的代码搬运到RAM运行我们的CH32V2/3系列MCU有几十K,几百K的非零等待FLASH,遇到FLASH不够用的情况实在可惜。主要以V307举例,针对零等待FLASH不够用的情况。典型应用:1.IAP占用了几K空间,搬到RAM后,可以让IAP所占零等待FLASH大幅降低,接近忽略2.切换功能的代码,不......
  • 11-CSS3属性详解(一)
    title:11-CSS3属性详解(一)publish:true前言我们在上一篇文章中学习了CSS3的选择器,本文来学一下CSS3的一些属性。本文主要内容:文本盒模型中的box-sizing属性处理兼容性问题:私有前缀边框背景属性渐变文本text-shadow:设置文本的阴影格式举例: text-s......
  • 12-CSS3属性详解:动画详解
    title:12-CSS3属性详解:动画详解publish:true前言本文主要内容:过渡:transition2D转换transform3D转换transform动画:animation过渡:transitiontransition的中文含义是过渡。过渡是CSS3中具有颠覆性的一个特征,可以实现元素不同状态间的平滑过渡(补间动画),经常......
  • 13-CSS3属性:Flex布局图文详解
    title:13-CSS3属性:Flex布局图文详解publish:true前言CSS3中的flex属性,在布局方面做了非常大的改进,使得我们对多个元素之间的布局排列变得十分灵活,适应性非常强。其强大的伸缩性和自适应性,在网页开中可以发挥极大的作用。flex初体验我们先来看看下面这个最简单的布局:......
  • 14-CSS3属性详解:Web字体
    title:14-CSS3属性详解:Web字体publish:true前言开发人员可以为自已的网页指定特殊的字体(将指定字体提前下载到站点中),无需考虑用户电脑上是否安装了此特殊字体。从此,把特殊字体处理成图片的方式便成为了过去。支持程度比较好,甚至IE低版本的浏览器也能支持。字体的常见格......
  • 17-CSS3的常见边框汇总
    title:17-CSS3的常见边框汇总publish:trueCSS3常见边框汇总<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>CSS3边框</title><style>body,ul,li,dl,dt,dd,h1,......