diff --git a/usermods/sd_card/platformio_override.ini.sample b/usermods/sd_card/platformio_override.ini.sample new file mode 100644 index 0000000000..0c96c6557e --- /dev/null +++ b/usermods/sd_card/platformio_override.ini.sample @@ -0,0 +1,43 @@ +; Options +; ------- +;; see readme.md + + +;; simple examples how to configure this usermod -> add to your platformio_override.ini + +[env:esp32_sdcard_spi] +extends = env:esp32dev +custom_usermods = ${env:esp32dev.custom_usermods} sd_card +build_flags = ${env:esp32dev.build_flags} + -D WLED_USE_SD_SPI + +[env:esp32_sdcard_mmc] +extends = env:esp32dev +custom_usermods = ${env:esp32dev.custom_usermods} sd_card +build_flags = ${env:esp32dev.build_flags} + -D WLED_USE_SD_MMC + +[env:esp32S3_sdcard_spi] +extends = env:esp32s3dev_8MB_opi +custom_usermods = ${env:esp32s3dev_8MB_opi.custom_usermods} sd_card +build_flags = ${env:esp32s3dev_8MB_opi.build_flags} + -D WLED_USE_SD_SPI + +[env:esp32S3_sdcard_mmc] +extends = env:esp32s3_4M_qspi +custom_usermods = ${env:esp32s3_4M_qspi.custom_usermods} sd_card +build_flags = ${env:esp32s3_4M_qspi.build_flags} + -D WLED_USE_SD_MMC + + +[env:esp32C3_sdcard_SPI] +extends = env:esp32c3dev +custom_usermods = ${env:esp32c3dev.custom_usermods} sd_card +build_flags = ${env:esp32c3dev.build_flags} + -D WLED_USE_SD_SPI + +[env:esp32S2_sdcard_SPI] +extends = env:lolin_s2_mini +custom_usermods = ${env:lolin_s2_mini.custom_usermods} sd_card +build_flags = ${env:lolin_s2_mini.build_flags} + -D WLED_USE_SD_SPI diff --git a/usermods/sd_card/readme.md b/usermods/sd_card/readme.md index 96390c05ac..9c502f33d2 100644 --- a/usermods/sd_card/readme.md +++ b/usermods/sd_card/readme.md @@ -5,6 +5,7 @@ - choose the way your SD is connected 1. via `-D WLED_USE_SD_MMC` when connected via MMC 2. via `-D WLED_USE_SD_SPI` when connected via SPI (use usermod page to setup SPI pins) + 3. Customize pins with the following `-D UM_SD_SELECT=16 -D UM_SD_CLOCK=14 -D UM_SD_POCI=36 -D UM_SD_PICO=15` ### Test - enable `-D SD_PRINT_HOME_DIR` and `-D WLED_DEBUG` diff --git a/usermods/sd_card/sd_card.cpp b/usermods/sd_card/sd_card.cpp index 4e68b97a34..72cbc7735e 100644 --- a/usermods/sd_card/sd_card.cpp +++ b/usermods/sd_card/sd_card.cpp @@ -11,11 +11,41 @@ #define USED_STORAGE_FILESYSTEMS "SD SPI, LittleFS" #include "SD.h" #include "SPI.h" +#else + #error "Please define either WLED_USE_SD_MMC or WLED_USE_SD_SPI" +#endif +#if defined(WLED_USE_SD_SPI) && defined(WLED_USE_SD_MMC) + #error "Both WLED_USE_SD_MMC and WLED_USE_SD_SPI are defined, please use only one." #endif -#ifdef WLED_USE_SD_MMC -#elif defined(WLED_USE_SD_SPI) - SPIClass spiPort = SPIClass(VSPI); + +#ifndef UM_SD_SELECT + #define UM_SD_SELECT 16 +#endif +#ifndef UM_SD_CLOCK + #define UM_SD_CLOCK 14 +#endif +#ifndef UM_SD_POCI + #if CONFIG_IDF_TARGET_ESP32S3 && (CONFIG_SPIRAM_MODE_OCT || CONFIG_ESPTOOLPY_FLASHMODE_OPI) // on -S3 with octal (opi) flash or PSRAM, Pin 22-37 are not available + #define UM_SD_POCI 44 + #else + #define UM_SD_POCI 36 + #endif +#endif +#ifndef UM_SD_PICO + #define UM_SD_PICO 15 +#endif + + +#ifdef WLED_USE_SD_SPI + // SD_MMC configuration handled elsewhere + // HSPI bus should be used both on -S3 and classic esp32; try VSPI (classic esp32) or FSPI (esp32-s3) in case of conflicts + SPIClass spiPort = SPIClass(HSPI); + #if defined(WLED_USE_ETHERNET) || defined(CONFIG_IDF_TARGET_ESP32C3) + // Ethernet boards only have one SPI bus (HSPI) availeable + // ESP32-C3 only has one SPI bus + #warning "SD card may have conflicts with 2-pin LEDs." + #endif #endif void listDir( const char * dirname, uint8_t levels); @@ -24,11 +54,13 @@ class UsermodSdCard : public Usermod { private: bool sdInitDone = false; - #ifdef WLED_USE_SD_SPI - int8_t configPinSourceSelect = 16; - int8_t configPinSourceClock = 14; - int8_t configPinPoci = 36; // confusing names? Then have a look :) - int8_t configPinPico = 15; // https://www.oshwa.org/a-resolution-to-redefine-spi-signal-names/ +// confusing names? Then have a look +// https://oshwa.org/resources/a-resolution-to-redefine-spi-signal-names/ +#ifdef WLED_USE_SD_SPI + int8_t configPinSourceSelect = UM_SD_SELECT; + int8_t configPinSourceClock = UM_SD_CLOCK; + int8_t configPinPoci = UM_SD_POCI; + int8_t configPinPico = UM_SD_PICO; //acquired and initialize the SPI port void init_SD_SPI() @@ -51,10 +83,9 @@ class UsermodSdCard : public Usermod { bool returnOfInitSD = false; - #if defined(WLED_USE_SD_SPI) - spiPort.begin(configPinSourceClock, configPinPoci, configPinPico, configPinSourceSelect); - returnOfInitSD = SD_ADAPTER.begin(configPinSourceSelect, spiPort); - #endif + // This whole function is only enabled when compiling with WLED_USE_SD_SPI + spiPort.begin(configPinSourceClock, configPinPoci, configPinPico, configPinSourceSelect); + returnOfInitSD = SD_ADAPTER.begin(configPinSourceSelect, spiPort); if(!returnOfInitSD) { DEBUG_PRINTF("[%s] SPI begin failed!\n", _name); @@ -241,4 +272,4 @@ void listDir( const char * dirname, uint8_t levels){ #endif static UsermodSdCard sd_card; -REGISTER_USERMOD(sd_card); \ No newline at end of file +REGISTER_USERMOD(sd_card);