@@ -304,35 +304,39 @@ SPI Pin Mapping Summary
304304
305305 .. code-block :: c
306306
307+ #include <stdio.h>
307308 #include <string.h>
308309 #include <sys/stat.h>
310+ #include <sys/unistd.h>
309311 #include "esp_log.h"
310312 #include "esp_vfs_fat.h"
311313 #include "sdmmc_cmd.h"
314+ #include "driver/gpio.h"
312315
316+ #define TAG "SD_OPS"
313317 #define MOUNT_POINT "/sdcard"
318+ #define FILE_PREFIX MOUNT_POINT"/data_"
319+ #define MAX_FILES 20
320+
321+ #define PIN_NUM_MISO 0
322+ #define PIN_NUM_MOSI 5
323+ #define PIN_NUM_CLK 4
324+ #define PIN_NUM_CS 11
325+ #define BTN_PIN GPIO_NUM_9
326+
327+ FILE* safe_fopen(const char* path, const char* mode, int tries) {
328+ FILE* f = NULL;
329+ for (int i = 0; i < tries; ++i) {
330+ f = fopen(path, mode);
331+ if (f) return f;
332+ vTaskDelay(50 / portTICK_PERIOD_MS);
333+ }
334+ return NULL;
335+ }
314336
315- #define PIN_NUM_MISO CONFIG_EXAMPLE_PIN_MISO
316- #define PIN_NUM_MOSI CONFIG_EXAMPLE_PIN_MOSI
317- #define PIN_NUM_CLK CONFIG_EXAMPLE_PIN_CLK
318- #define PIN_NUM_CS CONFIG_EXAMPLE_PIN_CS
319-
320- static const char *TAG = "SDCARD";
321-
322- void app_main(void)
323- {
324- esp_err_t ret;
325- sdmmc_card_t *card;
326-
327- ESP_LOGI(TAG, "Initializing SD card...");
328-
329- esp_vfs_fat_sdmmc_mount_config_t mount_config = {
330- .format_if_mount_failed = false,
331- .max_files = 3,
332- .allocation_unit_size = 16 * 1024
333- };
334-
337+ esp_err_t mount_sdcard(sdmmc_card_t **card_out) {
335338 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
339+ host.max_freq_khz = 4000;
336340
337341 spi_bus_config_t bus_cfg = {
338342 .mosi_io_num = PIN_NUM_MOSI,
@@ -343,51 +347,127 @@ SPI Pin Mapping Summary
343347 .max_transfer_sz = 4000,
344348 };
345349
346- ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
350+ gpio_set_pull_mode(PIN_NUM_MISO, GPIO_PULLUP_ONLY);
351+ gpio_set_pull_mode(PIN_NUM_MOSI, GPIO_PULLUP_ONLY);
352+ gpio_set_pull_mode(PIN_NUM_CLK, GPIO_PULLUP_ONLY);
353+ gpio_set_pull_mode(PIN_NUM_CS, GPIO_PULLUP_ONLY);
354+
355+ esp_err_t ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
356+ if (ret == ESP_ERR_INVALID_STATE) {
357+ ESP_LOGW(TAG, "SPI bus already initialized, attempting to free and reinitialize.");
358+ spi_bus_free(host.slot); // Libera el bus anterior
359+ ret = spi_bus_initialize(host.slot, &bus_cfg, SDSPI_DEFAULT_DMA);
360+ }
361+
347362 if (ret != ESP_OK) {
348- ESP_LOGE(TAG, "Failed to init SPI bus." );
349- return;
363+ ESP_LOGE(TAG, "SPI bus init failed: %s", esp_err_to_name(ret) );
364+ return ret ;
350365 }
351366
352367 sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
353368 slot_config.gpio_cs = PIN_NUM_CS;
354369 slot_config.host_id = host.slot;
355370
371+ esp_vfs_fat_sdmmc_mount_config_t mount_config = {
372+ .format_if_mount_failed = false,
373+ .max_files = 5,
374+ .allocation_unit_size = 16 * 1024
375+ };
376+
377+ sdmmc_card_t *card;
356378 ret = esp_vfs_fat_sdspi_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card);
357379 if (ret != ESP_OK) {
358- ESP_LOGE(TAG, "Failed to mount filesystem.");
359- return;
380+ ESP_LOGE(TAG, "SD mount failed: %s", esp_err_to_name(ret));
381+ spi_bus_free(host.slot); // Importante: liberar SPI si falla montaje
382+ return ret;
360383 }
361384
362- ESP_LOGI(TAG, "Filesystem mounted.");
385+ *card_out = card;
386+ sdmmc_card_print_info(stdout, card);
387+ return ESP_OK;
388+ }
389+
363390
364- const char *file_path = MOUNT_POINT"/test.txt";
365- FILE *f = fopen(file_path, "w");
366- if (f == NULL) {
367- ESP_LOGE(TAG, "Failed to open file for writing.");
368- return;
391+ void write_files() {
392+ for (int i = 0; i < MAX_FILES; i++) {
393+ char path[64];
394+ snprintf(path, sizeof(path), FILE_PREFIX"%d.txt", i);
395+ FILE *f = safe_fopen(path, "w", 5);
396+ if (f) {
397+ fprintf(f, "This is file number %d\n", i);
398+ fflush(f);
399+ fclose(f);
400+ ESP_LOGI(TAG, "Wrote %s", path);
401+ } else {
402+ ESP_LOGE(TAG, "Failed to write %s", path);
403+ }
404+ vTaskDelay(10 / portTICK_PERIOD_MS);
369405 }
406+ }
370407
371- fprintf(f, "Hello from ESP32!\n");
372- fclose(f);
373- ESP_LOGI(TAG, "File written.");
408+ void read_files() {
409+ for (int i = 0; i < MAX_FILES; i++) {
410+ char path[64], buffer[128];
411+ snprintf(path, sizeof(path), FILE_PREFIX"%d.txt", i);
412+ FILE *f = safe_fopen(path, "r", 5);
413+ if (f) {
414+ if (fgets(buffer, sizeof(buffer), f)) {
415+ ESP_LOGI(TAG, "[%d] %s", i, buffer);
416+ } else {
417+ ESP_LOGW(TAG, "File %s is empty or unreadable.", path);
418+ }
419+ fclose(f);
420+ } else {
421+ ESP_LOGE(TAG, "Failed to read %s", path);
422+ }
423+ vTaskDelay(10 / portTICK_PERIOD_MS);
424+ }
425+ }
374426
375- f = fopen(file_path, "r");
376- if (f) {
377- char line[64];
378- fgets(line, sizeof(line), f);
379- fclose(f);
380- ESP_LOGI(TAG, "Read from file: '%s'", line);
381- } else {
382- ESP_LOGE(TAG, "Failed to read file.");
427+ void wait_for_button_press() {
428+ ESP_LOGI(TAG, "Waiting for button press (GPIO9) to retry...");
429+ while (gpio_get_level(BTN_PIN) == 1) {
430+ vTaskDelay(100 / portTICK_PERIOD_MS);
431+ }
432+ while (gpio_get_level(BTN_PIN) == 0) {
433+ vTaskDelay(100 / portTICK_PERIOD_MS);
383434 }
435+ vTaskDelay(200 / portTICK_PERIOD_MS);
436+ }
384437
385- esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
386- spi_bus_free(host.slot);
387- ESP_LOGI(TAG, "Card unmounted.");
438+ void app_main(void)
439+ {
440+ gpio_config_t io_conf = {
441+ .pin_bit_mask = (1ULL << BTN_PIN),
442+ .mode = GPIO_MODE_INPUT,
443+ .pull_up_en = GPIO_PULLUP_ENABLE,
444+ .pull_down_en = GPIO_PULLDOWN_DISABLE,
445+ .intr_type = GPIO_INTR_DISABLE
446+ };
447+ gpio_config(&io_conf);
448+
449+ while (1) {
450+ sdmmc_card_t *card;
451+ ESP_LOGI(TAG, "Mounting SD card...");
452+
453+ if (mount_sdcard(&card) == ESP_OK) {
454+ ESP_LOGI(TAG, "SD card mounted. Starting file operations.");
455+ write_files();
456+ read_files();
457+ esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
458+ sdmmc_host_t host = SDSPI_HOST_DEFAULT();
459+ spi_bus_free(host.slot);
460+ ESP_LOGI(TAG, "SD card unmounted and SPI bus freed.");
461+ } else {
462+ ESP_LOGW(TAG, "Operation aborted due to mount failure.");
463+ }
464+
465+ wait_for_button_press();
466+ }
388467 }
389468
390469
470+
391471 .. figure :: /_static/menuconfig.png
392472 :align: center
393473 :alt: ESP-IDF Menuconfig
0 commit comments