Hendrik Langer
8 years ago
2 changed files with 49 additions and 62 deletions
@ -1,90 +1,69 @@ |
|||||
/*
|
/*
|
||||
* SD card |
* 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
|
* https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card
|
||||
*/ |
*/ |
||||
|
|
||||
#include <stdio.h> |
#include "Arduino.h" |
||||
#include <string.h> |
#include "FS.h" |
||||
#include <sys/unistd.h> |
#include "SD.h" |
||||
#include <sys/stat.h> |
#include "SPI.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 "sdcard.h" |
#include "sdcard.h" |
||||
|
|
||||
using namespace std; |
using namespace std; |
||||
|
|
||||
static const char* TAG = "SDCard"; |
static const char* TAG = "SDCard"; |
||||
|
#define PIN_SD_CD 17 |
||||
|
|
||||
SDCard::SDCard() { |
SDCard::SDCard() { |
||||
} |
} |
||||
|
|
||||
void SDCard::mount() { |
void SDCard::mount() { |
||||
ESP_LOGI(TAG, "Initializing SD card"); |
pinMode(PIN_SD_CD, INPUT); |
||||
|
if (!digitalRead(PIN_SD_CD)) { |
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT(); |
Serial.println("No card detected!"); |
||||
|
} |
||||
// To use 1-line SD mode, uncomment the following line:
|
if(!SD.begin(5)){ |
||||
// host.flags = SDMMC_HOST_FLAG_1BIT;
|
Serial.println("Card Mount Failed"); |
||||
|
return; |
||||
// 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.
|
this->cardType = SD.cardType(); |
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); |
|
||||
|
|
||||
// Options for mounting the filesystem.
|
if(this->cardType == CARD_NONE){ |
||||
// If format_if_mount_failed is set to true, SD card will be partitioned and formatted
|
Serial.println("No SD card attached"); |
||||
// in case when mounting fails.
|
return; |
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = { |
} |
||||
.format_if_mount_failed = false, |
|
||||
.max_files = 5 |
|
||||
}; |
|
||||
|
|
||||
// Use settings defined above to initialize SD card and mount FAT filesystem.
|
Serial.print("SD_MMC Card Type: "); |
||||
// Note: esp_vfs_fat_sdmmc_mount is an all-in-one convenience function.
|
if(this->cardType == CARD_MMC){ |
||||
// Please check its source code and implement error recovery when developing
|
Serial.println("MMC"); |
||||
// production applications.
|
} else if(this->cardType == CARD_SD){ |
||||
sdmmc_card_t* card; |
Serial.println("SDSC"); |
||||
esp_err_t ret = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card); |
} else if(this->cardType == CARD_SDHC){ |
||||
if (ret != ESP_OK) { |
Serial.println("SDHC"); |
||||
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 { |
} else { |
||||
ESP_LOGE(TAG, "Failed to initialize the card (%d). Make sure SD card lines have pull-up resistors in place.", ret); |
Serial.println("UNKNOWN"); |
||||
} |
|
||||
return; |
|
||||
} |
} |
||||
|
|
||||
// Card has been initialized, print its properties
|
this->cardSize = SD.cardSize() / (1024 * 1024); |
||||
sdmmc_card_print_info(stdout, card); |
Serial.printf("SD_MMC Card Size: %lluMB\n", this->cardSize); |
||||
} |
} |
||||
|
|
||||
void SDCard::umount() { |
void SDCard::umount() { |
||||
// All done, unmount partition and disable SDMMC host peripheral
|
SD.end(); |
||||
esp_vfs_fat_sdmmc_unmount(); |
|
||||
ESP_LOGI(TAG, "Card unmounted"); |
|
||||
} |
} |
||||
|
|
||||
int SDCard::readFile(const char *path){ |
void SDCard::readFile(const char *path){ |
||||
ESP_LOGI(TAG, "Reading file"); |
File file = fs.open(path); |
||||
FILE* f = fopen("/sdcard/foo.txt", "r"); |
if(!file){ |
||||
if (f == NULL) { |
Serial.println("Failed to open file for reading"); |
||||
ESP_LOGE(TAG, "Failed to open file for reading"); |
return; |
||||
return -1; |
|
||||
} |
|
||||
char line[64]; |
|
||||
fgets(line, sizeof(line), f); |
|
||||
fclose(f); |
|
||||
// strip newline
|
|
||||
char* pos = strchr(line, '\n'); |
|
||||
if (pos) { |
|
||||
*pos = '\0'; |
|
||||
} |
} |
||||
ESP_LOGI(TAG, "Read from file: '%s'", line); |
|
||||
|
|
||||
return 0; |
Serial.print("Read from file: "); |
||||
|
while(file.available()){ |
||||
|
Serial.write(file.read()); |
||||
|
} |
||||
} |
} |
||||
|
@ -1,13 +1,21 @@ |
|||||
#ifndef _SDCARD_H |
#ifndef _SDCARD_H |
||||
#define _SDCARD_H |
#define _SDCARD_H |
||||
|
|
||||
|
#include "Arduino.h" |
||||
|
#include "FS.h" |
||||
|
#include "SD.h" |
||||
|
#include "SPI.h" |
||||
|
|
||||
class SDCard { |
class SDCard { |
||||
public: |
public: |
||||
SDCard(); |
SDCard(); |
||||
void mount(); |
void mount(); |
||||
void umount(); |
void umount(); |
||||
int readFile(const char *path); |
void readFile(const char *path); |
||||
private: |
private: |
||||
|
fs::FS &fs = SD; |
||||
|
uint8_t cardType; |
||||
|
uint64_t cardSize; |
||||
}; |
}; |
||||
|
|
||||
#endif /* _SDCARD_H */ |
#endif /* _SDCARD_H */ |
||||
|
Loading…
Reference in new issue