在 ESP32-S3 上使用 SDIO 接口驱动 SD NAND 时,可通过以下策略显著提升写入速度:
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; // 40MHz
sdmmc_card_t* card;sdmmc_host_t host = SDMMC_HOST_DEFAULT();host.flags = SDMMC_HOST_FLAG_4BIT | SDMMC_HOST_FLAG_USE_HOLD_REG;esp_err_t ret = sdmmc_card_init(&host, &card);
注意事项:
sdmmc_card_t* card;card->csd.read_block_len = 9; // 设置为512字节(2^9)// 若SD NAND支持,可增大至4096字节(2^12)
批量写入:
使用esp_vfs_fat_sdmmc_mount()挂载文件系统时,设置较大的缓存:
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_fails = false,
.max_files = 5,
.allocation_unit_size = 16 * 1024 // 16KB分配单元};host.flags |= SDMMC_HOST_FLAG_NO_CRC;
禁用写保护:
确保 SD NAND 未设置写保护位(通过 CMD13 检查状态)。
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_fails = true,
.max_files = 5,
.allocation_unit_size = 32 * 1024 // 更大的分配单元};FILE* f = fopen("/sdcard/data.bin", "w");setvbuf(f, NULL, _IOFBF, 8192); // 设置8KB写缓存#include "driver/sdmmc_host.h"#include "driver/sdspi_host.h"#include "sdmmc_cmd.h"#include "esp_vfs_fat.h"void sdcard_benchmark() {
// 初始化SDIO主机
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.flags = SDMMC_HOST_FLAG_4BIT | SDMMC_HOST_FLAG_USE_HOLD_REG;
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
// 配置插槽
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
slot_config.width = 4;
// 挂载文件系统
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_fails = false,
.max_files = 5,
.allocation_unit_size = 32 * 1024
};
sdmmc_card_t* card;
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
// 写入测试
FILE* f = fopen("/sdcard/test.bin", "w");
if (!f) {
printf("Failed to open file for writing
");
return;
}
// 分配对齐的缓冲区(使用DMA时需要)
uint8_t* buffer __attribute__((aligned(4))) = malloc(32 * 1024);
memset(buffer, 0x55, 32 * 1024);
// 高速写入
uint32_t start_time = esp_timer_get_time();
for (int i = 0; i < 1000; i++) {
fwrite(buffer, 1, 32 * 1024, f);
// 每100块同步一次,避免频繁fsync
if (i % 100 == 0) fflush(f);
}
fflush(f);
uint32_t end_time = esp_timer_get_time();
float duration = (end_time - start_time) / 1e6;
float throughput = (1000 * 32 * 1024) / duration / (1024 * 1024);
printf("Write throughput: %.2f MB/s
", throughput);
fclose(f);
esp_vfs_fat_sdmmc_unmount();}| 优化策略 | 写入速度(MB/s) |
|---|---|
| 默认配置(1 线 SDIO) | ~3-5 |
| 启用 4 线 SDIO | ~10-15 |
| 提高时钟至 40MHz | ~15-20 |
| 增大块大小 + 批量写入 | ~20-25 |
| 结合 FAT32+DMA + 缓存优化 | ~25-30 |
通过上述优化,ESP32-S3 的 SDIO 写入性能可接近理论极限,满足大多数高速存储需求。
上一篇:sdnand的cmd13使用注意
下一篇:芯存者 存储
电话:176-6539-0767
Q Q:135-0379-986
邮箱:1350379986@qq.com
地址:深圳市南山区后海大道1021号C座