Skip to content

Commit

Permalink
ESP32: configurable SPI speed for SD-card
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertvanheusden committed Nov 6, 2024
1 parent 27e7ac4 commit 78c8512
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
40 changes: 23 additions & 17 deletions microcontrollers/backend-sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion microcontrollers/backend-sdcard.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mutex>
#include <optional>
#if defined(WT_ETH01)
#define SPI_DRIVER_SELECT 1
#endif
Expand All @@ -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<int> spi_speed;

#if defined(RP2040W)
mutex_t serial_access_lock;
Expand All @@ -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<int> spi_speed);
virtual ~backend_sdcard();

bool begin() override;
Expand Down
3 changes: 2 additions & 1 deletion microcontrollers/data/cfg-iESP.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"SD-MISO": 19,
"SD-MOSI": 23,
"SD-SCLK": 18,
"SD-CS": 5
"SD-CS": 5,
"spi-speed": 16
}
7 changes: 6 additions & 1 deletion microcontrollers/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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<int> spi_speed;
#endif

snmp *snmp_ { nullptr };
Expand Down Expand Up @@ -278,6 +280,9 @@ bool load_configuration() {
if (cfg.containsKey("log-level"))
logging::log_level_syslog = logging::parse_ll(cfg["log-level"].as<std::string>());

if (cfg.containsKey("spi-speed"))
spi_speed = cfg["spi-speed"].as<int>();

data_file.close();

#if !defined(TEENSY4_1)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 78c8512

Please sign in to comment.