diff --git a/.vscode/settings.json b/.vscode/settings.json index f421afc..ee293ab 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,8 @@ "idf.portWin": "COM6", "idf.flashType": "UART", "files.associations": { - "ledc.h": "c" + "ledc.h": "c", + "gpio.h": "c", + "sdmmc_host.h": "c" } } diff --git a/components/FluxSD/CMakeLists.txt b/components/FluxSD/CMakeLists.txt new file mode 100644 index 0000000..e16109d --- /dev/null +++ b/components/FluxSD/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "FluxSD.c" + INCLUDE_DIRS "." + REQUIRES fatfs) diff --git a/components/FluxSD/FluxSD.c b/components/FluxSD/FluxSD.c new file mode 100644 index 0000000..2785939 --- /dev/null +++ b/components/FluxSD/FluxSD.c @@ -0,0 +1,191 @@ +/* + * 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; +} + +void flux_sd_init(void) +{ + esp_err_t ret; + + esp_vfs_fat_sdmmc_mount_config_t mount_config = { + .format_if_mount_failed = false, + .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; + } +} + + diff --git a/components/FluxSD/FluxSD.h b/components/FluxSD/FluxSD.h new file mode 100644 index 0000000..9c8ce79 --- /dev/null +++ b/components/FluxSD/FluxSD.h @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define EXAMPLE_MAX_CHAR_SIZE 64 +#define MOUNT_POINT "/sdcard" + + +typedef struct { + const char** names; + const int* pins; +} pin_configuration_t; + + +void check_sd_card_pins(pin_configuration_t *config, const int pin_count); + +void flux_sd_init(void); +#ifdef __cplusplus +} +#endif + + diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 3a45aac..8b4dbb6 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,7 +4,7 @@ if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY) endif() idf_component_register(SRCS "FreeBus.c" "i80_controller_example_main.c" "lvgl_demo_ui.c" ${embedded_images} -REQUIRES FluxUI FluxPower spiffs +REQUIRES FluxUI FluxPower spiffs FluxSD INCLUDE_DIRS ".") if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM) diff --git a/main/i80_controller_example_main.c b/main/i80_controller_example_main.c index 56880ab..2ba92dc 100644 --- a/main/i80_controller_example_main.c +++ b/main/i80_controller_example_main.c @@ -19,6 +19,9 @@ #include "driver/ledc.h" #include "lvgl.h" +/*包含SD卡读写控制头文件*/ +#include "FluxSD.h" + /*导入Modbus控制头文件*/ #include "FreeBus.h" @@ -449,7 +452,9 @@ void app_main(void) /*配置LEDC进行背光亮度调节 */ example_ledc_init(); - + /*初始化SD卡*/ + flux_sd_init(); + // Lock the mutex due to the LVGL APIs are not thread-safe if (example_lvgl_lock(-1)) { //example_lvgl_demo_ui(disp);