From 93f10609f4784fbe5a1580021553c1fa4a72dcc7 Mon Sep 17 00:00:00 2001 From: "Serguei S. Dukachev" Date: Fri, 1 Oct 2021 15:48:20 +0300 Subject: [PATCH] SDMMC frequency selection based on board type (#5688) * SDMMC frequency selection based on board type On Olimex ESP32 EVB I/O operations with SD card can cause error when LAN is used in same time. Problem is disappearing if SD MMC frequency lower down from SDMMC_FREQ_HIGHSPEED to SDMMC_FREQ_DEFAULT. No problem if WiFi used instead LAN. * Code rewritten according to https://github.com/espressif/arduino-esp32/pull/5688#pullrequestreview-759359645 --- libraries/SD_MMC/src/SD_MMC.cpp | 4 ++-- libraries/SD_MMC/src/SD_MMC.h | 9 ++++++++- variants/esp32-evb/pins_arduino.h | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/libraries/SD_MMC/src/SD_MMC.cpp b/libraries/SD_MMC/src/SD_MMC.cpp index 0c95fb9d..450657cf 100644 --- a/libraries/SD_MMC/src/SD_MMC.cpp +++ b/libraries/SD_MMC/src/SD_MMC.cpp @@ -36,7 +36,7 @@ SDMMCFS::SDMMCFS(FSImplPtr impl) : FS(impl), _card(NULL) {} -bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount_failed) +bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount_failed, int sdmmc_frequency) { if(_card) { return true; @@ -46,7 +46,7 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount sdmmc_host_t host; host.flags = SDMMC_HOST_FLAG_4BIT; host.slot = SDMMC_HOST_SLOT_1; - host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; + host.max_freq_khz = sdmmc_frequency; host.io_voltage = 3.3f; host.init = &sdmmc_host_init; host.set_bus_width = &sdmmc_host_set_bus_width; diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index 6e40fd45..e8540b32 100644 --- a/libraries/SD_MMC/src/SD_MMC.h +++ b/libraries/SD_MMC/src/SD_MMC.h @@ -21,6 +21,13 @@ #include "driver/sdmmc_types.h" #include "sd_defines.h" +// If reading/writing to the SD card is unstable, +// you can define BOARD_MAX_SDMMC_FREQ with lower value (Ex. SDMMC_FREQ_DEFAULT) +// in pins_arduino.h for your board variant. +#ifndef BOARD_MAX_SDMMC_FREQ +#define BOARD_MAX_SDMMC_FREQ SDMMC_FREQ_HIGHSPEED +#endif + namespace fs { @@ -31,7 +38,7 @@ protected: public: SDMMCFS(FSImplPtr impl); - bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false); + bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false, int sdmmc_frequency=BOARD_MAX_SDMMC_FREQ); void end(); sdcard_type_t cardType(); uint64_t cardSize(); diff --git a/variants/esp32-evb/pins_arduino.h b/variants/esp32-evb/pins_arduino.h index 15fa98e2..a2e02dc7 100644 --- a/variants/esp32-evb/pins_arduino.h +++ b/variants/esp32-evb/pins_arduino.h @@ -29,5 +29,6 @@ static const uint8_t MISO = 15; static const uint8_t SCK = 14; #define BOARD_HAS_1BIT_SDMMC +#define BOARD_MAX_SDMMC_FREQ SDMMC_FREQ_DEFAULT #endif /* Pins_Arduino_h */