diff --git a/README.md b/README.md index 7d0a8c15..864cee17 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,10 @@ There in the sensor configuration select "TheThingsNetwork" and set Decoding Pro # SD-card Data can be stored on an SD-card if one is availabe. Simply choose the file in src/hal and add the following lines to your hal-file: - #define HAS_SDCARD 1 // this board has an SD-card-reader/writer + #define HAS_SDCARD 1 // this board has an SD-card-reader/writer, with SPI interface + OR + #define HAS_SDCARD 2 // this board has an SD-card-reader/writer, using native SD interface + // Pins for SD-card #define SDCARD_CS (13) // fill in the correct numbers for your board #define SDCARD_MOSI (15) diff --git a/include/sdcard.h b/include/sdcard.h index 4323c6a3..8157bb32 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -4,14 +4,37 @@ #include #include #include + +#if HAS_SDCARD == 1 #include +#elif HAS_SDCARD == 2 +#include +#else +#error HAS_SDCARD unknown card reader value, must be either 1 or 2 +#endif #include "sds011read.h" -#define SDCARD_FILE_NAME "paxcount.%02d" -#define SDCARD_FILE_HEADER "date, time, wifi, bluet" +#ifndef SDCARD_CS +#define SDCARD_CS SS +#endif -bool sdcard_init( void ); -void sdcardWriteData( uint16_t, uint16_t); +#ifndef SDCARD_MOSI +#define SDCARD_MOSI MOSI +#endif + +#ifndef SDCARD_MISO +#define SDCARD_MISO MISO +#endif + +#ifndef SDCARD_SCLK +#define SDCARD_SCLK SCK +#endif + +#define SDCARD_FILE_NAME "/paxcount.%02d" +#define SDCARD_FILE_HEADER "date, time, wifi, bluet" + +bool sdcard_init(void); +void sdcardWriteData(uint16_t, uint16_t); #endif diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 5faab5cd..3b573c70 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -14,12 +14,19 @@ static void createFile(void); File fileSDCard; bool sdcard_init() { - ESP_LOGD(TAG, "looking for SD-card..."); + ESP_LOGI(TAG, "looking for SD-card..."); +#if HAS_SDCARD == 1 + pinMode(SS, OUTPUT); useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK); - if (useSDCard) +#elif HAS_SDCARD == 2 + useSDCard = SD_MMC.begin(); +#endif + + if (useSDCard) { + ESP_LOGI(TAG, "SD-card found"); createFile(); - else - ESP_LOGD(TAG, "SD-card not found"); + } else + ESP_LOGI(TAG, "SD-card not found"); return useSDCard; } @@ -64,10 +71,22 @@ void createFile(void) { for (int i = 0; i < 100; i++) { sprintf(bufferFilename, SDCARD_FILE_NAME, i); ESP_LOGD(TAG, "SD: looking for file <%s>", bufferFilename); + +#if HAS_SDCARD == 1 bool fileExists = SD.exists(bufferFilename); +#elif HAS_SDCARD == 2 + bool fileExists = SD_MMC.exists(bufferFilename); +#endif + if (!fileExists) { ESP_LOGD(TAG, "SD: file does not exist: opening"); + +#if HAS_SDCARD == 1 fileSDCard = SD.open(bufferFilename, FILE_WRITE); +#elif HAS_SDCARD == 2 + fileSDCard = SD_MMC.open(bufferFilename, FILE_WRITE); +#endif + if (fileSDCard) { ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); fileSDCard.print(SDCARD_FILE_HEADER);