硬件:ESP32-S3-DevKitC-1
环境:ubuntu+vscode
IDF版本:v4.4
问题描述:
我想使用一个8线的mmc芯片作为外部存储,但是当我稍微修改例程后,发现无法挂载文件系统
代码:
Code: [Select all] [Expand/Collapse]
- #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"
- // #include "driver/gpio.h"
- #include "driver/sdmmc_host.h"
- static const char *TAG = "SD_EMMC";
- #define MOUNT_POINT "/sdcard"
- void MY_emmc_test(void)
- {
- // sdmmc_host_init();
- esp_err_t ret;
- // Options for mounting the filesystem.
- // If format_if_mount_failed is set to true, SD card will be partitioned and
- // formatted in case when mounting fails.
- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
- .format_if_mount_failed = true,
- .max_files = 5,
- .allocation_unit_size = 16 * 1024,
- };
- sdmmc_card_t *card;
- const char mount_point[ = MOUNT_POINT;
- ESP_LOGI(TAG, "Initializing SD card");
- // Use settings defined above to initialize SD card and mount FAT filesystem.
- // Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
- // Please check its source code and implement error recovery when developing
- // production applications.
- ESP_LOGI(TAG, "Using SDMMC peripheral");
- sdmmc_host_t host = SDMMC_HOST_DEFAULT();
- host.slot = SDMMC_HOST_SLOT_0;
- host.flags = SDMMC_HOST_FLAG_8BIT;
- // 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();
- // On chips where the GPIOs used for SD card can be configured, set them in
- // the slot_config structure:
- sdmmc_slot_config_t slot_config =
- {
- .clk = GPIO_NUM_17,
- .cmd = GPIO_NUM_8,
- .d0 = GPIO_NUM_14,
- .d1 = GPIO_NUM_13,
- .d2 = GPIO_NUM_12,
- .d3 = GPIO_NUM_11,
- .d4 = GPIO_NUM_10,
- .d5 = GPIO_NUM_9,
- .d6 = GPIO_NUM_46,
- .d7 = GPIO_NUM_3,
- .cd = SDMMC_SLOT_NO_CD,
- .wp = SDMMC_SLOT_NO_WP,
- .width = SDMMC_SLOT_WIDTH_DEFAULT,
- .flags = 0,
- };
- //slot_config.RST is GPIO_NUM_18;RST always hign level;
- gpio_set_level(GPIO_NUM_18, 1);
- // To use 1-line SD mode, change this to 1:
- slot_config.width = 8;
- // 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;
- // sdmmc_host_init_slot(SDMMC_HOST_SLOT_0, &slot_config);
- // sdmmc_card_init(&host, &card);
- ESP_LOGI(TAG, "Mounting filesystem");
- ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
- 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;
- }
- ESP_LOGI(TAG, "Filesystem mounted");
- // Card has been initialized, print its properties
- sdmmc_card_print_info(stdout, card);
- // Use POSIX and C standard library functions to work with files:
- // First create a file.
- const char *file_hello = MOUNT_POINT "/hello.txt";
- ESP_LOGI(TAG, "Opening file %s", file_hello);
- FILE *f = fopen(file_hello, "w");
- if (f == NULL)
- {
- ESP_LOGE(TAG, "Failed to open file for writing");
- return;
- }
- fprintf(f, "Hello %s!n", card->cid.name);
- fclose(f);
- ESP_LOGI(TAG, "File written");
- const char *file_foo = MOUNT_POINT "/foo.txt";
- // Check if destination file exists before renaming
- struct stat st;
- if (stat(file_foo, &st) == 0)
- {
- // Delete it if it exists
- unlink(file_foo);
- }
- // Rename original file
- ESP_LOGI(TAG, "Renaming file %s to %s", file_hello, file_foo);
- if (rename(file_hello, file_foo) != 0)
- {
- ESP_LOGE(TAG, "Rename failed");
- return;
- }
- // Open renamed file for reading
- ESP_LOGI(TAG, "Reading file %s", file_foo);
- f = fopen(file_foo, "r");
- if (f == NULL)
- {
- ESP_LOGE(TAG, "Failed to open file for reading");
- return;
- }
- // Read a line from file
- char line[64;
- fgets(line, sizeof(line), f);
- fclose(f);
- // Strip newline
- char *pos = strchr(line, 'n');
- if (pos)
- {
- *pos = '
|