| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Unlicense OR CC0-1.0
- */
- /* HTTP File Server Example, SD card / SPIFFS mount functions.
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <string.h>
- #include "esp_log.h"
- #include "esp_err.h"
- #include "esp_vfs_fat.h"
- #include "esp_spiffs.h"
- #include "sdkconfig.h"
- #include "soc/soc_caps.h"
- #include "driver/sdspi_host.h"
- #include "driver/spi_common.h"
- #if SOC_SDMMC_HOST_SUPPORTED
- #include "driver/sdmmc_host.h"
- #endif
- #include "sdmmc_cmd.h"
- #include "file_serving_example_common.h"
- static const char *TAG = "example_mount";
- #ifdef CONFIG_EXAMPLE_MOUNT_SD_CARD
- esp_err_t example_mount_storage(const char* base_path)
- {
- ESP_LOGI(TAG, "Initializing SD card");
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
- #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_SDCARD_FAILED
- .format_if_mount_failed = true,
- #else
- .format_if_mount_failed = false,
- #endif // CONFIG_EXAMPLE_FORMAT_IF_MOUNT_SDCARD_FAILED
- .max_files = 5,
- .allocation_unit_size = 16 * 1024
- };
- esp_err_t ret;
- sdmmc_card_t* card;
- #ifdef CONFIG_EXAMPLE_USE_SDMMC_HOST
- ESP_LOGI(TAG, "Using SDMMC peripheral");
- sdmmc_host_t host = SDMMC_HOST_DEFAULT();
- // This initializes the slot without card detect (CD) and write protect (WP) signals.
- // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
- sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
- // To use 1-line SD mode, change this to 1:
- slot_config.width = 4;
- #ifdef SOC_SDMMC_USE_GPIO_MATRIX
- // For chips which support GPIO Matrix for SDMMC peripheral, specify the pins.
- slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
- slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
- slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
- slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
- slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
- slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
- #endif // SOC_SDMMC_USE_GPIO_MATRIX
- // Enable internal pullups on enabled pins. The internal pullups
- // are insufficient however, please make sure 10k external pullups are
- // connected on the bus. This is for debug / example purpose only.
- slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
- ret = esp_vfs_fat_sdmmc_mount(base_path, &host, &slot_config, &mount_config, &card);
- #else // CONFIG_EXAMPLE_USE_SDMMC_HOST
- ESP_LOGI(TAG, "Using SPI peripheral");
- sdmmc_host_t host = SDSPI_HOST_DEFAULT();
- spi_bus_config_t bus_cfg = {
- .mosi_io_num = CONFIG_EXAMPLE_PIN_MOSI,
- .miso_io_num = CONFIG_EXAMPLE_PIN_MISO,
- .sclk_io_num = CONFIG_EXAMPLE_PIN_CLK,
- .quadwp_io_num = -1,
- .quadhd_io_num = -1,
- .max_transfer_sz = 4000,
- };
- ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to initialize bus.");
- return ret;
- }
- // This initializes the slot without card detect (CD) and write protect (WP) signals.
- // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
- sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
- slot_config.gpio_cs = CONFIG_EXAMPLE_PIN_CS;
- slot_config.host_id = host.slot;
- ret = esp_vfs_fat_sdspi_mount(base_path, &host, &slot_config, &mount_config, &card);
- #endif // !CONFIG_EXAMPLE_USE_SDMMC_HOST
- if (ret != ESP_OK){
- if (ret == ESP_FAIL) {
- ESP_LOGE(TAG, "Failed to mount filesystem. "
- "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
- } else {
- ESP_LOGE(TAG, "Failed to initialize the card (%s). "
- "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
- }
- return ret;
- }
- sdmmc_card_print_info(stdout, card);
- return ESP_OK;
- }
- #else // CONFIG_EXAMPLE_MOUNT_SD_CARD
- /* Function to initialize SPIFFS */
- esp_err_t example_mount_storage(const char* base_path)
- {
- ESP_LOGI(TAG, "Initializing SPIFFS");
- esp_vfs_spiffs_conf_t conf = {
- .base_path = base_path,
- .partition_label = NULL,
- .max_files = 5, // This sets the maximum number of files that can be open at the same time
- .format_if_mount_failed = true
- };
- esp_err_t ret = esp_vfs_spiffs_register(&conf);
- if (ret != ESP_OK) {
- if (ret == ESP_FAIL) {
- ESP_LOGE(TAG, "Failed to mount or format filesystem");
- } else if (ret == ESP_ERR_NOT_FOUND) {
- ESP_LOGE(TAG, "Failed to find SPIFFS partition");
- } else {
- ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
- }
- return ret;
- }
- size_t total = 0, used = 0;
- ret = esp_spiffs_info(NULL, &total, &used);
- if (ret != ESP_OK) {
- ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
- return ret;
- }
- ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
- return ESP_OK;
- }
- #endif // !CONFIG_EXAMPLE_MOUNT_SD_CARD
|