文章目录

  • 1 前言
  • 2 硬件
  • 3 代码
  • 4 结果
  • 5 问题

1 前言

本章使用esp32s3接tf卡,使用的是spi。

2 硬件

3 代码

代码来源:https://gitee.com/EspressifSystems/esp-idf/tree/master/examples/storage/sd_card/sdspi

使用的是示例,主要修改了引脚信息,以及频率,默认就是20M。

/* SD card and FAT filesystem example.This example uses SPI peripheral to communicate with SD card.This example code is in the Public Domain (or CC0 licensed, at your option.)Unless required by applicable law or agreed to in writing, thissoftware is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES ORCONDITIONS OF ANY KIND, either express or implied.
*/#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"static const char *TAG = "example";#define MOUNT_POINT "/sdcard"// Pin assignments can be set in menuconfig, see "SD SPI Example Configuration" menu.
// You can also change the pin assignments here by changing the following 4 lines.
#define PIN_NUM_MISO  37
#define PIN_NUM_MOSI  35
#define PIN_NUM_CLK   36
#define PIN_NUM_CS    34void 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 SPI peripheral");sdmmc_host_t host = SDSPI_HOST_DEFAULT();host.max_freq_khz = 20000;     //设置clkspi_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,};ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);if (ret != ESP_OK) {ESP_LOGE(TAG, "Failed to initialize bus.");return;}// 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.sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();slot_config.gpio_cs = PIN_NUM_CS;slot_config.host_id = host.slot;ESP_LOGI(TAG, "Mounting filesystem");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 CONFIG_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 propertiessdmmc_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 renamingstruct stat st;if (stat(file_foo, &st) == 0) {// Delete it if it existsunlink(file_foo);}// Rename original fileESP_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 readingESP_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 filechar line[64];fgets(line, sizeof(line), f);fclose(f);// Strip newlinechar *pos = strchr(line, '\n');if (pos) {*pos = '\0';}ESP_LOGI(TAG, "Read from file: '%s'", line);// All done, unmount partition and disable SPI peripheralesp_vfs_fat_sdcard_unmount(mount_point, card);ESP_LOGI(TAG, "Card unmounted");//deinitialize the bus after all devices are removedspi_bus_free(host.slot);
}

4 结果

5 问题

需要注意部分tf卡模块未接上拉电阻,会报初始化失败。

我犯了一个错误,是gnd未接,因为是面包板电源划分为两块,所以gnd没接。

【esp32-s3】6.1 文件系统——spi挂载tf卡相关推荐

  1. 【Luat-esp32c3】4.2 文件系统——sdmmc挂载tf卡

    文章目录 1 前言 2 硬件 3 固件 4 官方示例 5 结果 1 前言 esp32挂载tf卡. 2 硬件 3 固件 https://gitee.com/dreamcmi/LuatOS-ESP32/r ...

  2. Arduino ESP32 使用HSPI和VSPI接口挂载SD卡区别

    Arduino ESP32 使用HSPI和VSPI接口挂载SD卡区别 在挂载SD卡时,ESP32使用HSPI和VSPI接口挂载SD卡区别 如果使用hard SPI(HSPI)接口需要注意以下几点: 烧 ...

  3. linux u盘分区 mdev 卸载问题,迅为开发板最小Linux自动挂载TF卡/U盘等存储设备

    原标题:迅为开发板最小Linux自动挂载TF卡/U盘等存储设备 本文转自迅为论坛:http://www.topeetboard.com 通过 mdev 工具实现 U 盘和 SD/TF 卡的自动挂载步骤 ...

  4. 模拟SPI进行TF卡操作+Fatfs文件系统移植

    FATFS版本:FATFS R0.13b SD卡容量:16G 概述 本文的重点是进行Fatfs文件系统的移植和初步的使用.TF卡的操作实际上是指令操作,即你想它发送固定的CMD指令,它接收到指令给你返 ...

  5. 【无标题】STM32F407VGT6文件系统挂载TF卡学习笔记CUBEMXKEIL5

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 目录 前言 一.CUBEMX配置 二.添加代码main.c文件 1.main函数定义变量 2.文件系统函数 三.调试结果 总结 前言 ...

  6. Android5.1设备无法识别exFAT文件系统的64G TF卡问题

    64G TF卡刚买回来的时候默认exFAT文件系统,在电脑端(XP和WIN7)可以识别,但在我们Android5.1S设备无法识别,采用guiformat工具格式化为FAT32文件系统后才可以正常识别 ...

  7. Jetson-Nano挂载TF卡/挂载磁盘

    Nano挂在TF卡有两种方式 .一种是使用nano自带的DISK工具,第二种是使用命令. 第一种方式请参考答主制作TF启动文章文章,在这里就不复述 第二种:使用指令 第一步:查看有哪些数据盘可以挂载: ...

  8. 【中移芯昇】5. spi接口测试tf卡

    文章目录 1 前言 2 源码 3 添加头文件 4 硬件 5 代码 6 结果 1 前言 本章测试spi tf卡示例,实现txt文件的读写. 2 源码 源码路径: https://gitee.com/CM ...

  9. 安卓挂载tf卡到自定义目录

    需求 最近有个需求,为了兼容之前的固件,需要把tf卡映射到指定路径.然后通过查询资料,最简单的方法就是在挂载的时候进行一个链接操作.实际只需要修改vold可执行文件就可以了. 修改方法 只需要修改如下 ...

  10. stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)

    借鉴于正点原子stm32 Mini板 下方的代码仅为对TF卡的初始化 FATFS移植方法请看正点原子Mini板教程,以及建议完成USMART的移植 因为TFATFS移植与USMATRT没有什么好写的, ...

最新文章

  1. getComputedStyle currentStyle 获取当前元素所有最终使用的CSS属性值
  2. db2 日期英式写法_《学霸英语》16:美国人和英国人“表达日期”,差距竟然这么大!...
  3. 求素数——多线程练习
  4. Java方法中的参数太多,第3部分:构建器模式
  5. bzoj1049[HAOI2006]数字序列
  6. [导入][导入][c#]Web开发中Tag的开发技巧
  7. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第3节 线程同步机制_5_同步技术的原理...
  8. Outlook收件箱无法看到新邮件而OWA的收件箱可以
  9. VScode远程连接linux
  10. java数据结构银行叫号_数据结构C语言版利用队列结构实现银行叫号系统要..._结构工程师_帮考网...
  11. CRUISE软件测试工程师,CruiseControl.NET配置
  12. 小米6通话音量补丁_手机通话声音小?只需打开这个开关,音量既大又清晰
  13. nodejs mysql嵌套查询_nodejs+mysql嵌套查询的问题
  14. [ delphi ] AES-256-ECB 加密、解密算法控件说明
  15. 下列不是SQL的服务器组件,北语网院15秋《数据库应用(SQL server)》作业3答案...
  16. RSAT(Regulatory Sequence Analysis Tools)详解
  17. C# 超市满减打折优惠
  18. brpc源码解析(四)—— Bthread机制
  19. Vue:前端体系与前后端分离
  20. Excel如何批量删除工作表全部图片?

热门文章

  1. 常用计算机病毒表及其专杀工具,维金病毒-谁可以提供一个可以在win98平台下使用的维金病毒专杀工具(好象是....
  2. 啦啦外卖修改飞鹅打印机的打印模板
  3. 频繁默认网关不可用_电脑经常掉线提示默认网关不可用原因分析和解决办法
  4. 2019年 CCF 中国计算机学会推荐国际学术会议和期刊目录 最新版
  5. 我的编程之路点滴记录(五)
  6. 企鹅龙(DRBL)无盘启动+再生龙(clonezilla)网络备份与还原系统
  7. Day 23 What Drivers You Crazy
  8. IE 提示 当前安全设置不允许下载该文件
  9. icom对讲机写频线定义_哈罗CQ火腿社区 - QRP and DIY - 各种写频线的资料,放上来备用吧 - Powered by phpwind...
  10. 全面揭露网络交易出现的十大欺诈骗术