[已解决]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芯片作为外部存储,但是当我稍微修改例程后,发现无法挂载文件系统
代码:

Code: Untitled.c Select all

#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#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 = '\0';
}
ESP_LOGI(TAG, "Read from file: '%s'", line);

// All done, unmount partition and disable SDMMC peripheral
esp_vfs_fat_sdcard_unmount(mount_point, card);
ESP_LOGI(TAG, "Card unmounted");
}
使用以上代码后串口打印的信息是:

Code: Untitled.txt Select all

I (0) cpu_start: App cpu up.
I (222) cpu_start: Pro cpu start user code
I (222) cpu_start: cpu freq: 160000000
I (222) cpu_start: Application information:
I (225) cpu_start: Project name: main
I (230) cpu_start: App version: 3577b0a-dirty
I (235) cpu_start: Compile time: Nov 24 2021 13:39:56
I (241) cpu_start: ELF file SHA256: 1ac03af898982e4b...
I (247) cpu_start: ESP-IDF: v4.4-dev-3235-g3e370c4296-dirty
I (254) heap_init: Initializing. RAM available for dynamic allocation:
I (262) heap_init: At 3FC94B58 len 0004B4A8 (301 KiB): D/IRAM
I (268) heap_init: At 3FCE0000 len 0000EE34 (59 KiB): STACK/DRAM
I (275) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (281) spiram: Adding pool of 2048K of external SPI memory to heap allocator
I (289) spi_flash: detected chip: generic
I (293) spi_flash: flash io: dio
I (298) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (328) spiram: Reserving pool of 64K of internal memory for DMA/internal allocations
I (328) SD_EMMC: Initializing SD card
I (338) SD_EMMC: Using SDMMC peripheral
I (338) SD_EMMC: Mounting filesystem
I (348) gpio: GPIO[17]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (358) gpio: GPIO[8]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (368) gpio: GPIO[14]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (368) gpio: GPIO[13]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (378) gpio: GPIO[12]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (388) gpio: GPIO[11]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (398) gpio: GPIO[10]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (408) gpio: GPIO[9]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (418) gpio: GPIO[46]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (428) gpio: GPIO[3]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (498) gpio: GPIO[11]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
E (498) sdmmc_cmd: sdmmc_read_sectors_dma: sdmmc_send_cmd returned 0x109
E (508) diskio_sdmmc: sdmmc_read_blocks failed (265)
W (508) vfs_fat_sdmmc: failed to mount card (1)
E (518) vfs_fat_sdmmc: mount_to_vfs failed (0xffffffff).
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 5800 times

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

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

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

我把

Code: Untitled.c Select all

 slot_config.width = 8;
改成

Code: Untitled.c Select all

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

Who is online

Users browsing this forum: No registered users and 1 guest