summaryrefslogtreecommitdiff
path: root/main/storage.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-10-06 21:20:04 +1100
committerjacqueline <me@jacqueline.id.au>2022-10-06 21:22:36 +1100
commit4e643baf5f6af1c65e08295bab4ab4f55c3feaf4 (patch)
tree96ef7af980f56a03b0ef972b525eba17664133f5 /main/storage.cpp
parent55264a826f006c51990abc84949734ee8ec0646b (diff)
downloadtangara-fw-4e643baf5f6af1c65e08295bab4ab4f55c3feaf4.tar.gz
Neaten up the gpio api, use RAII, make it thread safe
Diffstat (limited to 'main/storage.cpp')
-rw-r--r--main/storage.cpp23
1 files changed, 7 insertions, 16 deletions
diff --git a/main/storage.cpp b/main/storage.cpp
index 54829302..e1f6f386 100644
--- a/main/storage.cpp
+++ b/main/storage.cpp
@@ -1,7 +1,10 @@
#include "storage.h"
+#include <mutex>
+
#include "diskio_impl.h"
#include "diskio_sdmmc.h"
+#include "driver/gpio.h"
#include "driver/sdspi_host.h"
#include "esp_check.h"
#include "esp_err.h"
@@ -23,10 +26,9 @@ SdStorage::SdStorage(GpioExpander *gpio) {
SdStorage::~SdStorage() {}
SdStorage::Error SdStorage::Acquire(void) {
- // First switch to this device, and pull CS.
- gpio_->set_sd_mux(GpioExpander::ESP);
- gpio_->set_sd_cs(false);
- gpio_->Write();
+ // Acquiring the bus will also flush the mux switch change.
+ gpio_->set_pin(GpioExpander::SD_MUX_SWITCH, GpioExpander::SD_MUX_ESP);
+ auto lock = gpio_->AcquireSpiBus(GpioExpander::SD_CARD);
// Now we can init the driver and set up the SD card into SPI mode.
sdspi_host_init();
@@ -47,8 +49,6 @@ SdStorage::Error SdStorage::Acquire(void) {
esp_err_t err = sdmmc_card_init(&host_, &card_);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Failed to read, err: %d", err);
- gpio_->set_sd_cs(true);
- gpio_->Write();
return Error::FAILED_TO_READ;
}
@@ -62,16 +62,11 @@ SdStorage::Error SdStorage::Acquire(void) {
return Error::FAILED_TO_MOUNT;
}
- // We're done chatting for now.
- //gpio_->set_sd_cs(true);
- //gpio_->Write();
-
return Error::OK;
}
void SdStorage::Release(void) {
- gpio_->set_sd_cs(false);
- gpio_->Write();
+ auto lock = gpio_->AcquireSpiBus(GpioExpander::SD_CARD);
// Unmount and unregister the filesystem
f_unmount("");
@@ -82,10 +77,6 @@ void SdStorage::Release(void) {
// Uninstall the SPI driver
sdspi_host_remove_device(this->handle_);
sdspi_host_deinit();
-
- gpio_->set_sd_mux(GpioExpander::USB);
- gpio_->set_sd_cs(true);
- gpio_->Write();
}
} // namespace gay_ipod