arduino-audio-tools
Loading...
Searching...
No Matches
VFS_SDSPI.h
Go to the documentation of this file.
1#pragma once
2#include <string.h>
3
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);
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.
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;
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
91 .ldo_chan_id = CONFIG_SD_PWR_CTRL_LDO_IO_ID,
92 };
94
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
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
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.
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);
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
142 return true;
143 }
144
145 void end() {
146 if (card == nullptr) return;
147 // All done, unmount partition and disable SPI peripheral
149 card = nullptr;
150
151 LOGI("Card unmounted");
152
153 // deinitialize the bus after all devices are removed
155
156 // Deinitialize the power control driver if it was used
157#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
158
160 if (ret != ESP_OK) {
161 LOGE("Failed to delete the on-chip LDO power control driver");
162 return;
163 }
164#endif
165 }
166
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;
180 int max_files = 5;
182 int allocation_unit_size = 16 * 1024;
183};
184} // namespace audio_tools
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define DEFAULT_CLK
Definition VFS_SDMMC.h:24
#define DEFAULT_MISO
Definition VFS_SDSPI.h:20
#define DEFAULT_MAX_TRANSFER_SIZE
Definition VFS_SDSPI.h:26
#define DEFAULT_MOSI
Definition VFS_SDSPI.h:19
#define DEFAULT_CS
Definition VFS_SDSPI.h:18
ESP32 Virtual File System for SPI SD. The default mount point is "/sdcard" DRAFT implementation: not ...
Definition VFS_SDSPI.h:40
gpio_num_t pin_cs
Definition VFS_SDSPI.h:175
int max_transfer_sz
Definition VFS_SDSPI.h:179
int max_files
Definition VFS_SDSPI.h:180
void setMiso(int pin)
Definition VFS_SDSPI.h:56
void setPins(int CS, int MOSI, int MISO, int SCK)
Definition VFS_SDSPI.h:48
VFS_SDSPI(const char *mountPoint="/sd")
Definition VFS_SDSPI.h:42
sdmmc_host_t host
Definition VFS_SDSPI.h:174
int allocation_unit_size
Definition VFS_SDSPI.h:182
bool begin()
mount the file systems
Definition VFS_SDSPI.h:59
void setFormatIfMountFailed(bool format)
Definition VFS_SDSPI.h:169
void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS_SDSPI.h:58
void setMaxFiles(int files)
Definition VFS_SDSPI.h:168
void setAllocationUnitSize(int size)
Definition VFS_SDSPI.h:167
sd_pwr_ctrl_handle_t pwr_ctrl_handle
Definition VFS_SDSPI.h:173
void setCS(int pin)
Definition VFS_SDSPI.h:54
void setMosi(int pin)
Definition VFS_SDSPI.h:55
gpio_num_t pin_clk
Definition VFS_SDSPI.h:178
void end()
unmount the file system
Definition VFS_SDSPI.h:145
void setClk(int pin)
Definition VFS_SDSPI.h:57
gpio_num_t pin_miso
Definition VFS_SDSPI.h:177
VFS_SDSPI(int CS, int MOSI, int MISO, int SCK, const char *mountPoint="/sd")
Definition VFS_SDSPI.h:43
sdmmc_card_t * card
Definition VFS_SDSPI.h:172
bool format_if_mount_failed
Definition VFS_SDSPI.h:181
gpio_num_t pin_mosi
Definition VFS_SDSPI.h:176
Base class which uses c++ file functions. It is also used as base class for an ESP32 Virtual File Sys...
Definition VFS.h:15
const char * mount_point
Definition VFS.h:57
const char * mountPoint()
provides the actual mount point
Definition VFS.h:54
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:512