From 78c8512a861192cdfd540e43e37bbb3dfa56b00a Mon Sep 17 00:00:00 2001 From: Folkert van Heusden Date: Wed, 6 Nov 2024 15:04:55 +0100 Subject: [PATCH] ESP32: configurable SPI speed for SD-card --- microcontrollers/backend-sdcard.cpp | 40 ++++++++++++--------- microcontrollers/backend-sdcard.h | 4 ++- microcontrollers/data/cfg-iESP.json.example | 3 +- microcontrollers/main.ino | 7 +++- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/microcontrollers/backend-sdcard.cpp b/microcontrollers/backend-sdcard.cpp index 4802a31..11fe0c6 100644 --- a/microcontrollers/backend-sdcard.cpp +++ b/microcontrollers/backend-sdcard.cpp @@ -17,14 +17,15 @@ extern void write_led(const int gpio, const int state); -backend_sdcard::backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS): +backend_sdcard::backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS, const std::optional spi_speed): backend("SD-card"), led_read(led_read), led_write(led_write), pin_SD_MISO(pin_SD_MISO), pin_SD_MOSI(pin_SD_MOSI), pin_SD_SCLK(pin_SD_SCLK), - pin_SD_CS(pin_SD_CS) + pin_SD_CS(pin_SD_CS), + spi_speed(spi_speed) { #if defined(RP2040W) mutex_init(&serial_access_lock); @@ -64,24 +65,29 @@ bool backend_sdcard::reinit(const bool close_first) bool ok = false; SPI.begin(pin_SD_SCLK, pin_SD_MISO, pin_SD_MOSI, pin_SD_CS); #if defined(WT_ETH01) - for(int sp=22; sp>=14; sp -= 2) { - Serial.printf("Trying %d MHz...\r\n", sp); - if (sd.begin(SdSpiConfig(pin_SD_CS, SHARED_SPI, SD_SCK_MHZ(sp)))) { - ok = true; - Serial.printf("Accessing SD card at %d MHz\r\n", sp); - break; - } - } + const int start_speed = 22; + const int end_speed = 14; + const int steps = 2; #else - for(int sp=50; sp>=14; sp -= 4) { - Serial.printf("Trying %d MHz...\r\n", sp); - if (sd.begin(SdSpiConfig(pin_SD_CS, SHARED_SPI, SD_SCK_MHZ(sp)))) { - ok = true; - Serial.printf("Accessing SD card at %d MHz\r\n", sp); - break; + const int start_speed = 50; + const int end_speed = 14; + const int steps = 4; +#endif + if (spi_speed.has_value()) { + ok = sd.begin(SdSpiConfig(pin_SD_CS, SHARED_SPI, SD_SCK_MHZ(spi_speed.value()))); + if (ok) + Serial.printf("Accessing SD card at %d MHz\r\n", spi_speed.value()); + } + else { + for(int sp=start_speed; sp>=end_speed; sp -= steps) { + Serial.printf("Trying %d MHz...\r\n", sp); + if (sd.begin(SdSpiConfig(pin_SD_CS, SHARED_SPI, SD_SCK_MHZ(sp)))) { + ok = true; + Serial.printf("Accessing SD card at %d MHz\r\n", sp); + break; + } } } -#endif if (ok == false) { Serial.printf("SD-card mount failed (assuming CS is on pin %d)\r\n", pin_SD_CS); diff --git a/microcontrollers/backend-sdcard.h b/microcontrollers/backend-sdcard.h index ac31529..dd1c488 100644 --- a/microcontrollers/backend-sdcard.h +++ b/microcontrollers/backend-sdcard.h @@ -1,4 +1,5 @@ #include +#include #if defined(WT_ETH01) #define SPI_DRIVER_SELECT 1 #endif @@ -20,6 +21,7 @@ class backend_sdcard : public backend const int pin_SD_MOSI { -1 }; const int pin_SD_SCLK { -1 }; const int pin_SD_CS { -1 }; + const std::optional spi_speed; #if defined(RP2040W) mutex_t serial_access_lock; @@ -33,7 +35,7 @@ class backend_sdcard : public backend void wait_for_card(); public: - backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS); + backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS, const std::optional spi_speed); virtual ~backend_sdcard(); bool begin() override; diff --git a/microcontrollers/data/cfg-iESP.json.example b/microcontrollers/data/cfg-iESP.json.example index d3511b6..e38b4eb 100644 --- a/microcontrollers/data/cfg-iESP.json.example +++ b/microcontrollers/data/cfg-iESP.json.example @@ -15,5 +15,6 @@ "SD-MISO": 19, "SD-MOSI": 23, "SD-SCLK": 18, - "SD-CS": 5 + "SD-CS": 5, + "spi-speed": 16 } diff --git a/microcontrollers/main.ino b/microcontrollers/main.ino index 1c3d590..193be66 100644 --- a/microcontrollers/main.ino +++ b/microcontrollers/main.ino @@ -90,11 +90,13 @@ int pin_SD_MISO= -1; int pin_SD_MOSI= -1; int pin_SD_SCLK= -1; int pin_SD_CS = -1; +std::optional spi_speed { 14 }; #else int pin_SD_MISO= 19; int pin_SD_MOSI= 23; int pin_SD_SCLK= 18; int pin_SD_CS = 5; +std::optional spi_speed; #endif snmp *snmp_ { nullptr }; @@ -278,6 +280,9 @@ bool load_configuration() { if (cfg.containsKey("log-level")) logging::log_level_syslog = logging::parse_ll(cfg["log-level"].as()); + if (cfg.containsKey("spi-speed")) + spi_speed = cfg["spi-speed"].as(); + data_file.close(); #if !defined(TEENSY4_1) @@ -638,7 +643,7 @@ void setup() { bs = new backend_sdcard_teensy41(led_green, led_yellow); #else Serial.printf("LEDgreen: %d, LEDyellow: %d, MISO: %d, MOSI: %d, SCLK: %d, CS: %d\r\n", led_green, led_yellow, pin_SD_MISO, pin_SD_MOSI, pin_SD_SCLK, pin_SD_CS); - bs = new backend_sdcard(led_green, led_yellow, pin_SD_MISO, pin_SD_MOSI, pin_SD_SCLK, pin_SD_CS); + bs = new backend_sdcard(led_green, led_yellow, pin_SD_MISO, pin_SD_MOSI, pin_SD_SCLK, pin_SD_CS, spi_speed); #endif draw_status(13);