From 852181250cf0c7ae5ee0ed70744982fc21655203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 5 Jun 2023 14:57:40 +0200 Subject: [PATCH 1/2] sdmmc periman implemented --- libraries/SD_MMC/src/SD_MMC.cpp | 47 +++++++++++++++++++++++++++++++-- libraries/SD_MMC/src/SD_MMC.h | 4 +++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/libraries/SD_MMC/src/SD_MMC.cpp b/libraries/SD_MMC/src/SD_MMC.cpp index d8544ff67ce..a8dc7c0c7ae 100644 --- a/libraries/SD_MMC/src/SD_MMC.cpp +++ b/libraries/SD_MMC/src/SD_MMC.cpp @@ -24,10 +24,10 @@ #include "sdmmc_cmd.h" #include "soc/sdmmc_pins.h" #include "ff.h" +#include "esp32-hal-periman.h" using namespace fs; - SDMMCFS::SDMMCFS(FSImplPtr impl) : FS(impl), _card(nullptr) { @@ -43,6 +43,14 @@ SDMMCFS::SDMMCFS(FSImplPtr impl) #endif // defined(SOC_SDMMC_USE_GPIO_MATRIX) && defined(BOARD_HAS_SDMMC) } +bool SDMMCFS::sdmmcDetachBus(void * bus_pointer){ + SDMMCFS *bus = (SDMMCFS *) bus_pointer; + if(bus->_card) { + bus->end(); + } + return true; +} + bool SDMMCFS::setPins(int clk, int cmd, int d0) { return setPins(clk, cmd, d0, GPIO_NUM_NC, GPIO_NUM_NC, GPIO_NUM_NC); @@ -89,17 +97,28 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount if(_card) { return true; } + perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC, SDMMCFS::sdmmcDetachBus); + //mount sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); #ifdef SOC_SDMMC_USE_GPIO_MATRIX // SoC supports SDMMC pin configuration via GPIO matrix. - // Chech that the pins have been set either in the constructor or setPins function. + // Check that the pins have been set either in the constructor or setPins function. if (_pin_cmd == -1 || _pin_clk == -1 || _pin_d0 == -1 || (!mode1bit && (_pin_d1 == -1 || _pin_d2 == -1 || _pin_d3 == -1))) { log_e("SDMMCFS: some SD pins are not set"); return false; } + if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!mode1bit) { + if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL)){ return false; } + } + slot_config.clk = (gpio_num_t) _pin_clk; slot_config.cmd = (gpio_num_t) _pin_cmd; slot_config.d0 = (gpio_num_t) _pin_d0; @@ -115,6 +134,8 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount #ifdef BOARD_HAS_1BIT_SDMMC mode1bit = true; #endif + _mode1bit = mode1bit; + if(mode1bit) { host.flags = SDMMC_HOST_FLAG_1BIT; //use 1-line SD mode slot_config.width = 1; @@ -142,7 +163,21 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount return false; } _impl->mountpoint(mountpoint); + + if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + if(!mode1bit) { + if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; } + } return true; + +err: + log_e("Failed to set all pins bus to SDMMC"); + SDMMCFS::sdmmcDetachBus((void *)(this)); + return false; } void SDMMCFS::end() @@ -151,6 +186,14 @@ void SDMMCFS::end() esp_vfs_fat_sdcard_unmount(_impl->mountpoint(), _card); _impl->mountpoint(NULL); _card = NULL; + perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL); + if(!_mode1bit) { + perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL); + perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL); + } } } diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index ffbc3b8f964..f9b9ebc5fad 100644 --- a/libraries/SD_MMC/src/SD_MMC.h +++ b/libraries/SD_MMC/src/SD_MMC.h @@ -45,6 +45,7 @@ class SDMMCFS : public FS int8_t _pin_d2 = -1; int8_t _pin_d3 = -1; #endif + bool _mode1bit = false; public: SDMMCFS(FSImplPtr impl); @@ -56,6 +57,9 @@ class SDMMCFS : public FS uint64_t cardSize(); uint64_t totalBytes(); uint64_t usedBytes(); + +private: + static bool sdmmcDetachBus(void * bus_pointer); }; } From 3b77fe0d6e5267f018a6feb46188e64df9729644 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 5 Jun 2023 16:03:10 +0200 Subject: [PATCH 2/2] save pins when SOC_SDMMC_USE_IOMUX --- libraries/SD_MMC/src/SD_MMC.cpp | 44 ++++++++++++++++++++------------- libraries/SD_MMC/src/SD_MMC.h | 3 --- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/libraries/SD_MMC/src/SD_MMC.cpp b/libraries/SD_MMC/src/SD_MMC.cpp index a8dc7c0c7ae..cdc41ca3c3b 100644 --- a/libraries/SD_MMC/src/SD_MMC.cpp +++ b/libraries/SD_MMC/src/SD_MMC.cpp @@ -40,7 +40,17 @@ SDMMCFS::SDMMCFS(FSImplPtr impl) _pin_d2 = SDMMC_D2; _pin_d3 = SDMMC_D3; #endif // BOARD_HAS_1BIT_SDMMC -#endif // defined(SOC_SDMMC_USE_GPIO_MATRIX) && defined(BOARD_HAS_SDMMC) + +#elif SOC_SDMMC_USE_IOMUX + _pin_clk = SDMMC_SLOT1_IOMUX_PIN_NUM_CLK; + _pin_cmd = SDMMC_SLOT1_IOMUX_PIN_NUM_CMD; + _pin_d0 = SDMMC_SLOT1_IOMUX_PIN_NUM_D0; +#ifndef BOARD_HAS_1BIT_SDMMC + _pin_d1 = SDMMC_SLOT1_IOMUX_PIN_NUM_D1; + _pin_d2 = SDMMC_SLOT1_IOMUX_PIN_NUM_D2; + _pin_d3 = SDMMC_SLOT1_IOMUX_PIN_NUM_D3; +#endif // BOARD_HAS_1BIT_SDMMC +#endif } bool SDMMCFS::sdmmcDetachBus(void * bus_pointer){ @@ -76,12 +86,12 @@ bool SDMMCFS::setPins(int clk, int cmd, int d0, int d1, int d2, int d3) // Since SDMMCFS::begin hardcodes the usage of slot 1, only check if // the pins match slot 1 pins. bool pins_ok = (clk == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CLK) && - (cmd == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CMD) && - (d0 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D0) && - (((d1 == -1) && (d2 == -1) && (d3 == -1)) || - ((d1 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D1) && - (d2 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D2) && - (d3 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D3))); + (cmd == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_CMD) && + (d0 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D0) && + (((d1 == -1) && (d2 == -1) && (d3 == -1)) || + ((d1 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D1) && + (d2 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D2) && + (d3 == (int)SDMMC_SLOT1_IOMUX_PIN_NUM_D3))); if (!pins_ok) { log_e("SDMMCFS: specified pins are not supported by this chip."); return false; @@ -110,6 +120,15 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount return false; } + slot_config.clk = (gpio_num_t) _pin_clk; + slot_config.cmd = (gpio_num_t) _pin_cmd; + slot_config.d0 = (gpio_num_t) _pin_d0; + slot_config.d1 = (gpio_num_t) _pin_d1; + slot_config.d2 = (gpio_num_t) _pin_d2; + slot_config.d3 = (gpio_num_t) _pin_d3; + slot_config.width = 4; +#endif // SOC_SDMMC_USE_GPIO_MATRIX + if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL)){ return false; } if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL)){ return false; } if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL)){ return false; } @@ -119,14 +138,6 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL)){ return false; } } - slot_config.clk = (gpio_num_t) _pin_clk; - slot_config.cmd = (gpio_num_t) _pin_cmd; - slot_config.d0 = (gpio_num_t) _pin_d0; - slot_config.d1 = (gpio_num_t) _pin_d1; - slot_config.d2 = (gpio_num_t) _pin_d2; - slot_config.d3 = (gpio_num_t) _pin_d3; - slot_config.width = 4; -#endif // SOC_SDMMC_USE_GPIO_MATRIX sdmmc_host_t host = SDMMC_HOST_DEFAULT(); host.flags = SDMMC_HOST_FLAG_4BIT; host.slot = SDMMC_HOST_SLOT_1; @@ -134,12 +145,11 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount #ifdef BOARD_HAS_1BIT_SDMMC mode1bit = true; #endif - _mode1bit = mode1bit; - if(mode1bit) { host.flags = SDMMC_HOST_FLAG_1BIT; //use 1-line SD mode slot_config.width = 1; } + _mode1bit = mode1bit; esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = format_if_mount_failed, diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index f9b9ebc5fad..ed0165dabb0 100644 --- a/libraries/SD_MMC/src/SD_MMC.h +++ b/libraries/SD_MMC/src/SD_MMC.h @@ -36,15 +36,12 @@ class SDMMCFS : public FS { protected: sdmmc_card_t* _card; - -#ifdef SOC_SDMMC_USE_GPIO_MATRIX int8_t _pin_clk = -1; int8_t _pin_cmd = -1; int8_t _pin_d0 = -1; int8_t _pin_d1 = -1; int8_t _pin_d2 = -1; int8_t _pin_d3 = -1; -#endif bool _mode1bit = false; public: