summaryrefslogtreecommitdiff
path: root/main/storage.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-10-12 11:59:42 +1100
committerjacqueline <me@jacqueline.id.au>2022-10-12 11:59:42 +1100
commit4fc5f931acf5bdc582fdad3cb48e6810964198c5 (patch)
tree9057debaa13ec996450a63adf5f3228cd5ff1b54 /main/storage.cpp
parentefd5392f6cf2149369d7d3170400bbe8f2d5c82e (diff)
downloadtangara-fw-4fc5f931acf5bdc582fdad3cb48e6810964198c5.tar.gz
WIP use result<> and RAII
Diffstat (limited to 'main/storage.cpp')
-rw-r--r--main/storage.cpp120
1 files changed, 87 insertions, 33 deletions
diff --git a/main/storage.cpp b/main/storage.cpp
index d2f88894..9e34ade0 100644
--- a/main/storage.cpp
+++ b/main/storage.cpp
@@ -1,5 +1,6 @@
#include "storage.h"
+#include <atomic>
#include <mutex>
#include "diskio_impl.h"
@@ -15,77 +16,130 @@
#include "hal/spi_types.h"
#include "sdmmc_cmd.h"
+static const char* kTag = "SDSTORAGE";
+static const uint8_t kMaxOpenFiles = 8;
+
namespace gay_ipod {
-static const char* TAG = "SDSTORAGE";
+const char* kStoragePath = "/sdcard";
-SdStorage::SdStorage(GpioExpander *gpio) {
- this->gpio_ = gpio;
-}
+// Static functions for interrop with the ESP IDF API, which requires a
+// function pointer.
+namespace callback {
+static std::atomic<SdStorage*> instance = nullptr;
+static std::atomic<esp_err_t (*)(sdspi_dev_handle_t, sdmmc_command_t*)>
+ bootstrap = nullptr;
-SdStorage::~SdStorage() {}
+static esp_err_t do_transaction(sdspi_dev_handle_t handle,
+ sdmmc_command_t* cmdinfo) {
+ auto bootstrap_fn = bootstrap.load();
+ if (bootstrap_fn != nullptr) {
+ return bootstrap_fn(handle, cmdinfo);
+ }
+ auto instance_unwrapped = instance.load();
+ if (instance_unwrapped == nullptr) {
+ ESP_LOGW(kTag, "uncaught sdspi transaction");
+ return ESP_OK;
+ }
+ // TODO: what if a transaction comes in right now?
+ return instance_unwrapped->HandleTransaction(handle, cmdinfo);
+}
+} // namespace callback
-SdStorage::Error SdStorage::Acquire(void) {
+auto SdStorage::create(GpioExpander* gpio)
+ -> cpp::result<std::unique_ptr<SdStorage>, Error> {
// Acquiring the bus will also flush the mux switch change.
- gpio_->set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP);
+ gpio->set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP);
+
+ sdspi_dev_handle_t handle;
+ sdmmc_host_t host;
+ sdmmc_card_t card;
+ FATFS* fs = nullptr;
// Now we can init the driver and set up the SD card into SPI mode.
sdspi_host_init();
sdspi_device_config_t config = {
- .host_id = VSPI_HOST,
- // CS handled manually bc it's on the GPIO expander
- .gpio_cs = GPIO_NUM_2,
- .gpio_cd = SDSPI_SLOT_NO_CD,
- .gpio_wp = SDSPI_SLOT_NO_WP,
- .gpio_int = GPIO_NUM_NC,
+ .host_id = VSPI_HOST,
+ // CS handled manually bc it's on the GPIO expander
+ .gpio_cs = GPIO_NUM_2,
+ .gpio_cd = SDSPI_SLOT_NO_CD,
+ .gpio_wp = SDSPI_SLOT_NO_WP,
+ .gpio_int = GPIO_NUM_NC,
};
- ESP_ERROR_CHECK(sdspi_host_init_device(&config, &handle_));
+ if (esp_err_t err = sdspi_host_init_device(&config, &handle) != ESP_OK) {
+ ESP_LOGE(kTag, "Failed to init, err %d", err);
+ return cpp::fail(Error::FAILED_TO_INIT);
+ }
- host_ = sdmmc_host_t SDSPI_HOST_DEFAULT();
+ host = sdmmc_host_t SDSPI_HOST_DEFAULT();
// We manage the CS pin ourselves via the GPIO expander. To do this safely in
// a multithreaded environment, we wrap the ESP IDF do_transaction function
// with our own that acquires the CS mutex for the duration of the SPI
// transaction.
- auto src = host_.do_transaction;
- sdspi::do_transaction_wrapper = [=](sdspi_dev_handle_t handle, sdmmc_command_t *cmd) -> esp_err_t {
- auto lock = gpio_->AcquireSpiBus(GpioExpander::SD_CARD);
- return src(handle, cmd);
- };
- host_.do_transaction = &sdspi::do_transaction;
+ auto do_transaction = host.do_transaction;
+ host.do_transaction = &callback::do_transaction;
+ host.slot = handle;
+ callback::bootstrap = do_transaction;
- host_.slot = handle_;
+ auto lock = gpio->AcquireSpiBus(GpioExpander::SD_CARD);
// Will return ESP_ERR_INVALID_RESPONSE if there is no card
- esp_err_t err = sdmmc_card_init(&host_, &card_);
+ esp_err_t err = sdmmc_card_init(&host, &card);
if (err != ESP_OK) {
- ESP_LOGW(TAG, "Failed to read, err: %d", err);
- return Error::FAILED_TO_READ;
+ ESP_LOGW(kTag, "Failed to read, err: %d", err);
+ return cpp::fail(Error::FAILED_TO_READ);
}
- ESP_ERROR_CHECK(esp_vfs_fat_register(kStoragePath, "", kMaxOpenFiles, &fs_));
- ff_diskio_register_sdmmc(fs_->pdrv, &card_);
+ ESP_ERROR_CHECK(esp_vfs_fat_register(kStoragePath, "", kMaxOpenFiles, &fs));
+ ff_diskio_register_sdmmc(fs->pdrv, &card);
// Mount right now, not on first operation.
- FRESULT ferr = f_mount(fs_, "", 1);
+ FRESULT ferr = f_mount(fs, "", 1);
if (ferr != FR_OK) {
- ESP_LOGW(TAG, "Failed to mount, err: %d", ferr);
- return Error::FAILED_TO_MOUNT;
+ ESP_LOGW(kTag, "Failed to mount, err: %d", ferr);
+ return cpp::fail(Error::FAILED_TO_MOUNT);
}
- return Error::OK;
+ return std::make_unique<SdStorage>(gpio, do_transaction, handle, host, card,
+ fs);
+}
+
+SdStorage::SdStorage(GpioExpander* gpio,
+ esp_err_t (*do_transaction)(sdspi_dev_handle_t,
+ sdmmc_command_t*),
+ sdspi_dev_handle_t handle,
+ sdmmc_host_t host,
+ sdmmc_card_t card,
+ FATFS* fs)
+ : gpio_(gpio),
+ do_transaction_(do_transaction),
+ handle_(handle),
+ host_(host),
+ card_(card),
+ fs_(fs) {
+ callback::instance = this;
+ callback::bootstrap = nullptr;
}
-void SdStorage::Release(void) {
+SdStorage::~SdStorage() {
// Unmount and unregister the filesystem
f_unmount("");
ff_diskio_register(fs_->pdrv, NULL);
esp_vfs_fat_unregister_path(kStoragePath);
fs_ = nullptr;
+ callback::instance = nullptr;
+
// Uninstall the SPI driver
sdspi_host_remove_device(this->handle_);
sdspi_host_deinit();
}
-} // namespace gay_ipod
+auto SdStorage::HandleTransaction(sdspi_dev_handle_t handle,
+ sdmmc_command_t* cmdinfo) -> esp_err_t {
+ auto lock = gpio_->AcquireSpiBus(GpioExpander::SD_CARD);
+ return do_transaction_(handle, cmdinfo);
+}
+
+} // namespace gay_ipod