新增sd卡配置组件

This commit is contained in:
jarvis 2025-03-07 19:07:31 +08:00
parent efefb37dde
commit a41ac8d939
6 changed files with 236 additions and 3 deletions

View File

@ -15,6 +15,8 @@
"idf.portWin": "COM6", "idf.portWin": "COM6",
"idf.flashType": "UART", "idf.flashType": "UART",
"files.associations": { "files.associations": {
"ledc.h": "c" "ledc.h": "c",
"gpio.h": "c",
"sdmmc_host.h": "c"
} }
} }

View File

@ -0,0 +1,3 @@
idf_component_register(SRCS "FluxSD.c"
INCLUDE_DIRS "."
REQUIRES fatfs)

191
components/FluxSD/FluxSD.c Normal file
View File

@ -0,0 +1,191 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <inttypes.h>
#include <unistd.h>
#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<<pin)
#define CONFIG_EXAMPLE_PIN_CLK (14)
#define CONFIG_EXAMPLE_PIN_CMD (21)
#define CONFIG_EXAMPLE_PIN_D0 (13)
#define CONFIG_EXAMPLE_PIN_D1 (12)
#define CONFIG_EXAMPLE_PIN_D2 (48)
#define CONFIG_EXAMPLE_PIN_D3 (47)
const char* names[] = {"CLK", "CMD", "D0", "D1", "D2", "D3"};
const int pins[] = {CONFIG_EXAMPLE_PIN_CLK,
CONFIG_EXAMPLE_PIN_CMD,
CONFIG_EXAMPLE_PIN_D0,
CONFIG_EXAMPLE_PIN_D1,
CONFIG_EXAMPLE_PIN_D2,
CONFIG_EXAMPLE_PIN_D3
};
const int pin_count = sizeof(pins)/sizeof(pins[0]);
pin_configuration_t config = {
.names = names,
.pins = pins,
};
static uint32_t get_cycles_until_pin_level(int i, int level, int timeout) {
uint32_t start = esp_cpu_get_cycle_count();
while(gpio_get_level(i) == !level && esp_cpu_get_cycle_count() - start < timeout) {
;
}
uint32_t end = esp_cpu_get_cycle_count();
return end - start;
}
void check_sd_card_pins(pin_configuration_t *config, const int pin_count)
{
ESP_LOGI(TAG, "Testing SD pin connections and pullup strength");
gpio_config_t io_conf = {};
for (int i = 0; i < pin_count; ++i) {
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT_OUTPUT_OD;
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL(config->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;
}
}

View File

@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
#include <stdint.h>
#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

View File

@ -4,7 +4,7 @@ if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY)
endif() endif()
idf_component_register(SRCS "FreeBus.c" "i80_controller_example_main.c" "lvgl_demo_ui.c" ${embedded_images} 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 ".") INCLUDE_DIRS ".")
if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM) if(CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM)

View File

@ -19,6 +19,9 @@
#include "driver/ledc.h" #include "driver/ledc.h"
#include "lvgl.h" #include "lvgl.h"
/*包含SD卡读写控制头文件*/
#include "FluxSD.h"
/*导入Modbus控制头文件*/ /*导入Modbus控制头文件*/
#include "FreeBus.h" #include "FreeBus.h"
@ -449,7 +452,9 @@ void app_main(void)
/*配置LEDC进行背光亮度调节 */ /*配置LEDC进行背光亮度调节 */
example_ledc_init(); example_ledc_init();
/*初始化SD卡*/
flux_sd_init();
// Lock the mutex due to the LVGL APIs are not thread-safe // Lock the mutex due to the LVGL APIs are not thread-safe
if (example_lvgl_lock(-1)) { if (example_lvgl_lock(-1)) {
//example_lvgl_demo_ui(disp); //example_lvgl_demo_ui(disp);