arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
VFS_SDSPI.h
1#pragma once
2#include <string.h>
3
4#include "AudioTools/Disk/VFS.h"
5#include "esp_vfs_fat.h"
6#include "sdmmc_cmd.h"
7#ifdef SOC_SDMMC_IO_POWER_EXTERNAL
8#include "sd_pwr_ctrl_by_on_chip_ldo.h"
9#endif
10
11#if !defined(DEFAULT_CS)
12#if defined(AUDIOBOARD_SD)
13#define DEFAULT_CS 13
14#define DEFAULT_MOSI 15
15#define DEFAULT_MISO 2
16#define DEFAULT_CLK 14
17#else
18#define DEFAULT_CS SS
19#define DEFAULT_MOSI MOSI
20#define DEFAULT_MISO MISO
21#define DEFAULT_CLK SCK
22#endif
23#endif
24
25#ifndef DEFAULT_MAX_TRANSFER_SIZE
26#define DEFAULT_MAX_TRANSFER_SIZE 4000
27#endif
28
29namespace audio_tools {
30
40class VFS_SDSPI : public VFS {
41 public:
42 VFS_SDSPI(const char* mountPoint = "/sd") { mount_point = mountPoint; };
43 VFS_SDSPI(int CS, int MOSI, int MISO, int SCK,
44 const char* mountPoint = "/sd")
46 setPins(CS, MOSI, MISO, SCK);
47 }
48 void setPins(int CS, int MOSI, int MISO, int SCK) {
49 setCS(CS);
50 setMosi(MOSI);
51 setMiso(MISO);
52 setClk(SCK);
53 }
54 void setCS(int pin) { pin_cs = (gpio_num_t)pin; }
55 void setMosi(int pin) { pin_mosi = (gpio_num_t)pin; }
56 void setMiso(int pin) { pin_miso = (gpio_num_t)pin; }
57 void setClk(int pin) { pin_clk = (gpio_num_t)pin; }
58 void setMountPoint(const char* mp) { mount_point = mp; }
59 bool begin() {
60 esp_err_t ret;
61
62 // Options for mounting the filesystem.
63 // If format_if_mount_failed is set to true, SD card will be partitioned and
64 // formatted in case when mounting fails.
65 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
66 .format_if_mount_failed = format_if_mount_failed,
67 .max_files = max_files,
68 .allocation_unit_size = allocation_unit_size};
69 LOGI("Initializing SD card");
70
71 // Use settings defined above to initialize SD card and mount FAT
72 // filesystem. Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience
73 // functions. Please check its source code and implement error recovery when
74 // developing production applications.
75 LOGI("Using SPI peripheral");
76
77 // By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT
78 // (20MHz) For setting a specific frequency, use host.max_freq_khz (range
79 // 400kHz - 20MHz for SDSPI) Example: for fixed frequency of 10MHz, use
80 // host.max_freq_khz = 10000;
81 host = SDSPI_HOST_DEFAULT();
82
83 // For SoCs where the SD power can be supplied both via an internal or
84 // external (e.g. on-board LDO) power supply. When using specific IO pins
85 // (which can be used for ultra high-speed SDMMC) to connect to the SD card
86 // and the internal LDO power supply, we need to initialize the power supply
87 // first.
88#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
89
90 sd_pwr_ctrl_ldo_config_t ldo_config = {
91 .ldo_chan_id = CONFIG_SD_PWR_CTRL_LDO_IO_ID,
92 };
93 pwr_ctrl_handle = NULL;
94
95 ret = sd_pwr_ctrl_new_on_chip_ldo(&ldo_config, &pwr_ctrl_handle);
96 if (ret != ESP_OK) {
97 LOGE("Failed to create a new on-chip LDO power control driver");
98 return false;
99 }
100 host.pwr_ctrl_handle = pwr_ctrl_handle;
101#endif
102
103 spi_bus_config_t bus_cfg = {
104 .mosi_io_num = pin_mosi,
105 .miso_io_num = pin_miso,
106 .sclk_io_num = pin_clk,
107 .quadwp_io_num = -1,
108 .quadhd_io_num = -1,
109 .max_transfer_sz = max_transfer_sz,
110 };
111
112 ret = spi_bus_initialize((spi_host_device_t)host.slot, &bus_cfg,
113 SDSPI_DEFAULT_DMA);
114 if (ret != ESP_OK) {
115 LOGE("Failed to initialize bus.");
116 return false;
117 }
118
119 // This initializes the slot without card detect (CD) and write protect (WP)
120 // signals. Modify slot_config.gpio_cd and slot_config.gpio_wp if your board
121 // has these signals.
122 sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
123 slot_config.gpio_cs = pin_cs;
124 slot_config.host_id = (spi_host_device_t)host.slot;
125
126 LOGI("Mounting filesystem at %s", mount_point);
127 ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config,
128 &mount_config, &card);
129
130 if (ret != ESP_OK) {
131 if (ret == ESP_FAIL) {
132 LOGE("Failed to mount filesystem");
133 } else {
134 LOGE("Failed to initialize the card (%s)", esp_err_to_name(ret));
135 }
136 return false;
137 }
138 LOGI("Filesystem mounted");
139
140 // Card has been initialized, print its properties
141 sdmmc_card_print_info(stdout, card);
142 return true;
143 }
144
145 void end() {
146 if (card == nullptr) return;
147 // All done, unmount partition and disable SPI peripheral
148 esp_vfs_fat_sdcard_unmount(mount_point, card);
149 card = nullptr;
150
151 LOGI("Card unmounted");
152
153 // deinitialize the bus after all devices are removed
154 spi_bus_free((spi_host_device_t)host.slot);
155
156 // Deinitialize the power control driver if it was used
157#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
158
159 ret = sd_pwr_ctrl_del_on_chip_ldo(pwr_ctrl_handle);
160 if (ret != ESP_OK) {
161 LOGE("Failed to delete the on-chip LDO power control driver");
162 return;
163 }
164#endif
165 }
166
167 void setAllocationUnitSize(int size) { allocation_unit_size = size; }
168 void setMaxFiles(int files) { max_files = files; }
169 void setFormatIfMountFailed(bool format) { format_if_mount_failed = format; }
170
171 protected:
172 sdmmc_card_t* card = nullptr;
173 sd_pwr_ctrl_handle_t pwr_ctrl_handle;
174 sdmmc_host_t host;
175 gpio_num_t pin_cs = (gpio_num_t)DEFAULT_CS;
176 gpio_num_t pin_mosi = (gpio_num_t)DEFAULT_MOSI;
177 gpio_num_t pin_miso = (gpio_num_t)DEFAULT_MISO;
178 gpio_num_t pin_clk = (gpio_num_t)DEFAULT_CLK;
179 int max_transfer_sz = DEFAULT_MAX_TRANSFER_SIZE;
180 int max_files = 5;
181 bool format_if_mount_failed = false;
182 int allocation_unit_size = 16 * 1024;
183};
184} // namespace audio_tools
ESP32 Virtual File System for SPI SD. The default mount point is "/sdcard" DRAFT implementation: not ...
Definition VFS_SDSPI.h:40
bool begin()
mount the file systems
Definition VFS_SDSPI.h:59
void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS_SDSPI.h:58
void end()
unmount the file system
Definition VFS_SDSPI.h:145
Definition VFS.h:16
const char * mountPoint()
provides the actual mount point
Definition VFS.h:55
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10