51 enum class Speed { HS, UHS_SDR, UHS_DDR };
52 enum class BusWidth { Byte1 = 1, Byte4 = 4 };
54 VFS_SDMMC(
int pinClk,
int pinCmd,
int pinD0,
int pinD1,
int pinD2 = -1,
55 int pinD3 = -1,
const char*
mountPoint =
"/sdmmc")
57 setBusWidth(pinD1 != -1 && pinD2 != -1 && pinD3 != -1 ? BusWidth::Byte4
59 setPins(pinClk, pinCmd, pinD0, pinD1, pinD2, pinD3);
62 void setPins(
int pinClk,
int pinCmd,
int pinD0,
int pinD1,
int pinD2 = -1,
71 void setClk(
int pin) { pin_clk = (gpio_num_t)pin; }
72 void setCmd(
int pin) { pin_clk = (gpio_num_t)pin; }
73 void setD0(
int pin) { pin_d0 = (gpio_num_t)pin; }
74 void setD1(
int pin) { pin_d1 = (gpio_num_t)pin; }
75 void setD2(
int pin) { pin_d2 = (gpio_num_t)pin; }
76 void setD3(
int pin) { pin_d3 = (gpio_num_t)pin; }
79 void setSpeed(Speed speed) { this->speed = speed; }
80 void setBusWidth(BusWidth bits) { bus_width = bits; }
88 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
89 .format_if_mount_failed =
false,
90 .max_files = max_files,
91 .allocation_unit_size = allocation_unit_size};
92 LOGI(
"Initializing SD card");
99 LOGI(
"Using SDMMC peripheral");
105 host = SDMMC_HOST_DEFAULT();
109 host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
112 host.slot = SDMMC_HOST_SLOT_0;
113 host.max_freq_khz = SDMMC_FREQ_SDR50;
114 host.flags &= ~SDMMC_HOST_FLAG_DDR;
117 host.slot = SDMMC_HOST_SLOT_0;
118 host.max_freq_khz = SDMMC_FREQ_DDR50;
127#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
129 sd_pwr_ctrl_ldo_config_t ldo_config = {
130 .ldo_chan_id = CONFIG_SD_PWR_CTRL_LDO_IO_ID,
132 pwr_ctrl_handle = NULL;
134 ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
136 LOGE(
"Failed to create a new on-chip LDO power control driver");
139 host.pwr_ctrl_handle = pwr_ctrl_handle;
145 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
151 slot_config.width = (int)bus_width;
155#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
156 slot_config.clk = pin_clk;
157 slot_config.cmd = pin_cmd;
158 slot_config.d0 = pin_d0;
159 slot_config.d1 = pin_d1;
160 slot_config.d2 = pin_d2;
161 slot_config.d3 = pin_d3;
167 slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
169 LOGI(
"Mounting filesystem");
170 ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config,
171 &mount_config, &card);
174 if (ret == ESP_FAIL) {
175 LOGE(
"Failed to mount filesystem. ");
177 LOGE(
"Failed to initialize the card (%s). ", esp_err_to_name(ret));
181 LOGI(
"Filesystem mounted");
184 sdmmc_card_print_info(stdout, card);
189 if (card ==
nullptr)
return;
191 esp_vfs_fat_sdcard_unmount(mount_point, card);
192 LOGI(
"Card unmounted");
196#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
198 ret = sd_pwr_ctrl_del_on_chip_ldo(pwr_ctrl_handle);
200 LOGE(
"Failed to delete the on-chip LDO power control driver");
207 sdmmc_card_t* card =
nullptr;
209 sd_pwr_ctrl_handle_t pwr_ctrl_handle;
210 int max_files = DEFAULT_MAX_FILES;
211 size_t allocation_unit_size = DEFAULT_ALLOCATION_SIZE;
212 Speed speed = Speed::HS;
213 BusWidth bus_width = BusWidth::Byte4;
214 gpio_num_t pin_clk = (gpio_num_t)DEFAULT_CLK;
215 gpio_num_t pin_cmd = (gpio_num_t)DEFAULT_CMD;
216 gpio_num_t pin_d0 = (gpio_num_t)DEFAULT_D0;
217 gpio_num_t pin_d1 = (gpio_num_t)DEFAULT_D1;
218 gpio_num_t pin_d2 = (gpio_num_t)DEFAULT_D2;
219 gpio_num_t pin_d3 = (gpio_num_t)DEFAULT_D3;