SD card reader native mode added

This commit is contained in:
Klaus K Wilting 2020-05-16 15:30:05 +02:00
parent 2b46a69dbe
commit 6e487983b5
3 changed files with 54 additions and 9 deletions

View File

@ -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)

View File

@ -4,14 +4,37 @@
#include <globals.h>
#include <stdio.h>
#include <SPI.h>
#if HAS_SDCARD == 1
#include <mySD.h>
#elif HAS_SDCARD == 2
#include <SD_MMC.h>
#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

View File

@ -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);