201 lines
5.8 KiB
C
201 lines
5.8 KiB
C
/*
|
||
* 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;
|
||
}
|
||
|
||
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);
|
||
|
||
// esp_vfs_fat_register(MOUNT_POINT, card,)
|
||
|
||
/*<2A>ļ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
|
||
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;
|
||
}
|
||
|
||
}
|
||
|
||
|