|
|
@ -14,15 +14,15 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace gay_ipod { |
|
|
|
namespace gay_ipod { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char* TAG = "SDSTORAGE"; |
|
|
|
|
|
|
|
|
|
|
|
SdStorage::SdStorage(GpioExpander *gpio) { |
|
|
|
SdStorage::SdStorage(GpioExpander *gpio) { |
|
|
|
this->gpio_ = gpio; |
|
|
|
this->gpio_ = gpio; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
SdStorage::~SdStorage() { |
|
|
|
SdStorage::~SdStorage() {} |
|
|
|
ESP_ERROR_CHECK(sdspi_host_deinit()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t SdStorage::Acquire(void) { |
|
|
|
SdStorage::Error SdStorage::Acquire(void) { |
|
|
|
// First switch to this device, and pull CS.
|
|
|
|
// First switch to this device, and pull CS.
|
|
|
|
gpio_->set_sd_mux(GpioExpander::ESP); |
|
|
|
gpio_->set_sd_mux(GpioExpander::ESP); |
|
|
|
gpio_->set_sd_cs(false); |
|
|
|
gpio_->set_sd_cs(false); |
|
|
@ -39,50 +39,44 @@ esp_err_t SdStorage::Acquire(void) { |
|
|
|
.gpio_wp = SDSPI_SLOT_NO_WP, |
|
|
|
.gpio_wp = SDSPI_SLOT_NO_WP, |
|
|
|
.gpio_int = GPIO_NUM_NC, |
|
|
|
.gpio_int = GPIO_NUM_NC, |
|
|
|
}; |
|
|
|
}; |
|
|
|
esp_err_t ret = sdspi_host_init_device(&config, &handle_); |
|
|
|
ESP_ERROR_CHECK(sdspi_host_init_device(&config, &handle_)); |
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
|
|
|
gpio_->set_sd_cs(true); |
|
|
|
|
|
|
|
gpio_->Write(); |
|
|
|
|
|
|
|
return ret; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
host_ = sdmmc_host_t SDSPI_HOST_DEFAULT(); |
|
|
|
host_ = sdmmc_host_t SDSPI_HOST_DEFAULT(); |
|
|
|
host_.slot = handle_; |
|
|
|
host_.slot = handle_; |
|
|
|
// Will return ESP_ERR_INVALID_RESPONSE if there is no card
|
|
|
|
// Will return ESP_ERR_INVALID_RESPONSE if there is no card
|
|
|
|
// TODO: use our own error code
|
|
|
|
esp_err_t err = sdmmc_card_init(&host_, &card_); |
|
|
|
ret = sdmmc_card_init(&host_, &card_); |
|
|
|
if (err != ESP_OK) { |
|
|
|
if (ret != ESP_OK) { |
|
|
|
ESP_LOGW(TAG, "Failed to read, err: %d", err); |
|
|
|
gpio_->set_sd_cs(true); |
|
|
|
gpio_->set_sd_cs(true); |
|
|
|
gpio_->Write(); |
|
|
|
gpio_->Write(); |
|
|
|
return ret; |
|
|
|
return Error::FAILED_TO_READ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK( |
|
|
|
ESP_ERROR_CHECK(esp_vfs_fat_register(kStoragePath, "", kMaxOpenFiles, &fs_)); |
|
|
|
esp_vfs_fat_register(STORAGE_PATH, "", MAX_OPEN_FILES, &fs_)); |
|
|
|
|
|
|
|
ff_diskio_register_sdmmc(fs_->pdrv, &card_); |
|
|
|
ff_diskio_register_sdmmc(fs_->pdrv, &card_); |
|
|
|
|
|
|
|
|
|
|
|
// Mount right now, not on first operation.
|
|
|
|
// Mount right now, not on first operation.
|
|
|
|
FRESULT fret = f_mount(fs_, "", 1); |
|
|
|
FRESULT ferr = f_mount(fs_, "", 1); |
|
|
|
if (fret != FR_OK) { |
|
|
|
if (ferr != FR_OK) { |
|
|
|
// TODO: Proper error handling
|
|
|
|
ESP_LOGW(TAG, "Failed to mount, err: %d", ferr); |
|
|
|
return ESP_ERR_INVALID_MAC; |
|
|
|
return Error::FAILED_TO_MOUNT; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// We're done chatting for now.
|
|
|
|
// We're done chatting for now.
|
|
|
|
//gpio_->set_sd_cs(true);
|
|
|
|
//gpio_->set_sd_cs(true);
|
|
|
|
//gpio_->Write();
|
|
|
|
//gpio_->Write();
|
|
|
|
|
|
|
|
|
|
|
|
return ret; |
|
|
|
return Error::OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
esp_err_t SdStorage::Release(void) { |
|
|
|
void SdStorage::Release(void) { |
|
|
|
gpio_->set_sd_cs(false); |
|
|
|
gpio_->set_sd_cs(false); |
|
|
|
gpio_->Write(); |
|
|
|
gpio_->Write(); |
|
|
|
|
|
|
|
|
|
|
|
// Unmount and unregister the filesystem
|
|
|
|
// Unmount and unregister the filesystem
|
|
|
|
f_unmount(""); |
|
|
|
f_unmount(""); |
|
|
|
ff_diskio_register(fs_->pdrv, NULL); |
|
|
|
ff_diskio_register(fs_->pdrv, NULL); |
|
|
|
esp_vfs_fat_unregister_path(STORAGE_PATH); |
|
|
|
esp_vfs_fat_unregister_path(kStoragePath); |
|
|
|
fs_ = nullptr; |
|
|
|
fs_ = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
// Uninstall the SPI driver
|
|
|
|
// Uninstall the SPI driver
|
|
|
@ -92,8 +86,6 @@ esp_err_t SdStorage::Release(void) { |
|
|
|
gpio_->set_sd_mux(GpioExpander::USB); |
|
|
|
gpio_->set_sd_mux(GpioExpander::USB); |
|
|
|
gpio_->set_sd_cs(true); |
|
|
|
gpio_->set_sd_cs(true); |
|
|
|
gpio_->Write(); |
|
|
|
gpio_->Write(); |
|
|
|
|
|
|
|
|
|
|
|
return ESP_OK; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} // namespace gay_ipod
|
|
|
|
} // namespace gay_ipod
|
|
|
|