ESP32

您所在的位置:网站首页 arduino储存数据到sd卡 ESP32

ESP32

#ESP32| 来源: 网络整理| 查看: 265

前言

本文旨在记录ESP32-S2的学习研究记录,实现ESP32-S2搭载NB-IoT BC26模块进行低功耗网络通信

1. sd_card_init #include #include #include #include #include "esp_err.h" #include "esp_log.h" #include "esp_vfs_fat.h" #include "driver/sdspi_host.h" #include "driver/spi_common.h" #include "sdmmc_cmd.h" #include "sdkconfig.h" #define CS_PIN 10 #define MOSI_PIN 11 #define SCK_PIN 12 #define MISO_PIN 13 #define MOUNT_POINT "/sd" #define SPI_DMA_CHAN host.slot // on ESP32-S2, DMA channel must be the same as host id int sd_card_init() { ESP_LOGI(TAG, "Initializing SD card"); esp_err_t ret; esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = false, .max_files = 5, .allocation_unit_size = 16 * 1024 }; sdmmc_host_t host = SDSPI_HOST_DEFAULT(); spi_bus_config_t bus_cfg = { .mosi_io_num = MOSI_PIN, .miso_io_num = MISO_PIN, .sclk_io_num = SCK_PIN, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 4000, }; ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize sd_spi bus."); return ret; } sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT(); slot_config.gpio_cs = CS_PIN; slot_config.host_id = host.slot; // init sd sdmmc_card_t *card; ret = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card); if (ret != ESP_OK) { if (ret == ESP_FAIL) { ESP_LOGE(TAG, "Failed to mount filesystem"); } else { ESP_LOGE(TAG, "Failed to initialize the sd_card (%s)", esp_err_to_name(ret)); } return ret; } //sdmmc_card_print_info(stdout, card); int sd_card_size = ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024); ESP_LOGI(TAG, "sd size: %d", sd_card_size); return ret; } 2. 使用说明

1)SD卡接线

此处使用TF卡存储日志,使用最简单方便的 MicroSD Card Adapter, 安装TF卡,MicroSD 有 6 个引脚:CS, SCK, MOSI, MISO, VCC, GND

ESP32-S2 与 MicroSD Card Adapter(TF卡)的引脚接线为:

MicroSD Card Adapter        ------        ESP32-S2

CS            ------        IO10 (FSPICS0)

SCK          ------        IO12 (FSPICLK)

MOSI        ------        IO11(FSPID 输入)

MISO        ------        IO13 (FSPIQ 输出)

VCC          ------        5V        (3V不好用)

GND          ------        GND

2)代码参考及注意

代码参考于:esp-idf/examples/storage/sd_card/main/sd_card_example_main.c

示例中有 ESP32-S2 and ESP32-C3 doesn't have an SD Host peripheral, always use SPI,

所以ESP32-S2 使用SD卡,只能使用SPI外设。

3)配置 esp_vfs_fat_sdspi_mount 各个参数

(1)挂载点配置

esp_vfs_fat_sdmmc_mount_config_t mount_config = {         .format_if_mount_failed = false,  // 如果不能挂载FAT分区,且该参数为真,则创建分区表并格式化文件系统         .max_files = 5,        // 打开文件的最大数量         .allocation_unit_size = 16 * 1024  // 如果设置了 format_if_mount_failed=true,并且挂载失败,则使用给定的分配单元大小格式化卡     };

(2) host配置

sdmmc_host_t host = SDSPI_HOST_DEFAULT();

// 指向描述 SDMMC 主机的结构的指针。使用 SPI 外设时,可以使用 SDSPI_HOST_DEFAULT() 宏初始化此结构

spi_bus_config_t bus_cfg = {        // 配置SPI总线,用于host配置         .mosi_io_num = MOSI_PIN,        // MOSI输入引脚         .miso_io_num = MISO_PIN,        // MISO输出引脚         .sclk_io_num = SCK_PIN,           // SCK时钟引脚         .quadwp_io_num = -1,         .quadhd_io_num = -1,         .max_transfer_sz = 4000,     };

spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);  // 在使用host.slot之前,确保SPI已使用spi_bus_initialize()进行了初始化 

(3)sdspi_device_config_t 配置

    SD SPI 设备的额外配置

    sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();     slot_config.gpio_cs = CS_PIN;        // CS信号的GPIO引脚     slot_config.host_id = host.slot;        // 要使用的 SPI 主机

(4)从FAT文件系统中,挂载SD卡

sdmmc_card_t *card;

esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card);

4) 存储日志到SD卡

        FILE *f = fopen(MOUNT_POINT"/esp32s2.log", "a+");         if (f == NULL) {             ESP_LOGE(TAG, "Failed to open file for writing");             return 1;         }

        // 自定义日志格式到SD卡文件, 例如:[设备运行时间] [日志级别] -- 日志信息

        // [ 0.276] [INFO] -- dev_init_used_time: 0.131         fprintf(f, "[%6.3f] [%s] -- %s\n", run_sec(), level[log_level - 1], esp_buf);         fclose(f);

ESP_IDF编程指南参考:FAT Filesystem Support - ESP32-S2 - — ESP-IDF Programming Guide latest documentation



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3