Hendrik Langer
8 years ago
4 changed files with 122 additions and 1 deletions
@ -0,0 +1,90 @@ |
|||
/*
|
|||
* SD card |
|||
* |
|||
* 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 "sdcard.h" |
|||
|
|||
using namespace std; |
|||
|
|||
static const char* TAG = "SDCard"; |
|||
|
|||
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 |
|||
}; |
|||
|
|||
// 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); |
|||
} |
|||
return; |
|||
} |
|||
|
|||
// Card has been initialized, print its properties
|
|||
sdmmc_card_print_info(stdout, card); |
|||
} |
|||
|
|||
void SDCard::umount() { |
|||
// All done, unmount partition and disable SDMMC host peripheral
|
|||
esp_vfs_fat_sdmmc_unmount(); |
|||
ESP_LOGI(TAG, "Card unmounted"); |
|||
} |
|||
|
|||
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'; |
|||
} |
|||
ESP_LOGI(TAG, "Read from file: '%s'", line); |
|||
|
|||
return 0; |
|||
} |
@ -0,0 +1,13 @@ |
|||
#ifndef _SDCARD_H |
|||
#define _SDCARD_H |
|||
|
|||
class SDCard { |
|||
public: |
|||
SDCard(); |
|||
void mount(); |
|||
void umount(); |
|||
int readFile(const char *path); |
|||
private: |
|||
}; |
|||
|
|||
#endif /* _SDCARD_H */ |
Loading…
Reference in new issue