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
43class VFS_SDSPI : public VFS {
44 public:
45 VFS_SDSPI(const char* mountPoint = "/sd") { mount_point = mountPoint; };
46 VFS_SDSPI(int CS, int MOSI, int MISO, int SCK,
47 const char* mountPoint = "/sd")
49 setPins(CS, MOSI, MISO, SCK);
50 }
51 void setPins(int CS, int MOSI, int MISO, int SCK) {
52 setCS(CS);
55 setClk(SCK);
56 }
57 void setCS(int pin) { pin_cs = (gpio_num_t)pin; }
58 void setMosi(int pin) { pin_mosi = (gpio_num_t)pin; }
59 void setMiso(int pin) { pin_miso = (gpio_num_t)pin; }
60 void setClk(int pin) { pin_clk = (gpio_num_t)pin; }
61 void setMountPoint(const char* mp) { mount_point = mp; }
62 bool begin() {
63 esp_err_t ret;
64
65 // Options for mounting the filesystem.
66 // If format_if_mount_failed is set to true, SD card will be partitioned and
67 // formatted in case when mounting fails.
69 .format_if_mount_failed = format_if_mount_failed,
70 .max_files = max_files,
71 .allocation_unit_size = allocation_unit_size};
72 LOGI("Initializing SD card");
73
74 // Use settings defined above to initialize SD card and mount FAT
75 // filesystem. Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience
76 // functions. Please check its source code and implement error recovery when
77 // developing production applications.
78 LOGI("Using SPI peripheral");
79
80 // By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT
81 // (20MHz) For setting a specific frequency, use host.max_freq_khz (range
82 // 400kHz - 20MHz for SDSPI) Example: for fixed frequency of 10MHz, use
83 // host.max_freq_khz = 10000;
85
86 // For SoCs where the SD power can be supplied both via an internal or
87 // external (e.g. on-board LDO) power supply. When using specific IO pins
88 // (which can be used for ultra high-speed SDMMC) to connect to the SD card
89 // and the internal LDO power supply, we need to initialize the power supply
90 // first.
91#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
92
94 .ldo_chan_id = CONFIG_SD_PWR_CTRL_LDO_IO_ID,
95 };
97
99 if (ret != ESP_OK) {
100 LOGE("Failed to create a new on-chip LDO power control driver");
101 return false;
102 }
103 host.pwr_ctrl_handle = pwr_ctrl_handle;
104#endif
105
107 .mosi_io_num = pin_mosi,
108 .miso_io_num = pin_miso,
109 .sclk_io_num = pin_clk,
110 .quadwp_io_num = -1,
111 .quadhd_io_num = -1,
112 .max_transfer_sz = max_transfer_sz,
113 };
114
117 if (ret != ESP_OK) {
118 LOGE("Failed to initialize bus.");
119 return false;
120 }
121
122 // This initializes the slot without card detect (CD) and write protect (WP)
123 // signals. Modify slot_config.gpio_cd and slot_config.gpio_wp if your board
124 // has these signals.
126 slot_config.gpio_cs = pin_cs;
127 slot_config.host_id = (spi_host_device_t)host.slot;
128
129 LOGI("Mounting filesystem at %s", mount_point);
131 &mount_config, &card);
132
133 if (ret != ESP_OK) {
134 if (ret == ESP_FAIL) {
135 LOGE("Failed to mount filesystem");
136 } else {
137 LOGE("Failed to initialize the card (%s)", esp_err_to_name(ret));
138 }
139 return false;
140 }
141 LOGI("Filesystem mounted");
142
143 // Card has been initialized, print its properties
145 return true;
146 }
147
148 void end() {
149 if (card == nullptr) return;
150 // All done, unmount partition and disable SPI peripheral
152 card = nullptr;
153
154 LOGI("Card unmounted");
155
156 // deinitialize the bus after all devices are removed
158
159 // Deinitialize the power control driver if it was used
160#ifdef CONFIG_SD_PWR_CTRL_LDO_IO_ID
161
163 if (ret != ESP_OK) {
164 LOGE("Failed to delete the on-chip LDO power control driver");
165 return;
166 }
167#endif
168 }
169
171 void setMaxFiles(int files) { max_files = files; }
172 void setFormatIfMountFailed(bool format) { format_if_mount_failed = format; }
173
174 protected:
175 sdmmc_card_t* card = nullptr;
183 int max_files = 5;
185 int allocation_unit_size = 16 * 1024;
186};
187} // 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:43
gpio_num_t pin_cs
Definition VFS_SDSPI.h:178
int max_transfer_sz
Definition VFS_SDSPI.h:182
int max_files
Definition VFS_SDSPI.h:183
void setMiso(int pin)
Definition VFS_SDSPI.h:59
void setPins(int CS, int MOSI, int MISO, int SCK)
Definition VFS_SDSPI.h:51
VFS_SDSPI(const char *mountPoint="/sd")
Definition VFS_SDSPI.h:45
sdmmc_host_t host
Definition VFS_SDSPI.h:177
int allocation_unit_size
Definition VFS_SDSPI.h:185
bool begin()
mount the file systems
Definition VFS_SDSPI.h:62
void setFormatIfMountFailed(bool format)
Definition VFS_SDSPI.h:172
void setMountPoint(const char *mp)
provide the mount point (root directory for the file system)
Definition VFS_SDSPI.h:61
void setMaxFiles(int files)
Definition VFS_SDSPI.h:171
void setAllocationUnitSize(int size)
Definition VFS_SDSPI.h:170
sd_pwr_ctrl_handle_t pwr_ctrl_handle
Definition VFS_SDSPI.h:176
void setCS(int pin)
Definition VFS_SDSPI.h:57
void setMosi(int pin)
Definition VFS_SDSPI.h:58
gpio_num_t pin_clk
Definition VFS_SDSPI.h:181
void end()
unmount the file system
Definition VFS_SDSPI.h:148
void setClk(int pin)
Definition VFS_SDSPI.h:60
gpio_num_t pin_miso
Definition VFS_SDSPI.h:180
VFS_SDSPI(int CS, int MOSI, int MISO, int SCK, const char *mountPoint="/sd")
Definition VFS_SDSPI.h:46
sdmmc_card_t * card
Definition VFS_SDSPI.h:175
bool format_if_mount_failed
Definition VFS_SDSPI.h:184
gpio_num_t pin_mosi
Definition VFS_SDSPI.h:179
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