roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
sdspi.cpp
Go to the documentation of this file.
2
3#if defined(ROO_TESTING)
4
5#include "roo_testing/microcontrollers/esp32/fake_esp32.h"
6
7#endif
8
9#if (defined ESP_PLATFORM || defined ROO_TESTING)
10
11#if !defined(MLOG_roo_io_fs)
12#define MLOG_roo_io_fs 0
13#endif
14
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"
21
22namespace roo_io {
23
28
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();
33#else
34 mount_base_path_.clear();
35#endif
36 mount_base_path_.append(mountPoint());
37
38 esp_err_t ret;
39
40 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
41
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_;
45
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};
50
51 ret = esp_vfs_fat_sdspi_mount(mount_base_path_.c_str(), &host, &dev_config,
52 &mount_config, &card_);
53
54 if (ret != ESP_OK) {
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);
61 } else {
62 LOG(ERROR) << "Failed to initialize the card (" << esp_err_to_name(ret)
63 << ").";
64 }
65 return MountImpl::MountError(kGenericMountError);
66 }
67
68 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
69 new PosixMountImpl(mount_base_path_.c_str(), readOnly(), unmount_fn)));
70}
71
72void SdSpiFs::unmountImpl() {
73 MLOG(roo_io_fs) << "Unmounting SD card";
74 CHECK_NOTNULL(card_);
75
76 esp_err_t ret = esp_vfs_fat_sdcard_unmount(mount_base_path_.c_str(), card_);
77 if (ret != ESP_OK) {
78 LOG(ERROR) << "Unmounting card failed: " << esp_err_to_name(ret);
79 }
80 card_ = nullptr;
81 mount_base_path_.clear();
82}
83
84Filesystem::MediaPresence SdSpiFs::checkMediaPresence() {
85 return kMediaPresenceUnknown;
86}
87
88SdSpiFs CreateSdSpiFs() { return SdSpiFs(); }
89
90SdSpiFs SDSPI = CreateSdSpiFs();
91
92} // namespace roo_io
93
94#endif // (defined ESP_PLATFORM || defined ROO_TESTING)
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8