mirror of
https://github.com/Dejvino/lilybook.git
synced 2024-11-14 12:23:28 +00:00
126 lines
4.4 KiB
C
126 lines
4.4 KiB
C
|
|
#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/sdspi_host.h"
|
|
#include "sdmmc_cmd.h"
|
|
#include "spiffs/spiffs_vfs.h"
|
|
#include "storage.h"
|
|
|
|
static const char *tag = "storage";
|
|
|
|
// This example can use SDMMC and SPI peripherals to communicate with SD card.
|
|
// By default, SDMMC peripheral is used.
|
|
// To enable SPI mode, uncomment the following line:
|
|
|
|
//#define USE_SPI_MODE
|
|
|
|
// When testing SD and SPI modes, keep in mind that once the card has been
|
|
// initialized in SPI mode, it can not be reinitialized in SD mode without
|
|
// toggling power to the card.
|
|
|
|
#ifdef USE_SPI_MODE
|
|
// Pin mapping when using SPI mode.
|
|
// With this mapping, SD card can be used both in SPI and 1-line SD mode.
|
|
// Note that a pull-up on CS line is required in SD mode.
|
|
#define PIN_NUM_MISO ((gpio_num_t)2)
|
|
#define PIN_NUM_MOSI ((gpio_num_t)15)
|
|
#define PIN_NUM_CLK ((gpio_num_t)14)
|
|
#define PIN_NUM_CS ((gpio_num_t)13)
|
|
#endif //USE_SPI_MODE
|
|
|
|
int sdcard_init()
|
|
{
|
|
#ifndef USE_SPI_MODE
|
|
ESP_LOGI(tag, "Using SDMMC peripheral");
|
|
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
|
|
|
// 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();
|
|
|
|
// To use 1-line SD mode, uncomment the following line:
|
|
slot_config.width = 1;
|
|
|
|
// GPIOs 15, 2, 4, 12, 13 should have external 10k pull-ups.
|
|
// Internal pull-ups are not sufficient. However, enabling internal pull-ups
|
|
// does make a difference some boards, so we do that here.
|
|
gpio_set_pull_mode((gpio_num_t)15, GPIO_PULLUP_ONLY); // CMD, needed in 4- and 1- line modes
|
|
gpio_set_pull_mode((gpio_num_t)2, GPIO_PULLUP_ONLY); // D0, needed in 4- and 1-line modes
|
|
//gpio_set_pull_mode((gpio_num_t)4, GPIO_PULLUP_ONLY); // D1, needed in 4-line mode only
|
|
//gpio_set_pull_mode((gpio_num_t)12, GPIO_PULLUP_ONLY); // D2, needed in 4-line mode only
|
|
gpio_set_pull_mode((gpio_num_t)13, GPIO_PULLUP_ONLY); // D3, needed in 4- and 1-line modes
|
|
|
|
#else
|
|
ESP_LOGI(TAG, "Using SPI peripheral");
|
|
|
|
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
|
sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT();
|
|
slot_config.gpio_miso = PIN_NUM_MISO;
|
|
slot_config.gpio_mosi = PIN_NUM_MOSI;
|
|
slot_config.gpio_sck = PIN_NUM_CLK;
|
|
slot_config.gpio_cs = PIN_NUM_CS;
|
|
// 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.
|
|
#endif //USE_SPI_MODE
|
|
|
|
// 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,
|
|
.allocation_unit_size = 16 * 1024
|
|
};
|
|
|
|
// 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 (%s). "
|
|
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// Card has been initialized, print its properties
|
|
sdmmc_card_print_info(stdout, card);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int storage_init()
|
|
{
|
|
int ret = 0;
|
|
|
|
vfs_spiffs_register();
|
|
if (spiffs_is_mounted) {
|
|
ESP_LOGI(tag, "File system mounted.");
|
|
} else {
|
|
ESP_LOGE(tag, "Error mounting file system.");
|
|
ret += 1 << 1;
|
|
}
|
|
|
|
if (sdcard_init() == 0) {
|
|
ESP_LOGI(tag, "SD card mounted.");
|
|
} else {
|
|
ESP_LOGE(tag, "Error mounting SD card.");
|
|
ret += 1 << 2;
|
|
}
|
|
|
|
return ret;
|
|
}
|