#include "esp_vfs_fat.h" #include #include #include "esp_log.h" #include "sdcard.h" static const char *TAG = "SDCARD"; SdSpiCard::SdSpiCard() : _spi_hdl(nullptr), _card(nullptr), _mounted(false) { } SdSpiCard::~SdSpiCard() { if (_mounted) deinit(); } esp_err_t SdSpiCard::init() { esp_err_t ret; gpio_config_t miso_cfg = { .pin_bit_mask = 1ULL << SD_MISO_PIN, .mode = GPIO_MODE_INPUT, .pull_up_en = GPIO_PULLUP_ENABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE}; gpio_config(&miso_cfg); // 1. SPI总线初始化不变 // spi_bus_config_t bus_cfg = {}; // bus_cfg.mosi_io_num = SD_MOSI_PIN; // bus_cfg.miso_io_num = SD_MISO_PIN; // bus_cfg.sclk_io_num = SD_SCK_PIN; // bus_cfg.quadwp_io_num = -1; // bus_cfg.quadhd_io_num = -1; // bus_cfg.max_transfer_sz = 4096; // ret = spi_bus_initialize(SD_SPI_HOST, &bus_cfg, SPI_DMA_CH_AUTO); // if (ret != ESP_OK) // { // ESP_LOGE(TAG, "SPI bus init fail:%s", esp_err_to_name(ret)); // return ret; // } // 2. SD SPI设备配置 sdspi_device_config_t dev_cfg = SDSPI_DEVICE_CONFIG_DEFAULT(); dev_cfg.host_id = SD_SPI_HOST; dev_cfg.gpio_cs = SD_CS_PIN; // 3. SDMMC主机配置 sdmmc_host_t host_cfg = SDSPI_HOST_DEFAULT(); host_cfg.max_freq_khz = 100; // 100kHz // 4. 挂载配置 esp_vfs_fat_mount_config_t mount_cfg = { .format_if_mount_failed = false, .max_files = 8, .allocation_unit_size = 16 * 1024, .disk_status_check_enable = false, .use_one_fat = false}; // 5. 挂载SD卡 ret = esp_vfs_fat_sdspi_mount(SD_MOUNT_POINT, &host_cfg, &dev_cfg, &mount_cfg, &_card); if (ret != ESP_OK) { ESP_LOGE(TAG, "SD mount fail:%s", esp_err_to_name(ret)); //spi_bus_free(SD_SPI_HOST);//共享SPI return ret; } sdmmc_card_print_info(stdout, _card); _mounted = true; return ESP_OK; } esp_err_t SdSpiCard::deinit() { if (!_mounted) return ESP_ERR_INVALID_STATE; esp_err_t ret = esp_vfs_fat_sdcard_unmount(SD_MOUNT_POINT, _card); //spi_bus_free(SD_SPI_HOST); _card = nullptr; _mounted = false; return ret; } bool SdSpiCard::is_mounted() { return _mounted; } esp_err_t SdSpiCard::write_file(const std::string &path, const uint8_t *data, size_t len) { if (!_mounted) return ESP_ERR_INVALID_STATE; std::string full = std::string(SD_MOUNT_POINT) + "/" + path; FILE *f = fopen(full.c_str(), "wb"); if (!f) return ESP_FAIL; size_t w = fwrite(data, 1, len, f); fclose(f); vTaskDelay(pdMS_TO_TICKS(50)); return (w == len) ? ESP_OK : ESP_ERR_NO_MEM; } esp_err_t SdSpiCard::read_file(const std::string &path, uint8_t *buf, size_t buf_max, size_t &read_len) { if (!_mounted) return ESP_ERR_INVALID_STATE; std::string full = std::string(SD_MOUNT_POINT) + "/" + path; FILE *f = fopen(full.c_str(), "rb"); if (!f) return ESP_FAIL; read_len = fread(buf, 1, buf_max, f); fclose(f); return ESP_OK; } esp_err_t SdSpiCard::del_file(const std::string &path) { if (!_mounted) return ESP_ERR_INVALID_STATE; std::string full = std::string(SD_MOUNT_POINT) + "/" + path; return remove(full.c_str()) == 0 ? ESP_OK : ESP_FAIL; } esp_err_t SdSpiCard::copy_file_to_sd(const std::string &src_local_path, const std::string &dst_sd_path) { if (!_mounted) { ESP_LOGE(TAG, "SD not mounted"); return ESP_ERR_INVALID_STATE; } // 打开源文件(Flash/Spiffs/NVS分区文件) FILE *fp_src = fopen(src_local_path.c_str(), "rb"); if (!fp_src) { ESP_LOGE(TAG, "Open src file fail: %s", src_local_path.c_str()); return ESP_FAIL; } // 拼接SD卡完整路径 std::string sd_full_path = std::string(SD_MOUNT_POINT) + "/" + dst_sd_path; FILE *fp_dst = fopen(sd_full_path.c_str(), "wb"); if (!fp_dst) { ESP_LOGE(TAG, "Open sd dst file fail: %s", sd_full_path.c_str()); fclose(fp_src); return ESP_FAIL; } // 4KB缓冲区分段拷贝,避免栈溢出 uint8_t buf[4096]; size_t read_size; while ((read_size = fread(buf, 1, sizeof(buf), fp_src)) > 0) { size_t write_size = fwrite(buf, 1, read_size, fp_dst); if (write_size != read_size) { ESP_LOGE(TAG, "Write to SD incomplete"); fclose(fp_src); fclose(fp_dst); return ESP_ERR_NO_MEM; } } // 关闭文件,确保数据刷入SD卡 fclose(fp_src); fclose(fp_dst); ESP_LOGI(TAG, "Copy success: %s -> %s", src_local_path.c_str(), sd_full_path.c_str()); return ESP_OK; }