/* * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ #include #include #include #include #include #include #include "driver/gpio.h" #include "esp_cpu.h" #include "esp_log.h" #include "FluxSD.h" #include "esp_vfs_fat.h" #include "sdmmc_cmd.h" #include "driver/sdmmc_host.h" const static char *TAG = "SD_TEST"; #define GPIO_INPUT_PIN_SEL(pin) (1ULL<pins[i]); io_conf.pull_down_en = 0; io_conf.pull_up_en = 0; gpio_config(&io_conf); } printf("\n**** PIN recovery time ****\n\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_set_level(config->pins[i], 0); usleep(100); gpio_set_level(config->pins[i], 1); uint32_t cycles = get_cycles_until_pin_level(config->pins[i], 1, 10000); printf("PIN %2d %3s %"PRIu32" cycles\n", config->pins[i], config->names[i], cycles); } printf("\n**** PIN recovery time with weak pullup ****\n\n"); for (int i = 0; i < pin_count; ++i) { gpio_set_direction(config->pins[i], GPIO_MODE_INPUT_OUTPUT_OD); gpio_pullup_en(config->pins[i]); gpio_set_level(config->pins[i], 0); usleep(100); gpio_set_level(config->pins[i], 1); uint32_t cycles = get_cycles_until_pin_level(config->pins[i], 1, 10000); printf("PIN %2d %3s %"PRIu32" cycles\n", config->pins[i], config->names[i], cycles); gpio_pullup_dis(config->pins[i]); } } static esp_err_t s_example_write_file(const char *path, char *data) { ESP_LOGI(TAG, "Opening file %s", path); FILE *f = fopen(path, "w"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for writing"); return ESP_FAIL; } fprintf(f, data); fclose(f); ESP_LOGI(TAG, "File written"); return ESP_OK; } static esp_err_t s_example_read_file(const char *path) { ESP_LOGI(TAG, "Reading file %s", path); FILE *f = fopen(path, "r"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for reading"); return ESP_FAIL; } char line[EXAMPLE_MAX_CHAR_SIZE]; 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 ESP_OK; } uint64_t total_bytes = 0; uint64_t free_bytes = 0; float free_percent = 0; void flux_sd_init(void) { esp_err_t ret; esp_vfs_fat_sdmmc_mount_config_t mount_config = { .format_if_mount_failed = true, .max_files = 5, .allocation_unit_size = 16 * 1024 }; sdmmc_card_t *card; const char mount_point[] = MOUNT_POINT; ESP_LOGI(TAG, "Initializing SD card"); ESP_LOGI(TAG, "Using SDMMC peripheral"); sdmmc_host_t host = SDMMC_HOST_DEFAULT(); sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); slot_config.width = 4; slot_config.clk = CONFIG_EXAMPLE_PIN_CLK; slot_config.cmd = CONFIG_EXAMPLE_PIN_CMD; slot_config.d0 = CONFIG_EXAMPLE_PIN_D0; slot_config.d1 = CONFIG_EXAMPLE_PIN_D1; slot_config.d2 = CONFIG_EXAMPLE_PIN_D2; slot_config.d3 = CONFIG_EXAMPLE_PIN_D3; slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP; ESP_LOGI(TAG, "Mounting filesystem"); ret = esp_vfs_fat_sdmmc_mount(mount_point, &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 the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option."); } 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)); check_sd_card_pins(&config, pin_count); } return; } ESP_LOGI(TAG, "Filesystem mounted"); printf("hello!"); sdmmc_card_print_info(stdout, card); const char *file_hello = MOUNT_POINT"/hello.txt"; char data[EXAMPLE_MAX_CHAR_SIZE]; snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s!\n", "Hello how are you", card->cid.name); ret = s_example_write_file(file_hello, data); if (ret != ESP_OK) { return; } esp_vfs_fat_info(MOUNT_POINT,&total_bytes,&free_bytes); free_percent = (float)free_bytes / (float)total_bytes * 100; printf("Total space: %llu\n", total_bytes); printf("Free space: %llu\n", free_bytes); printf("Free percent: %.3f\n", free_percent); }