Browse Source

SDCard with Arduino library

main
Hendrik Langer 7 years ago
parent
commit
dbe05843d5
  1. 101
      src/sdcard.cpp
  2. 10
      src/sdcard.h

101
src/sdcard.cpp

@ -1,90 +1,69 @@
/*
* SD card
*
* https://github.com/espressif/arduino-esp32/blob/master/libraries/SD_MMC/examples/SDMMC_Test/SDMMC_Test.ino
* https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card
*/
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_vfs_fat.h"
#include "driver/sdmmc_host.h"
#include "driver/sdmmc_defs.h"
#include "sdmmc_cmd.h"
#include "Arduino.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include "sdcard.h"
using namespace std;
static const char* TAG = "SDCard";
#define PIN_SD_CD 17
SDCard::SDCard() {
}
void SDCard::mount() {
ESP_LOGI(TAG, "Initializing SD card");
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
// To use 1-line SD mode, uncomment the following line:
// host.flags = SDMMC_HOST_FLAG_1BIT;
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and formatted
// in case when mounting fails.
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
.format_if_mount_failed = false,
.max_files = 5
};
pinMode(PIN_SD_CD, INPUT);
if (!digitalRead(PIN_SD_CD)) {
Serial.println("No card detected!");
}
if(!SD.begin(5)){
Serial.println("Card Mount Failed");
return;
}
this->cardType = SD.cardType();
// Use settings defined above to initialize SD card and mount FAT filesystem.
// Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
// Please check its source code and implement error recovery when developing
// production applications.
sdmmc_card_t* card;
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount filesystem. If you want the card to be formatted, set format_if_mount_failed = true.");
} else {
ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret);
}
if(this->cardType == CARD_NONE){
Serial.println("No SD card attached");
return;
}
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
Serial.print("SD_MMC Card Type: ");
if(this->cardType == CARD_MMC){
Serial.println("MMC");
} else if(this->cardType == CARD_SD){
Serial.println("SDSC");
} else if(this->cardType == CARD_SDHC){
Serial.println("SDHC");
} else {
Serial.println("UNKNOWN");
}
this->cardSize = SD.cardSize() / (1024 * 1024);
Serial.printf("SD_MMC Card Size: %lluMB\n", this->cardSize);
}
void SDCard::umount() {
// All done, unmount partition and disable SDMMC host peripheral
esp_vfs_fat_sdmmc_unmount();
ESP_LOGI(TAG, "Card unmounted");
SD.end();
}
int SDCard::readFile(const char *path){
ESP_LOGI(TAG, "Reading file");
FILE* f = fopen("/sdcard/foo.txt", "r");
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open file for reading");
return -1;
}
char line[64];
fgets(line, sizeof(line), f);
fclose(f);
// strip newline
char* pos = strchr(line, '\n');
if (pos) {
*pos = '\0';
void SDCard::readFile(const char *path){
File file = fs.open(path);
if(!file){
Serial.println("Failed to open file for reading");
return;
}
ESP_LOGI(TAG, "Read from file: '%s'", line);
return 0;
Serial.print("Read from file: ");
while(file.available()){
Serial.write(file.read());
}
}

10
src/sdcard.h

@ -1,13 +1,21 @@
#ifndef _SDCARD_H
#define _SDCARD_H
#include "Arduino.h"
#include "FS.h"
#include "SD.h"
#include "SPI.h"
class SDCard {
public:
SDCard();
void mount();
void umount();
int readFile(const char *path);
void readFile(const char *path);
private:
fs::FS &fs = SD;
uint8_t cardType;
uint64_t cardSize;
};
#endif /* _SDCARD_H */

Loading…
Cancel
Save