From 7c056bdea9c6a550216f6cca7c5687f54633c82c Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 28 Feb 2022 22:39:25 +0100 Subject: [PATCH] improve sd-card MMC handling --- include/sdcard.h | 30 ++++++++++++++++++++---------- src/hal/ttgov21new.h | 5 +++-- src/sdcard.cpp | 10 +++++----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/include/sdcard.h b/include/sdcard.h index ba59ec1c..1f2b0bca 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -11,17 +11,8 @@ #define MOUNT_POINT "/sdcard" -#if HAS_SDCARD == 1 +#if HAS_SDCARD == 1 // SPI interface #include "driver/sdspi_host.h" -#elif HAS_SDCARD == 2 -#include "driver/sdmmc_host.h" -#else -#error HAS_SDCARD unknown card reader value, must be either 1 or 2 -#endif - -#ifdef HAS_SDS011 -#include "sds011read.h" -#endif #ifndef SDCARD_CS #define SDCARD_CS SS @@ -39,6 +30,25 @@ #define SDCARD_SCLK SCK #endif +#elif HAS_SDCARD == 2 // MMC interface +#include "driver/sdmmc_host.h" + +#ifndef SDCARD_SLOTWIDTH +#define SDCARD_SLOTWIDTH 1 +#endif + +#ifndef SDCARD_PULLUP +#define SDCARD_PULLUP SDMMC_SLOT_FLAG_INTERNAL_PULLUP +#endif + +#else +#error HAS_SDCARD unknown card reader value, must be either 1 or 2 +#endif + +#ifdef HAS_SDS011 +#include "sds011read.h" +#endif + #define SDCARD_FILE_NAME clientId #define SDCARD_FILE_HEADER "timestamp,wifi,ble" diff --git a/src/hal/ttgov21new.h b/src/hal/ttgov21new.h index fbaae22b..47145374 100644 --- a/src/hal/ttgov21new.h +++ b/src/hal/ttgov21new.h @@ -9,14 +9,15 @@ /* Hardware related definitions for TTGO V2.1 Board // ATTENTION: check your board version! -// This settings are for boards labeled v1.6 on pcb, NOT for v1.5 or older +// This settings are for boards labeled v1.6 or v1.6.1 on pcb, NOT for v1.5 or older */ #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 // HPD13A LoRa SoC // enable only if you want to store a local paxcount table on the device -#define HAS_SDCARD 2 // this board has a SDMMC card-reader/writer +#define HAS_SDCARD 2 // this board has a SD MMC card-reader/writer +#define SDCARD_SLOTWIDTH 4 // 4-line interface #define HAS_DISPLAY 1 #define HAS_LED (25) // green on board LED diff --git a/src/sdcard.cpp b/src/sdcard.cpp index a6d66a7d..11e011db 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -140,13 +140,13 @@ bool sdcard_init(bool create) { // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); - // To use 4-line SD mode, change this to 4: - slot_config.width = 1; + // Set 1-line or 4-line SD mode (default is 1-line) + slot_config.width = SDCARD_SLOTWIDTH; // Enable internal pullups on enabled pins. The internal pullups // are insufficient however, please make sure 10k external pullups are // connected on the bus. This is for debug / example purpose only. - slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP; + slot_config.flags |= SDCARD_PULLUP; // Use settings defined above to initialize SD card and mount FAT filesystem. esp_err_t ret = esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, @@ -180,7 +180,7 @@ bool sdcard_init(bool create) { fgetpos(data_file, &position); // empty file? then we write a header line - if (position = 0) { + if (position == 0) { fprintf(data_file, "%s", SDCARD_FILE_HEADER); #if (defined BAT_MEASURE_ADC || defined HAS_PMU) fprintf(data_file, "%s", SDCARD_FILE_HEADER_VOLTAGE); @@ -188,8 +188,8 @@ bool sdcard_init(bool create) { #if (HAS_SDS011) fprintf(data_file, "%s", SDCARD_FILE_HEADER_SDS011); #endif - fprintf(data_file, "\n"); } + fprintf(data_file, "\n"); } else { useSDCard = false;