arduino-audio-tools
Loading...
Searching...
No Matches
VFS_SDMMC.h
Go to the documentation of this file.
1#pragma once
2#include <string.h>
3
5#include "driver/sdmmc_host.h"
6#include "esp_vfs_fat.h"
7#include "sd_protocol_types.h"
8#include "sdmmc_cmd.h"
9
10#ifdef SOC_SDMMC_IO_POWER_EXTERNAL
11#include "sd_pwr_ctrl_by_on_chip_ldo.h"
12#endif
13
14#define SDMMC_FREQ_DEFAULT \
15 20000
16#define SDMMC_FREQ_HIGHSPEED \
17 40000
18#define SDMMC_FREQ_PROBING 400
19#define SDMMC_FREQ_52M 52000
20#define SDMMC_FREQ_26M 26000
21#define SDMMC_FREQ_DDR50 50000
22#define SDMMC_FREQ_SDR50 100000
24#define DEFAULT_CLK 14
25#define DEFAULT_CMD 15
26#define DEFAULT_D0 2
27#define DEFAULT_D1 4
28#define DEFAULT_D2 12
29#define DEFAULT_D3 13
30
31#ifndef DEFAULT_ALLOCATION_SIZE
32#define DEFAULT_ALLOCATION_SIZE 16 * 1024
33#endif
34#ifndef DEFAULT_MAX_FILES
35#define DEFAULT_MAX_FILES 5
36#endif
37
38namespace audio_tools {
39
52class VFS_SDMMC : public VFS {
53 public:
54 enum class Speed { HS, UHS_SDR, UHS_DDR };
55 enum class BusWidth { Byte1 = 1, Byte4 = 4 };
56 VFS_SDMMC(const char* mountPoint = "/sdmmc") { mount_point = mountPoint; }
57 VFS_SDMMC(int pinClk, int pinCmd, int pinD0, int pinD1, int pinD2 = -1,
58 int pinD3 = -1, const char* mountPoint = "/sdmmc")
60 setBusWidth(pinD1 != -1 && pinD2 != -1 && pinD3 != -1 ? BusWidth::Byte4
63 }
64
65 void setPins(int pinClk, int pinCmd, int pinD0, int pinD1, int pinD2 = -1,
66 int pinD3 = -1) {
69 setD0(pinD0);
70 setD1(pinD1);
71 setD2(pinD2);
72 setD3(pinD3);
73 }
74 void setClk(int pin) { pin_clk = (gpio_num_t)pin; }
75 void setCmd(int pin) { pin_clk = (gpio_num_t)pin; }
76 void setD0(int pin) { pin_d0 = (gpio_num_t)pin; }
77 void setD1(int pin) { pin_d1 = (gpio_num_t)pin; }
78 void setD2(int pin) { pin_d2 = (gpio_num_t)pin; }
79 void setD3(int pin) { pin_d3 = (gpio_num_t)pin; }
80
81 void setMountPoint(const char* mp) { mount_point = mp; }
82 void setSpeed(Speed speed) { this->speed = speed; }
83 void setBusWidth(BusWidth bits) { bus_width = bits; }
84
85 bool begin() {
86 esp_err_t ret;
87
88 // Options for mounting the filesystem.
89 // If format_if_mount_failed is set to true, SD card will be partitioned and
90 // formatted in case when mounting fails.
92 .format_if_mount_failed = false,
93 .max_files = max_files,
94 .allocation_unit_size = allocation_unit_size};
95 LOGI("Initializing SD card");
96
97 // Use settings defined above to initialize SD card and mount FAT
98 // filesystem. Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience
99 // functions. Please check its source code and implement error recovery when
100 // developing production applications.
101
102 LOGI("Using SDMMC peripheral");
103
104 // By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT
105 // (20MHz) For setting a specific frequency, use host.max_freq_khz (range
106 // 400kHz - 40MHz for SDMMC) Example: for fixed frequency of 10MHz, use
107 // host.max_freq_khz = 10000;
109
110 switch (speed) {
111 case Speed::HS:
112 host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
113 break;
114 case Speed::UHS_SDR:
115 host.slot = SDMMC_HOST_SLOT_0;
116 host.max_freq_khz = SDMMC_FREQ_SDR50;
117 host.flags &= ~SDMMC_HOST_FLAG_DDR;
118 break;
119 case Speed::UHS_DDR:
120 host.slot = SDMMC_HOST_SLOT_0;
121 host.max_freq_khz = SDMMC_FREQ_DDR50;
122 break;
123 }
124
125 // For SoCs where the SD power can be supplied both via an internal or
126 // external (e.g. on-board LDO) power supply. When using specific IO pins
127 // (which can be used for ultra high-speed SDMMC) to connect to the SD
128 // card and the internal LDO power supply, we need to initialize the power
129 // supply first.
130#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
131
133 .ldo_chan_id = CONFIG_SD_PWR_CTRL_LDO_IO_ID,
134 };
136
138 if (ret != ESP_OK) {
139 LOGE("Failed to create a new on-chip LDO power control driver");
140 return;
141 }
142 host.pwr_ctrl_handle = pwr_ctrl_handle;
143#endif
144
145 // This initializes the slot without card detect (CD) and write protect (WP)
146 // signals. Modify slot_config.gpio_cd and slot_config.gpio_wp if your board
147 // has these signals.
149 // #if EXAMPLE_IS_UHS1
150 // slot_config.flags |= SDMMC_SLOT_FLAG_UHS1;
151 // #endif
152
153 // Set bus width to use:
154 slot_config.width = (int)bus_width;
155
156 // On chips where the GPIOs used for SD card can be configured, set them in
157 // the slot_config structure:
158#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
159 slot_config.clk = pin_clk;
160 slot_config.cmd = pin_cmd;
161 slot_config.d0 = pin_d0;
162 slot_config.d1 = pin_d1;
163 slot_config.d2 = pin_d2;
164 slot_config.d3 = pin_d3;
165#endif // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
166
167 // Enable internal pullups on enabled pins. The internal pullups
168 // are insufficient however, please make sure 10k external pullups are
169 // connected on the bus. This is for debug / example purpose only.
171
172 LOGI("Mounting filesystem");
174 &mount_config, &card);
175
176 if (ret != ESP_OK) {
177 if (ret == ESP_FAIL) {
178 LOGE("Failed to mount filesystem. ");
179 } else {
180 LOGE("Failed to initialize the card (%s). ", esp_err_to_name(ret));
181 }
182 return false;
183 }
184 LOGI("Filesystem mounted");
185
186 // Card has been initialized, print its properties
188 return true;
189 }
190
191 void end() {
192 if (card == nullptr) return;
193 // All done, unmount partition and disable SDMMC peripheral
195 LOGI("Card unmounted");
196 card = nullptr;
197
198 // Deinitialize the power control driver if it was used
199#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
200
202 if (ret != ESP_OK) {
203 LOGE("Failed to delete the on-chip LDO power control driver");
204 return;
205 }
206#endif
207 }
208
209 protected:
210 sdmmc_card_t* card = nullptr;
223};
224
225} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define DEFAULT_D3
Definition VFS_SDMMC.h:29
#define SDMMC_FREQ_HIGHSPEED
Definition VFS_SDMMC.h:16
#define DEFAULT_D1
Definition VFS_SDMMC.h:27
#define SDMMC_FREQ_SDR50
Definition VFS_SDMMC.h:22
#define DEFAULT_ALLOCATION_SIZE
Definition VFS_SDMMC.h:32
#define DEFAULT_D2
Definition VFS_SDMMC.h:28
#define DEFAULT_CLK
Definition VFS_SDMMC.h:24
#define DEFAULT_CMD
Definition VFS_SDMMC.h:25
#define SDMMC_FREQ_DDR50
Definition VFS_SDMMC.h:21
#define DEFAULT_MAX_FILES
Definition VFS_SDMMC.h:35
#define DEFAULT_D0
Definition VFS_SDMMC.h:26
ESP32 Virtual File System for SDMMC. The default mount point is "/sdcard" DRAFT implementation: not t...
Definition VFS_SDMMC.h:52
gpio_num_t pin_cmd
Definition VFS_SDMMC.h:218
int max_files
Definition VFS_SDMMC.h:213
gpio_num_t pin_d3
Definition VFS_SDMMC.h:222
void setSpeed(Speed speed)
Definition VFS_SDMMC.h:82
void setD3(int pin)
Definition VFS_SDMMC.h:79
sdmmc_host_t host
Definition VFS_SDMMC.h:211
gpio_num_t pin_d0
Definition VFS_SDMMC.h:219
gpio_num_t pin_d1
Definition VFS_SDMMC.h:220
bool begin()
mount the file systems
Definition VFS_SDMMC.h:85
VFS_SDMMC(int pinClk, int pinCmd, int pinD0, int pinD1, int pinD2=-1, int pinD3=-1, const char *mountPoint="/sdmmc")
Definition VFS_SDMMC.h:57
Speed speed
Definition VFS_SDMMC.h:215
void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS_SDMMC.h:81
void setD0(int pin)
Definition VFS_SDMMC.h:76
sd_pwr_ctrl_handle_t pwr_ctrl_handle
Definition VFS_SDMMC.h:212
gpio_num_t pin_clk
Definition VFS_SDMMC.h:217
void end()
unmount the file system
Definition VFS_SDMMC.h:191
void setClk(int pin)
Definition VFS_SDMMC.h:74
void setPins(int pinClk, int pinCmd, int pinD0, int pinD1, int pinD2=-1, int pinD3=-1)
Definition VFS_SDMMC.h:65
gpio_num_t pin_d2
Definition VFS_SDMMC.h:221
BusWidth bus_width
Definition VFS_SDMMC.h:216
VFS_SDMMC(const char *mountPoint="/sdmmc")
Definition VFS_SDMMC.h:56
void setBusWidth(BusWidth bits)
Definition VFS_SDMMC.h:83
Speed
Definition VFS_SDMMC.h:54
sdmmc_card_t * card
Definition VFS_SDMMC.h:210
size_t allocation_unit_size
Definition VFS_SDMMC.h:214
void setCmd(int pin)
Definition VFS_SDMMC.h:75
BusWidth
Definition VFS_SDMMC.h:55
void setD2(int pin)
Definition VFS_SDMMC.h:78
void setD1(int pin)
Definition VFS_SDMMC.h:77
Base class which uses c++ file functions. It is also used as base class for an ESP32 Virtual File Sys...
Definition VFS.h:18
const char * mount_point
Definition VFS.h:60
const char * mountPoint()
provides the actual mount point
Definition VFS.h:57
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508