[已解决]ESP32S3无法挂载EMMC芯片

heipppppp
Posts: 10
Joined: Thu Sep 23, 2021 9:52 am

[已解决]ESP32S3无法挂载EMMC芯片

Postby heipppppp » Wed Nov 24, 2021 6:03 am

硬件:ESP32-S3-DevKitC-1
环境:ubuntu+vscode
IDF版本:v4.4
问题描述:
我想使用一个8线的mmc芯片作为外部存储,但是当我稍微修改例程后,发现无法挂载文件系统
代码:
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/unistd.h>
  4. #include <sys/stat.h>
  5. #include "esp_err.h"
  6. #include "esp_log.h"
  7. #include "esp_vfs_fat.h"
  8. #include "driver/sdspi_host.h"
  9. #include "driver/spi_common.h"
  10. #include "sdmmc_cmd.h"
  11. #include "sdkconfig.h"
  12. // #include "driver/gpio.h"
  13.  
  14. #include "driver/sdmmc_host.h"
  15.  
  16. static const char *TAG = "SD_EMMC";
  17.  
  18. #define MOUNT_POINT "/sdcard"
  19.  
  20. void MY_emmc_test(void)
  21. {
  22.  
  23.     // sdmmc_host_init();
  24.  
  25.     esp_err_t ret;
  26.  
  27.     // Options for mounting the filesystem.
  28.     // If format_if_mount_failed is set to true, SD card will be partitioned and
  29.     // formatted in case when mounting fails.
  30.     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  31.  
  32.         .format_if_mount_failed = true,
  33.  
  34.         .max_files = 5,
  35.         .allocation_unit_size = 16 * 1024,
  36.     };
  37.     sdmmc_card_t *card;
  38.     const char mount_point[] = MOUNT_POINT;
  39.     ESP_LOGI(TAG, "Initializing SD card");
  40.  
  41.     // Use settings defined above to initialize SD card and mount FAT filesystem.
  42.     // Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
  43.     // Please check its source code and implement error recovery when developing
  44.     // production applications.
  45.  
  46.     ESP_LOGI(TAG, "Using SDMMC peripheral");
  47.     sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  48.     host.slot = SDMMC_HOST_SLOT_0;
  49.     host.flags = SDMMC_HOST_FLAG_8BIT;
  50.  
  51.     // This initializes the slot without card detect (CD) and write protect (WP) signals.
  52.     // Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  53.  
  54.     // sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  55.     // On chips where the GPIOs used for SD card can be configured, set them in
  56.     // the slot_config structure:
  57.     sdmmc_slot_config_t slot_config =
  58.         {
  59.             .clk = GPIO_NUM_17,
  60.             .cmd = GPIO_NUM_8,
  61.             .d0 = GPIO_NUM_14,
  62.             .d1 = GPIO_NUM_13,
  63.             .d2 = GPIO_NUM_12,
  64.             .d3 = GPIO_NUM_11,
  65.             .d4 = GPIO_NUM_10,
  66.             .d5 = GPIO_NUM_9,
  67.             .d6 = GPIO_NUM_46,
  68.             .d7 = GPIO_NUM_3,
  69.             .cd = SDMMC_SLOT_NO_CD,
  70.             .wp = SDMMC_SLOT_NO_WP,
  71.             .width = SDMMC_SLOT_WIDTH_DEFAULT,
  72.             .flags = 0,
  73.         };
  74.  
  75.  
  76.     //slot_config.RST is GPIO_NUM_18;RST always hign level;
  77.     gpio_set_level(GPIO_NUM_18, 1);
  78.  
  79.     // To use 1-line SD mode, change this to 1:
  80.     slot_config.width = 8;
  81.  
  82.     // Enable internal pullups on enabled pins. The internal pullups
  83.     // are insufficient however, please make sure 10k external pullups are
  84.     // connected on the bus. This is for debug / example purpose only.
  85.     slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  86.  
  87.    // sdmmc_host_init_slot(SDMMC_HOST_SLOT_0, &slot_config);
  88.     // sdmmc_card_init(&host, &card);
  89.     ESP_LOGI(TAG, "Mounting filesystem");
  90.    ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  91.     if (ret != ESP_OK)
  92.     {
  93.         if (ret == ESP_FAIL)
  94.         {
  95.             ESP_LOGE(TAG, "Failed to mount filesystem. "
  96.                           "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  97.         }
  98.         else
  99.         {
  100.             ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  101.                           "Make sure SD card lines have pull-up resistors in place.",
  102.                      esp_err_to_name(ret));
  103.         }
  104.         return;
  105.     }
  106.     ESP_LOGI(TAG, "Filesystem mounted");
  107.  
  108.     // Card has been initialized, print its properties
  109.     sdmmc_card_print_info(stdout, card);
  110.  
  111.     // Use POSIX and C standard library functions to work with files:
  112.  
  113.     // First create a file.
  114.     const char *file_hello = MOUNT_POINT "/hello.txt";
  115.  
  116.     ESP_LOGI(TAG, "Opening file %s", file_hello);
  117.     FILE *f = fopen(file_hello, "w");
  118.     if (f == NULL)
  119.     {
  120.         ESP_LOGE(TAG, "Failed to open file for writing");
  121.         return;
  122.     }
  123.     fprintf(f, "Hello %s!\n", card->cid.name);
  124.     fclose(f);
  125.     ESP_LOGI(TAG, "File written");
  126.  
  127.     const char *file_foo = MOUNT_POINT "/foo.txt";
  128.  
  129.     // Check if destination file exists before renaming
  130.     struct stat st;
  131.     if (stat(file_foo, &st) == 0)
  132.     {
  133.         // Delete it if it exists
  134.         unlink(file_foo);
  135.     }
  136.  
  137.     // Rename original file
  138.     ESP_LOGI(TAG, "Renaming file %s to %s", file_hello, file_foo);
  139.     if (rename(file_hello, file_foo) != 0)
  140.     {
  141.         ESP_LOGE(TAG, "Rename failed");
  142.         return;
  143.     }
  144.  
  145.     // Open renamed file for reading
  146.     ESP_LOGI(TAG, "Reading file %s", file_foo);
  147.     f = fopen(file_foo, "r");
  148.     if (f == NULL)
  149.     {
  150.         ESP_LOGE(TAG, "Failed to open file for reading");
  151.         return;
  152.     }
  153.  
  154.     // Read a line from file
  155.     char line[64];
  156.     fgets(line, sizeof(line), f);
  157.     fclose(f);
  158.  
  159.     // Strip newline
  160.     char *pos = strchr(line, '\n');
  161.     if (pos)
  162.     {
  163.         *pos = '\0';
  164.     }
  165.     ESP_LOGI(TAG, "Read from file: '%s'", line);
  166.  
  167.     // All done, unmount partition and disable SDMMC peripheral
  168.     esp_vfs_fat_sdcard_unmount(mount_point, card);
  169.     ESP_LOGI(TAG, "Card unmounted");
  170. }
使用以上代码后串口打印的信息是:
  1. I (0) cpu_start: App cpu up.
  2. I (222) cpu_start: Pro cpu start user code
  3. I (222) cpu_start: cpu freq: 160000000
  4. I (222) cpu_start: Application information:
  5. I (225) cpu_start: Project name:     main
  6. I (230) cpu_start: App version:      3577b0a-dirty
  7. I (235) cpu_start: Compile time:     Nov 24 2021 13:39:56
  8. I (241) cpu_start: ELF file SHA256:  1ac03af898982e4b...
  9. I (247) cpu_start: ESP-IDF:          v4.4-dev-3235-g3e370c4296-dirty
  10. I (254) heap_init: Initializing. RAM available for dynamic allocation:
  11. I (262) heap_init: At 3FC94B58 len 0004B4A8 (301 KiB): D/IRAM
  12. I (268) heap_init: At 3FCE0000 len 0000EE34 (59 KiB): STACK/DRAM
  13. I (275) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
  14. I (281) spiram: Adding pool of 2048K of external SPI memory to heap allocator
  15. I (289) spi_flash: detected chip: generic
  16. I (293) spi_flash: flash io: dio
  17. I (298) cpu_start: Starting scheduler on PRO CPU.
  18. I (0) cpu_start: Starting scheduler on APP CPU.
  19. I (328) spiram: Reserving pool of 64K of internal memory for DMA/internal allocations
  20. I (328) SD_EMMC: Initializing SD card
  21. I (338) SD_EMMC: Using SDMMC peripheral
  22. I (338) SD_EMMC: Mounting filesystem
  23. I (348) gpio: GPIO[17]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  24. I (358) gpio: GPIO[8]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  25. I (368) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  26. I (368) gpio: GPIO[13]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  27. I (378) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  28. I (388) gpio: GPIO[11]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
  29. I (398) gpio: GPIO[10]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  30. I (408) gpio: GPIO[9]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  31. I (418) gpio: GPIO[46]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  32. I (428) gpio: GPIO[3]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  33. I (498) gpio: GPIO[11]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
  34. E (498) sdmmc_cmd: sdmmc_read_sectors_dma: sdmmc_send_cmd returned 0x109
  35. E (508) diskio_sdmmc: sdmmc_read_blocks failed (265)
  36. W (508) vfs_fat_sdmmc: failed to mount card (1)
  37. E (518) vfs_fat_sdmmc: mount_to_vfs failed (0xffffffff).
  38. E (518) SD_EMMC: Failed to mount filesystem. If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.
Attachments
5F03B207-CC5B-43df-B2FD-73E8C4A0877E.png
emmc接线原理图
5F03B207-CC5B-43df-B2FD-73E8C4A0877E.png (91.37 KiB) Viewed 4772 times

heipppppp
Posts: 10
Joined: Thu Sep 23, 2021 9:52 am

Re: [已解决]ESP32S3无法挂载EMMC芯片

Postby heipppppp » Wed Nov 24, 2021 8:13 am

我把
  1.  slot_config.width = 8;
改成
  1.  slot_config.width = 1;
就成功了,目前还不知道为什么,先去查找资料再说

Who is online

Users browsing this forum: No registered users and 95 guests