diff --git a/README.md b/README.md index 88a347b..969bb6d 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,27 @@ ESP32 Soundboard ================ Soundboard (computer program), a web application or computer program with buttons that play short, often humorous sound clips. + ## Hardware * [ESP32] µC * [MAX98357A] PCM Input Class D Audio Power Amplifiers * [SD card] + ## Wiring -tbd + +### SD card +ESP32 pin | SD card pin | Notes +--------------|-------------|------------ +GPIO14 (MTMS) | CLK | 10k pullup +GPIO15 (MTDO) | CMD | 10k pullup +GPIO2 | D0 | 10k pullup, pull low to go into download mode +GPIO4 | D1 | 10k pullup; not used in 1-line mode +GPIO12 (MTDI) | D2 | otherwise 10k pullup (see note below!); not used in 1-line mode +GPIO13 (MTCK) | D3 | 10k pullup needed at card side, even in 1-line mode +N/C | CD | +N/C | WP | + ## Build ```bash @@ -26,6 +40,7 @@ platformio run -t upload && platformio device monitor -b 115200 * internal DAC? * Add-ons? + ## References [ESP32 overview]: http://esp32.net diff --git a/src/main.cpp b/src/main.cpp index b6b62ea..5770874 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,11 +8,13 @@ #include "Arduino.h" #include "keyboard.h" +#include "sdcard.h" #define VERSION "0.0" #define LED_PIN 21 Keyboard keyboard; +SDCard sdcard; void setup() { @@ -26,6 +28,7 @@ void setup() Serial.println("-------------------------------------"); keyboard.init(); + sdcard.mount(); } void loop() diff --git a/src/sdcard.cpp b/src/sdcard.cpp new file mode 100644 index 0000000..e33f285 --- /dev/null +++ b/src/sdcard.cpp @@ -0,0 +1,90 @@ +/* + * SD card + * + * https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card + */ + +#include +#include +#include +#include +#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; +} diff --git a/src/sdcard.h b/src/sdcard.h new file mode 100644 index 0000000..adc42b3 --- /dev/null +++ b/src/sdcard.h @@ -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 */