3#if defined(ROO_TESTING)
5#include "roo_testing/microcontrollers/esp32/fake_esp32.h"
9#if (defined ESP_PLATFORM || defined ROO_TESTING)
11#if !defined(MLOG_roo_io_fs)
12#define MLOG_roo_io_fs 0
15#include "driver/sdspi_host.h"
16#include "driver/spi_common.h"
17#include "driver/spi_master.h"
18#include "esp_vfs_fat.h"
20#include "roo_logging.h"
29MountImpl::MountResult SdSpiFs::mountImpl(std::function<
void()> unmount_fn) {
30 MLOG(roo_io_fs) <<
"Mounting SD card";
31#if defined(ROO_TESTING)
32 mount_base_path_ = FakeEsp32().fs_root();
34 mount_base_path_.clear();
36 mount_base_path_.append(mountPoint());
40 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
42 sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
43 dev_config.host_id = spi_host_;
44 dev_config.gpio_cs = cs_pin_;
46 esp_vfs_fat_mount_config_t mount_config = {
47 .format_if_mount_failed = formatIfMountFailed(),
48 .max_files = maxOpenFiles(),
49 .allocation_unit_size = 16 * 1024};
51 ret = esp_vfs_fat_sdspi_mount(mount_base_path_.c_str(), &host, &dev_config,
52 &mount_config, &card_);
55 if (ret == ESP_FAIL) {
56 LOG(ERROR) <<
"Failed to mount filesystem. If you want the card to be "
57 "formatted, use setFormatIfEmpty(true)";
58 return MountImpl::MountError(kGenericMountError);
59 }
else if (ret == ESP_ERR_TIMEOUT) {
60 return MountImpl::MountError(kNoMedia);
62 LOG(ERROR) <<
"Failed to initialize the card (" << esp_err_to_name(ret)
65 return MountImpl::MountError(kGenericMountError);
68 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
69 new PosixMountImpl(mount_base_path_.c_str(), readOnly(), unmount_fn)));
72void SdSpiFs::unmountImpl() {
73 MLOG(roo_io_fs) <<
"Unmounting SD card";
76 esp_err_t ret = esp_vfs_fat_sdcard_unmount(mount_base_path_.c_str(), card_);
78 LOG(ERROR) <<
"Unmounting card failed: " << esp_err_to_name(ret);
81 mount_base_path_.clear();
84Filesystem::MediaPresence SdSpiFs::checkMediaPresence() {
85 return kMediaPresenceUnknown;
88SdSpiFs CreateSdSpiFs() {
return SdSpiFs(); }
90SdSpiFs SDSPI = CreateSdSpiFs();
roo::basic_string_view< CharT, Traits > basic_string_view