当前位置:   article > 正文

ESP32学习笔记(44)——SD卡使用(SPI方式_esp32 sd卡

esp32 sd卡

收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
img
img

如果你需要这些资料,可以戳这里获取

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

五、示例代码

根据 examples\storage\sd_card 中的例程修改

/\* SD card and FAT filesystem example.
 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 <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"

static const char \*TAG = "example";

#define MOUNT\_POINT "/sdcard"

// This example can use SDMMC and SPI peripherals to communicate with SD card.
// By default, SDMMC peripheral is used.
// To enable SPI mode, uncomment the following line:

// DMA channel to be used by the SPI peripheral
#define SPI\_DMA\_CHAN 1

// When testing SD and SPI modes, keep in mind that once the card has been
// initialized in SPI mode, it can not be reinitialized in SD mode without
// toggling power to the card.

// Pin mapping when using SPI mode.
// With this mapping, SD card can be used both in SPI and 1-line SD mode.
// Note that a pull-up on CS line is required in SD mode.
#define PIN\_NUM\_MISO 2
#define PIN\_NUM\_MOSI 15
#define PIN\_NUM\_CLK 14
#define PIN\_NUM\_CS 13

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 = {  // 文件系统挂载配置
        .format_if_mount_failed = true,                // 如果挂载失败:true会重新分区和格式化/false不会重新分区和格式化
        .max_files = 5,                                // 打开文件最大数量
        .allocation_unit_size = 16 \* 1024
    };
    sdmmc_card_t\* card;                        // SD / MMC卡信息结构
    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 SPI peripheral");

    sdmmc_host_t host = SDSPI\_HOST\_DEFAULT();
    spi_bus_config_t bus_cfg = {
        .mosi_io_num = PIN_NUM_MOSI,
        .miso_io_num = PIN_NUM_MISO,
        .sclk_io_num = PIN_NUM_CLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 4000,
    };
    // SPI总线初始化
    ret = spi\_bus\_initialize(host.slot, &bus_cfg, SPI_DMA_CHAN);
    if (ret != ESP_OK) {
        ESP\_LOGE(TAG, "Failed to initialize bus.");
        return;
    }

    // 这将初始化没有卡检测(CD)和写保护(WP)信号的插槽。
    // 如果您的主板有这些信号,请修改slot\_config.gpio\_cd和slot\_config.gpio\_wp。
    sdspi_device_config_t slot_config = SDSPI\_DEVICE\_CONFIG\_DEFAULT();
    slot_config.gpio_cs = PIN_NUM_CS;
    slot_config.host_id = host.slot;

    // 挂载文件系统
    ret = esp\_vfs\_fat\_sdspi\_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;
    }

    // Card has been initialized, print its properties
    sdmmc\_card\_print\_info(stdout, card);

    // 使用POSIX和C标准库函数来处理文件
    // First create a file.
    ESP\_LOGI(TAG, "Opening file");
    FILE\* f = fopen(MOUNT_POINT"/hello.txt", "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");

    // 重命名前检查目标文件是否存在
    struct stat st;
    if (stat(MOUNT_POINT"/foo.txt", &st) == 0) {
        // 删除(如果存在)
        unlink(MOUNT_POINT"/foo.txt");
    }

    // 重命名文件
    ESP\_LOGI(TAG, "Renaming file");
    if (rename(MOUNT_POINT"/hello.txt", MOUNT_POINT"/foo.txt") != 0) {
        ESP\_LOGE(TAG, "Rename failed");
        return;
    }

    // 读取文件
    ESP\_LOGI(TAG, "Reading file");
    f = fopen(MOUNT_POINT"/foo.txt", "r");    // 读取方式打开文件
    if (f == NULL) {
        ESP\_LOGE(TAG, "Failed to open file for reading");
        return;
    }
    char line[64];
    fgets(line, sizeof(line), f);    // 读取一行数据
    fclose(f);                       // 关闭文件
    // 在字符串中查找换行
    char\* pos = strchr(line, '\n'); 
    if (pos) {
        \*pos = '\0';                 // 替换为结束符
    }
    ESP\_LOGI(TAG, "Read from file: '%s'", line);

    // 卸载分区并禁用SDMMC或SPI外设
    esp\_vfs\_fat\_sdcard\_unmount(mount_point, card);
    ESP\_LOGI(TAG, "Card unmounted");

    // 卸载总线
    spi\_bus\_free(host.slot);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154

查看打印:


• 由 Leung 写于 2021 年 8 月 16 日

• 参考:ESP32 开发笔记(三)源码示例 9_SPI_SDCard 使用SPI总线实现TF卡文件系统示例

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

如果你需要这些资料,可以戳这里获取

式知识点,真正体系化!**

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新

需要这些体系化资料的朋友,可以加我V获取:vip1024c (备注嵌入式)

如果你需要这些资料,可以戳这里获取

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/794571
推荐阅读
相关标签
  

闽ICP备14008679号