目录
一. 分区表格式定义
分区表通常以 CSV(逗号分隔值)文件形式定义,例如:
二. 字段解析
分区表详解:
类型 | 分区属性 | 值类型 |
Name | 分区名称 | / |
Type | 分区类型 | app、data、0x40-0xFE自定义 |
SubType | 分区子类型 | 应用程序分区(Type=app)
|
数据分区(Type=data)
| ||
Offset | 分区起始地址(偏移地址) | 分区在flash中的起始地址 |
Size | 分区大小(支持单位:K、M,如1M=1048576 字节) | 分区占用的空间 |
Flags | 特殊标志 | 可选encrypted、readonly |
三. 自定义分区表
- 创建
partitions.csv
文件 - 在
CMakeLists.txt
中指定:set(PartitionTable "partitions.csv")
- 通过
idf.py menuconfig
验证配置,若看到如下内容,则说明配置生效
Partition Table --->
[*] Custom partition table CSV file
(partitions.csv) Custom partition table CSV file
四. 代码
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 1M,
user, 0x40, 0x01, , 0x1000,
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "esp_partition.h"
#include <string.h>
static const char* TAG = "main";
#define USER_PARTITION_TYPE 0x40 //自定义的分区类型
#define USER_PARTITION_SUBTYPE 0x01 //自定义的分区子类型
//分区指针
static const esp_partition_t* partition_res= NULL;
//读取缓存
static char g_esp_buf[1024];
void app_main(void)
{
//找到自定义分区,返回分区指针,后续用到这个指针进行各种操作
partition_res=esp_partition_find_first(USER_PARTITION_TYPE,USER_PARTITION_SUBTYPE,"user");
if(partition_res == NULL)
{
ESP_LOGI(TAG,"Can't find partition,return");
return;
}
//擦除
ESP_ERROR_CHECK(esp_partition_erase_range(partition_res,0x0,0x1000));
//测试字符串
const char* test_str = "this is for test string";
//从分区偏移位置0x0写入字符串
ESP_ERROR_ECK(esp_partition_write(partition_res,0x00, test_str, strlen(test_str)));
//从分区偏移位置0x0读取字符串
ESP_ERROR_ECK(esp_partition_read(partition_res,0x00, g_esp_buf, strlen(test_str)));
ESP_LOGI(TAG,"Read partition str:%s",g_esp_buf);
while(1)
{
vTaskDelay(pdMS_TO_TICKS(1000));
}
}