roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
sdmmc.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)
10
11#include "soc/soc_caps.h"
12
13#if SOC_SDMMC_HOST_SUPPORTED
14
15#include "ff.h"
16
17#if !defined(MLOG_roo_io_fs)
18#define MLOG_roo_io_fs 0
19#endif
20
21#include "diskio_sdmmc.h"
22#include "driver/sdmmc_host.h"
23#include "esp_idf_version.h"
24#include "esp_vfs_fat.h"
26#include "roo_logging.h"
27
28namespace roo_io {
29
30SdMmcFs::SdMmcFs()
39 width_(0),
41 pdrv_(0xFF) {}
42
43void SdMmcFs::setPins(uint8_t pin_clk, uint8_t pin_cmd, uint8_t pin_d0) {
44 use_default_pins_ = false;
45 pin_clk_ = (gpio_num_t)pin_clk;
46 pin_cmd_ = (gpio_num_t)pin_cmd;
47 pin_d0_ = (gpio_num_t)pin_d0;
48 width_ = 1;
49}
50
51void SdMmcFs::setPins(uint8_t pin_clk, uint8_t pin_cmd, uint8_t pin_d0,
52 uint8_t pin_d1, uint8_t pin_d2, uint8_t pin_d3) {
53 use_default_pins_ = false;
54 pin_clk_ = (gpio_num_t)pin_clk;
55 pin_cmd_ = (gpio_num_t)pin_cmd;
56 pin_d0_ = (gpio_num_t)pin_d0;
57 pin_d1_ = (gpio_num_t)pin_d1;
58 pin_d2_ = (gpio_num_t)pin_d2;
59 pin_d3_ = (gpio_num_t)pin_d3;
60 width_ = 4;
61}
62
63MountImpl::MountResult SdMmcFs::mountImpl(std::function<void()> unmount_fn) {
64 MLOG(roo_io_fs) << "Mounting SD card";
65#if defined(ROO_TESTING)
66 mount_base_path_ = FakeEsp32().fs_root();
67#else
68 mount_base_path_.clear();
69#endif
70 mount_base_path_.append(mountPoint());
71
72 sdmmc_host_t host = SDMMC_HOST_DEFAULT();
73
74 sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
75 if (!use_default_pins_) {
76#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
77 slot_config.clk = pin_clk_;
78 slot_config.cmd = pin_cmd_;
79 slot_config.d0 = pin_d0_;
80 slot_config.d1 = pin_d1_;
81 slot_config.d2 = pin_d2_;
82 slot_config.d3 = pin_d3_;
83#else
84 LOG(WARNING) << "Custom SDMMC pins not supported on ESP-IDF < 5.0";
85#endif
86 slot_config.width = width_;
87 }
88 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
89 .format_if_mount_failed = formatIfMountFailed(),
90 .max_files = maxOpenFiles(),
91 .allocation_unit_size = 16 * 1024};
92
93 esp_err_t ret = esp_vfs_fat_sdmmc_mount(mount_base_path_.c_str(), &host,
94 &slot_config, &mount_config, &card_);
95
96 if (ret != ESP_OK) {
97 if (ret == ESP_FAIL) {
98 LOG(ERROR) << "Failed to mount the filesystem.";
99 // " If you want the card to be formatted, use setFormatIfEmpty(true)";
100 } else {
101 LOG(ERROR) << "Failed to initialize the card: " << esp_err_to_name(ret);
102 }
103 return MountImpl::MountError(kGenericMountError);
104 }
105#if defined(ROO_TESTING)
106 pdrv_ = 0;
107#else
108 pdrv_ = ff_diskio_get_pdrv_card(card_);
109#endif
110 return MountImpl::Mounted(std::unique_ptr<MountImpl>(
111 new PosixMountImpl(mount_base_path_.c_str(), readOnly(), unmount_fn)));
112}
113
114void SdMmcFs::unmountImpl() {
115 MLOG(roo_io_fs) << "Unmounting SD card";
116 CHECK_NOTNULL(card_);
117
118 esp_err_t ret = esp_vfs_fat_sdcard_unmount(mount_base_path_.c_str(), card_);
119 if (ret != ESP_OK) {
120 LOG(ERROR) << "Unmounting card failed: " << esp_err_to_name(ret);
121 }
122 card_ = nullptr;
123 pdrv_ = 0xFF;
124 mount_base_path_.clear();
125}
126
127Filesystem::MediaPresence SdMmcFs::checkMediaPresence() {
128 FATFS* fsinfo;
129 DWORD fre_clust;
130 char drv[3] = {(char)(48 + pdrv_), ':', 0};
131 if (f_getfree(drv, &fre_clust, &fsinfo) != 0) {
132 return Filesystem::kMediaAbsent;
133 }
134 return (fsinfo->csize == 0) ? Filesystem::kMediaAbsent
135 : Filesystem::kMediaPresent;
136}
137
138SdMmcFs CreateSdMmcFs() { return SdMmcFs(); }
139
140SdMmcFs SDMMC = CreateSdMmcFs();
141
142} // namespace roo_io
143
144#endif
145#endif
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8