From f068626c12f7cdbe16077a758fe59d043f65eea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A1=2E=20=D0=94?= =?UTF-8?q?=D1=83=D0=BA=D0=B0=D1=87=D1=91=D0=B2?= Date: Mon, 20 Sep 2021 09:33:47 +0300 Subject: [PATCH 1/2] 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. --- libraries/SD_MMC/src/SD_MMC.cpp | 5 ++--- libraries/SD_MMC/src/SD_MMC.h | 6 +++++- variants/esp32-evb/pins_arduino.h | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libraries/SD_MMC/src/SD_MMC.cpp b/libraries/SD_MMC/src/SD_MMC.cpp index 16da3c8ead7..450657cfa6f 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_DEFAULT; + 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; @@ -58,7 +58,6 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount host.io_int_enable = &sdmmc_host_io_int_enable; host.io_int_wait = &sdmmc_host_io_int_wait; host.command_timeout_ms = 0; - host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; #ifdef BOARD_HAS_1BIT_SDMMC mode1bit = true; #endif diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index 6e40fd4540c..5f6a0a02dcb 100644 --- a/libraries/SD_MMC/src/SD_MMC.h +++ b/libraries/SD_MMC/src/SD_MMC.h @@ -31,7 +31,11 @@ class SDMMCFS : public FS public: SDMMCFS(FSImplPtr impl); - bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false); +#ifdef ETH_CAN_INTERFERE_WITH_SDMMC + bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false, int sdmmc_frequency=SDMMC_FREQ_DEFAULT); +#else + bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false, int sdmmc_frequency=SDMMC_FREQ_HIGHSPEED); +#endif 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 15fa98e2496..ea786516f80 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 ETH_CAN_INTERFERE_WITH_SDMMC #endif /* Pins_Arduino_h */ From 934e038c59acc274909e52eb712bacc25f492a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A1=2E=20=D0=94?= =?UTF-8?q?=D1=83=D0=BA=D0=B0=D1=87=D1=91=D0=B2?= Date: Tue, 21 Sep 2021 10:45:38 +0300 Subject: [PATCH 2/2] Code rewritten according to https://github.com/espressif/arduino-esp32/pull/5688#pullrequestreview-759359645 --- libraries/SD_MMC/src/SD_MMC.h | 13 ++++++++----- variants/esp32-evb/pins_arduino.h | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/libraries/SD_MMC/src/SD_MMC.h b/libraries/SD_MMC/src/SD_MMC.h index 5f6a0a02dcb..e8540b32fc4 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,11 +38,7 @@ class SDMMCFS : public FS public: SDMMCFS(FSImplPtr impl); -#ifdef ETH_CAN_INTERFERE_WITH_SDMMC - bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false, int sdmmc_frequency=SDMMC_FREQ_DEFAULT); -#else - bool begin(const char * mountpoint="/sdcard", bool mode1bit=false, bool format_if_mount_failed=false, int sdmmc_frequency=SDMMC_FREQ_HIGHSPEED); -#endif + 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 ea786516f80..a2e02dc71a6 100644 --- a/variants/esp32-evb/pins_arduino.h +++ b/variants/esp32-evb/pins_arduino.h @@ -29,6 +29,6 @@ static const uint8_t MISO = 15; static const uint8_t SCK = 14; #define BOARD_HAS_1BIT_SDMMC -#define ETH_CAN_INTERFERE_WITH_SDMMC +#define BOARD_MAX_SDMMC_FREQ SDMMC_FREQ_DEFAULT #endif /* Pins_Arduino_h */