VScode+esp-idf:安信可esp32-cam开发板测试sd卡


本文介绍如何使用esp-idf例程测试sd卡。
参考文章:
1.作者:Leung_ManWah,《 ESP32学习笔记(43)-- SD卡使用(SDMMC方式)》。

1.拷贝例程到目标工作区

源文件的位置在安装目录 esp-idf 下面的 example
在这里插入图片描述
用vscode打开 example,打开方法参考《VScode+ESP32快速安装ESP-IDF插件》的“4.测试编译例程”。
在这里插入图片描述
创建好新工程后,原文件会拷贝一份到工作区。
进入下一步。

2.配置并编译工程

进入配置工程:
在这里插入图片描述
主要是确认一下4线接法:
在这里插入图片描述
esp32-cam的sd卡引脚使用图:
在这里插入图片描述
保存配置,编译工程。
在这里插入图片描述
编译完成。
在这里插入图片描述

3.代码逻辑流程

这里抄录参考文章的代码流程:

  1. 使用“一体式” esp_vfs_fat_sdmmc_mount()函数进行初始化:
  • 初始化SDMMC外设;
  • 检测并初始化连接到SD/MMC插槽1的卡(HS2_CMD、HS2_CLK、HS2_D0、HS2_D1、HS2_D2、HS2_D3线);
  • 使用FATFS库安装FAT文件系统(如果无法安装文件系统,则使用格式化卡);
  • 在VFS中注册FAT文件系统,以使用C标准库和POSIX函数。
  1. 打印有关SD卡的信息,例如名称、类型、容量和支持的最大频率。
  2. 使用fopen()创建一个文件,并使用fprintf()写入该文件。
  3. 重命名该文件。重命名之前,请使用stat()函数检查目标文件是否已存在,并使用unlink()函数将其删除。
  4. 打开重命名的文件进行读取,读回该行,并将其打印到终端。

4.烧录并运行程序

选择串口,烧录方式,是开发板处于烧录状态,点击烧录按键:
在这里插入图片描述
烧录完成后,运行程序,打印以下信息:

I (330) example: Initializing SD card
I (330) example: Using SDMMC peripheral
I (340) example: Mounting filesystem
I (340) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (540) example: Filesystem mounted
Name: SS16G
Type: SDHC/SDXC
Speed: 20 MHz
Size: 15193MB
I (540) example: Opening file /sdcard/hello.txt
I (620) example: File written
I (620) example: Renaming file /sdcard/hello.txt to /sdcard/foo.txt
I (630) example: Reading file /sdcard/foo.txt
I (630) example: Read from file: 'Hello SS16G!'
I (630) example: Card unmounted

给开发板断电,取下sd卡,用读卡器在电脑上查看:
在这里插入图片描述

5.源代码main()

static const char *TAG = "example";

#define MOUNT_POINT "/sdcard"


void app_main(void)
{
    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 = {
#ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
        .format_if_mount_failed = true,
#else
        .format_if_mount_failed = false,
#endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
        .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();

    // 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();

    // Set bus width to use:
#ifdef CONFIG_EXAMPLE_SDMMC_BUS_WIDTH_4
    slot_config.width = 4;
#else
    slot_config.width = 1;
#endif

    // On chips where the GPIOs used for SD card can be configured, set them in
    // the slot_config structure:
#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
    slot_config.clk = CONFIG_EXAMPLE_PIN_CLK;
    slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD;
    slot_config.d0 = CONFIG_EXAMPLE_PIN_D0;
#ifdef CONFIG_EXAMPLE_SDMMC_BUS_WIDTH_4
    slot_config.d1 = CONFIG_EXAMPLE_PIN_D1;
    slot_config.d2 = CONFIG_EXAMPLE_PIN_D2;
    slot_config.d3 = CONFIG_EXAMPLE_PIN_D3;
#endif  // CONFIG_EXAMPLE_SDMMC_BUS_WIDTH_4
#endif  // CONFIG_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;

    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");
}
posted @ 2022-09-03 15:12  汉塘阿德  阅读(178)  评论(0编辑  收藏  举报  来源