14. SDIO
Schematic:
Download
Download the schematic from the Baidu Netdisk link in the Download Center, under Documentation Tutorial -> ESP-IDF.
Tested read/write on cards from 256 MB up to 32 GB, with the FatFs file system integrated.
Testing the SD Card via the SPI Interface
c
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_system.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_err.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "sdkconfig.h"
//#include "my_sd_fatfs.h"
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "esp_vfs_fat.h"
#include "driver/sdspi_host.h"
#include "driver/spi_common.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
//#define TAG "SPI_SDCard_Demo"
static const char *TAG = "SPI_SDCard_Demo";
// File system mount point
#define MOUNT_POINT "/sdcard"
// DMA channel
#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 cannot be re-initialized in SD mode without power-cycling the card.
#define PIN_NUM_MISO 3 // PIN_D0
#define PIN_NUM_MOSI 2 // PIN_CMD
#define PIN_NUM_CLK 1 // PIN_CLK
#define PIN_NUM_CS 8 // PIN_D3
static void get_fatfs_usage(size_t* out_total_bytes, size_t* out_free_bytes);
void SD_Fatfs_Init(void)
{
esp_err_t ret; // Result definition
sdmmc_card_t* card; // SD / MMC card structure
const char mount_point[] = MOUNT_POINT; // Mount point / root directory
char ReadFileBuff[64]; // File operation buffer
// File system mount configuration
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false, // If mount fails: true will repartition and reformat / false will not repartition and reformat
.max_files = 5, // Maximum number of open files
.allocation_unit_size = 16 * 1024 // Disk partition cluster size
};
ESP_LOGI(TAG,"Initializing SD Card");
ESP_LOGI(TAG,"Using SPI Peripheralr");
// Default SPI interface configuration
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
// SPI bus configuration
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 = 4 * 1024 * sizeof(uint8_t), // Maximum transfer size
};
host.slot = SPI3_HOST;// Use SPI3
// Initialize the SPI bus
ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK) {
ESP_LOGI(TAG,"Failed To Initialize Bus");
return;
}
// This initializes the slot without card-detect (CD) or write-protect (WP) signals.
// If your board has these signals, modify slot_config.gpio_cd and slot_config.gpio_wp.
// The development board does not connect these two signals.
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = PIN_NUM_CS;
slot_config.host_id = host.slot;
// Mount the file system
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGI(TAG,"Failed to mount filesystem If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
} else {
ESP_LOGI(TAG,"Failed to initialize the card %s Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
}
return;
}
// TF card initialized; print TF card properties
sdmmc_card_print_info(stdout, card);
// Print file system usage information
size_t bytes_total, bytes_free;
get_fatfs_usage(&bytes_total, &bytes_free);
ESP_LOGI(TAG,"FAT FS Total: %d MB, Free: %d MB", bytes_total / 1024, bytes_free / 1024);
// Use POSIX and C standard library functions to handle files.
ESP_LOGI(TAG,"Opening File");
FILE* f = fopen(MOUNT_POINT"/hello.txt", "w");// Create a file
if (f == NULL) {
ESP_LOGI(TAG,"Failed to open file for writing");
return;
}
fprintf(f, "ESP32-S3 Read-write test: Hello %s!\n", card->cid.name);
fclose(f);
ESP_LOGI(TAG,"File Written");
// Check whether the destination file exists before renaming
struct stat st;
if (stat(MOUNT_POINT"/foo.txt", &st) == 0) {
unlink(MOUNT_POINT"/foo.txt");// Delete it if it exists
}
// Rename the file
ESP_LOGI(TAG,"Renaming File");
if (rename(MOUNT_POINT"/hello.txt", MOUNT_POINT"/foo.txt") != 0) {
ESP_LOGI(TAG,"Rename Failed");
return;
}
// Read the file
ESP_LOGI(TAG,"Reading file");
f = fopen(MOUNT_POINT"/foo.txt", "r");// Open the file for reading
if (f == NULL) {
ESP_LOGI(TAG,"Failed to open file for reading");
return;
}
fgets(ReadFileBuff, sizeof(ReadFileBuff), f); // Read one line of data
fclose(f); // Close the file
char* pos = strchr(ReadFileBuff, '\n'); // Look for a newline in the string
if (pos) {
*pos = '\0'; // Replace with a terminator
}
ESP_LOGI(TAG,"Read from file: '%s' ",ReadFileBuff);
// Unmount the partition and disable the SDMMC or SPI peripheral
esp_vfs_fat_sdcard_unmount(mount_point, card);
ESP_LOGI(TAG,"Card Unmounted");
// Free the bus
spi_bus_free(host.slot);
}
static void get_fatfs_usage(size_t* out_total_bytes, size_t* out_free_bytes)
{
FATFS *fs;
size_t free_clusters;
int res = f_getfree("0:", &free_clusters, &fs);//
assert(res == FR_OK);
size_t total_sectors = (fs->n_fatent - 2) * fs->csize;
size_t free_sectors = free_clusters * fs->csize;
size_t sd_total = total_sectors/1024;
size_t sd_total_KB = sd_total*fs->ssize;
size_t sd_total_MB = (sd_total*fs->ssize)/1024;
size_t sd_free = free_sectors/1024;
size_t sd_free_KB = sd_free*fs->ssize;
size_t sd_free_MB = (sd_free*fs->ssize)/1024;
//printf("SD Cart sd_total_KB %d KByte\r\n ", sd_total_KB);
//printf("SD Cart sd_total_MB %d MByte\r\n ", sd_total_MB);
//printf("SD Cart sd_free_KB %d KByte\r\n ", sd_free_KB);
//printf("SD Cart sd_free_MB %d MByte\r\n ", sd_free_MB);
// Assume the total size is less than 4 GiB, which should be true for SPI Flash
if (out_total_bytes != NULL) {
*out_total_bytes = sd_total_KB;
}
if (out_free_bytes != NULL) {
*out_free_bytes = sd_free_KB;
}
}
// Main function
void app_main()
{
ESP_LOGI(TAG, "[APP] Start!~\r\n");
ESP_LOGI(TAG, "[APP] IDF Version is %d.%d.%d",ESP_IDF_VERSION_MAJOR,ESP_IDF_VERSION_MINOR,ESP_IDF_VERSION_PATCH);
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
vTaskDelay(1000 / portTICK_PERIOD_MS);
SD_Fatfs_Init();
}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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Display card information
Display the read/write test contents: