sdcard bugfixes

This commit is contained in:
cyberman54 2022-01-30 21:53:11 +01:00
parent 2b9c7c8804
commit d45c96e544
2 changed files with 48 additions and 31 deletions

View File

@ -8,10 +8,8 @@
#if (HAS_SDCARD)
#if HAS_SDCARD == 1
#include <mySD.h>
#define MYSD SD
#elif HAS_SDCARD == 2
#include <SD_MMC.h>
#define MYSD SD_MMC
#else
#error HAS_SDCARD unknown card reader value, must be either 1 or 2
#endif

View File

@ -10,7 +10,7 @@ static const char TAG[] = __FILE__;
#ifdef HAS_SDCARD
static bool useSDCard = false;
static bool useSDCard;
static void openFile(void);
File fileSDCard;
@ -27,20 +27,25 @@ bool sdcard_init(bool create) {
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html
#if HAS_SDCARD == 1 // use SD SPI host driver
useSDCard = MYSD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK);
#elif HAS_SDCARD == 2 // use SD MMC host driver
// enable internal pullups of sd-data lines
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA0), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA1), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA2), GPIO_PULLUP_ONLY);
gpio_set_pull_mode(gpio_num_t(SDCARD_DATA3), GPIO_PULLUP_ONLY);
useSDCard = MYSD.begin();
useSDCard = SD_MMC.begin();
#endif
if (useSDCard) {
ESP_LOGI(TAG, "SD-card found");
cardType = MYSD.cardType();
cardSize = MYSD.cardSize() / (1024 * 1024);
#if HAS_SDCARD == 1
cardType = SD.cardType();
cardSize = SD.cardSize() / (1024 * 1024);
#elif HAS_SDCARD == 2
cardType = SD_MMC.cardType();
cardSize = SD_MMC.cardSize() / (1024 * 1024);
#endif
} else {
ESP_LOGI(TAG, "SD-card not found");
return false;
@ -60,8 +65,7 @@ bool sdcard_init(bool create) {
} else {
ESP_LOGI(TAG, "SD Card type: UNKNOWN");
}
ESP_LOGI(TAG, "SD Card Size: %lluMB\n", cardSize);
ESP_LOGI(TAG, "SD Card Size: %lluMB", cardSize);
openFile();
@ -121,35 +125,50 @@ void openFile(void) {
snprintf(bufferFilename, sizeof(bufferFilename), "/%s.csv", SDCARD_FILE_NAME);
ESP_LOGI(TAG, "SD: looking for file <%s>", bufferFilename);
/*
if (MYSD.exists(bufferFilename)) {
if (MYSD.open(bufferFilename, FILE_APPEND))
useSDCard = true;
} else {
ESP_LOGI(TAG, "SD: file does not exist, creating it");
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
}
*/
#if HAS_SDCARD == 1
bool fileExists = SD.exists(bufferFilename);
#elif HAS_SDCARD == 2
bool fileExists = SD_MMC.exists(bufferFilename);
#endif
if (!MYSD.exists(bufferFilename))
ESP_LOGI(TAG, "SD: file does not exist, creating it");
// file not exists, create it
if (!fileExists) {
ESP_LOGD(TAG, "SD: file not found, creating...");
if (MYSD.open(bufferFilename, FILE_WRITE))
useSDCard = true;
#if HAS_SDCARD == 1
fileSDCard = SD.open(bufferFilename, FILE_WRITE);
#elif HAS_SDCARD == 2
fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE);
#endif
if (useSDCard) {
ESP_LOGI(TAG, "SD: file opened: <%s>", bufferFilename);
fileSDCard.print(SDCARD_FILE_HEADER);
if (fileSDCard) {
ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename);
fileSDCard.print(SDCARD_FILE_HEADER);
#if (defined BAT_MEASURE_ADC || defined HAS_PMU)
fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data
fileSDCard.print(SDCARD_FILE_HEADER_VOLTAGE); // for battery level data
#endif
#if (HAS_SDS011)
fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
fileSDCard.print(SDCARD_FILE_HEADER_SDS011);
#endif
fileSDCard.println();
} else {
ESP_LOGE(TAG, "SD: file not opened error");
fileSDCard.println();
useSDCard = true;
}
}
// file exists, append data
else {
ESP_LOGD(TAG, "SD: file found, opening...");
#if HAS_SDCARD == 1
fileSDCard = SD.open(bufferFilename, FILE_APPEND);
#elif HAS_SDCARD == 2
fileSDCard = SD_MMC.open(bufferFilename, FILE_APPEND);
#endif
if (fileSDCard) {
ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename);
useSDCard = true;
}
}
return;