diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 085f5683..b51c3259 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,4 @@ -idf_component_register(SRCS "gay-ipod-fw.cpp" "gpio-expander.cpp" "battery.cpp" "storage.cpp" - INCLUDE_DIRS ".") +idf_component_register( + SRCS "gay-ipod-fw.cpp" "gpio-expander.cpp" "battery.cpp" "storage.cpp" + INCLUDE_DIRS "." + REQUIRES "esp_adc_cal" "fatfs") diff --git a/main/gay-ipod-fw.cpp b/main/gay-ipod-fw.cpp index a01a99b1..ed35c469 100644 --- a/main/gay-ipod-fw.cpp +++ b/main/gay-ipod-fw.cpp @@ -1,4 +1,5 @@ #include +#include #include "battery.h" #include "driver/adc.h" @@ -107,9 +108,23 @@ extern "C" void app_main(void) gay_ipod::SdStorage storage(&expander); ESP_ERROR_CHECK(storage.Acquire()); + ESP_LOGI(TAG, "Looks okay? Let's list some files!"); + vTaskDelay(pdMS_TO_TICKS(1000)); + + DIR *d; + struct dirent *dir; + d = opendir(gay_ipod::STORAGE_PATH); + if (d) { + while ((dir = readdir(d)) != NULL) { + ESP_LOGI(TAG, "file! %s", dir->d_name); + } + closedir(d); + } else { + ESP_LOGI(TAG, "nope!"); + } - ESP_LOGI(TAG, "Looks okay? Trying to deinit now."); vTaskDelay(pdMS_TO_TICKS(1000)); + ESP_LOGI(TAG, "Time to deinit."); ESP_ERROR_CHECK(storage.Release()); ESP_LOGI(TAG, "Hooray!"); diff --git a/main/storage.cpp b/main/storage.cpp index b1d5aa6c..378bc3cf 100644 --- a/main/storage.cpp +++ b/main/storage.cpp @@ -1,8 +1,12 @@ #include "storage.h" -#include "esp_check.h" +#include "diskio_impl.h" +#include "diskio_sdmmc.h" #include "driver/sdspi_host.h" +#include "esp_check.h" #include "esp_err.h" +#include "esp_vfs_fat.h" +#include "ff.h" #include "gpio-expander.h" #include "hal/gpio_types.h" #include "hal/spi_types.h" @@ -47,10 +51,26 @@ esp_err_t SdStorage::Acquire(void) { // Will return ESP_ERR_INVALID_RESPONSE if there is no card // TODO: use our own error code ret = sdmmc_card_init(&host_, &card_); + if (ret != ESP_OK) { + gpio_->set_sd_cs(true); + gpio_->Write(); + return ret; + } + + ESP_ERROR_CHECK( + esp_vfs_fat_register(STORAGE_PATH, "", MAX_OPEN_FILES, &fs_)); + ff_diskio_register_sdmmc(fs_->pdrv, &card_); + + // Mount right now, not on first operation. + FRESULT fret = f_mount(fs_, "", 1); + if (fret != FR_OK) { + // TODO: Proper error handling + return ESP_ERR_INVALID_MAC; + } // We're done chatting for now. - gpio_->set_sd_cs(true); - gpio_->Write(); + //gpio_->set_sd_cs(true); + //gpio_->Write(); return ret; } @@ -59,6 +79,13 @@ esp_err_t SdStorage::Release(void) { gpio_->set_sd_cs(false); gpio_->Write(); + // Unmount and unregister the filesystem + f_unmount(""); + ff_diskio_register(fs_->pdrv, NULL); + esp_vfs_fat_unregister_path(STORAGE_PATH); + fs_ = nullptr; + + // Uninstall the SPI driver sdspi_host_remove_device(this->handle_); sdspi_host_deinit(); diff --git a/main/storage.h b/main/storage.h index 79267b0b..514457f3 100644 --- a/main/storage.h +++ b/main/storage.h @@ -4,10 +4,15 @@ #include "driver/sdmmc_types.h" #include "driver/sdspi_host.h" #include "esp_err.h" +#include "esp_vfs_fat.h" #include "gpio-expander.h" +#define MAX_OPEN_FILES (8) + namespace gay_ipod { +static const char *STORAGE_PATH = "/sd"; + class SdStorage { public: SdStorage(GpioExpander *gpio); @@ -24,9 +29,13 @@ class SdStorage { private: GpioExpander *gpio_; + // SPI and SD driver info sdspi_dev_handle_t handle_; sdmmc_host_t host_; sdmmc_card_t card_; + + // Filesystem info + FATFS *fs_ = nullptr; }; } // namespace gay_ipod diff --git a/sdkconfig b/sdkconfig index f6eaccd9..3974c112 100644 --- a/sdkconfig +++ b/sdkconfig @@ -653,13 +653,18 @@ CONFIG_FATFS_CODEPAGE_437=y # CONFIG_FATFS_CODEPAGE_949 is not set # CONFIG_FATFS_CODEPAGE_950 is not set CONFIG_FATFS_CODEPAGE=437 -CONFIG_FATFS_LFN_NONE=y -# CONFIG_FATFS_LFN_HEAP is not set +# CONFIG_FATFS_LFN_NONE is not set +CONFIG_FATFS_LFN_HEAP=y # CONFIG_FATFS_LFN_STACK is not set +CONFIG_FATFS_MAX_LFN=255 +# CONFIG_FATFS_API_ENCODING_ANSI_OEM is not set +# CONFIG_FATFS_API_ENCODING_UTF_16 is not set +CONFIG_FATFS_API_ENCODING_UTF_8=y CONFIG_FATFS_FS_LOCK=0 CONFIG_FATFS_TIMEOUT_MS=10000 CONFIG_FATFS_PER_FILE_CACHE=y -# CONFIG_FATFS_USE_FASTSEEK is not set +CONFIG_FATFS_USE_FASTSEEK=y +CONFIG_FATFS_FAST_SEEK_BUFFER_SIZE=64 # end of FAT Filesystem support #