fork the esp-idf fatfs for f_forward and exfat support
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(COMPONENTS main)
|
||||
set(EXTRA_COMPONENT_DIRS "${CMAKE_CURRENT_LIST_DIR}/../test_fatfs_common")
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
project(test_fatfs_sdcard)
|
||||
@@ -0,0 +1,14 @@
|
||||
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |
|
||||
|
||||
This test app runs a few FATFS test cases in a FAT-formatted SD card.
|
||||
|
||||
These tests require a development board with an SD card slot:
|
||||
|
||||
* ESP32-WROVER-KIT
|
||||
* ESP32-S2 USB_OTG
|
||||
* ESP32-C3-DevKit-C with an SD card breakout board
|
||||
|
||||
The test cases are split between `[sdmmc]` and `[sdspi]`. Only a few tests are executed for sdspi, though. The app could be refactored to ensure that a similar set of tests runs for sdmmc and sdspi.
|
||||
|
||||
See [../README.md](../README.md) for more information about FATFS test apps.
|
||||
@@ -0,0 +1,8 @@
|
||||
idf_component_register(SRCS "test_fatfs_sdcard_main.c" "test_fatfs_sdspi.c"
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES unity fatfs vfs sdmmc driver test_fatfs_common
|
||||
WHOLE_ARCHIVE)
|
||||
|
||||
if(CONFIG_SOC_SDMMC_HOST_SUPPORTED)
|
||||
target_sources(${COMPONENT_LIB} PRIVATE "test_fatfs_sdmmc.c")
|
||||
endif()
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include "unity.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
unity_run_menu();
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "unity.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/sdmmc_defs.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
#include "ff.h"
|
||||
#include "test_fatfs_common.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#define SDSPI_MISO_PIN 2
|
||||
#define SDSPI_MOSI_PIN 15
|
||||
#define SDSPI_CLK_PIN 14
|
||||
#define SDSPI_CS_PIN 13
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
// Adapted for internal test board ESP-32-S3-USB-OTG-Ev-BOARD_V1.0 (with ESP32-S2-MINI-1 module)
|
||||
#define SDSPI_MISO_PIN 37
|
||||
#define SDSPI_MOSI_PIN 35
|
||||
#define SDSPI_CLK_PIN 36
|
||||
#define SDSPI_CS_PIN 34
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3
|
||||
#define SDSPI_MISO_PIN 6
|
||||
#define SDSPI_MOSI_PIN 4
|
||||
#define SDSPI_CLK_PIN 5
|
||||
#define SDSPI_CS_PIN 1
|
||||
#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
|
||||
#endif //CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
|
||||
|
||||
#ifndef SPI_DMA_CHAN
|
||||
#define SPI_DMA_CHAN 1
|
||||
#endif //SPI_DMA_CHAN
|
||||
#define SDSPI_HOST_ID SPI2_HOST
|
||||
|
||||
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
|
||||
// No runner
|
||||
#include "driver/sdmmc_host.h"
|
||||
|
||||
static void test_setup_sdmmc(sdmmc_card_t **out_card)
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 16 * 1024
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
|
||||
*out_card = card;
|
||||
}
|
||||
|
||||
static void test_teardown_sdmmc(sdmmc_card_t *card)
|
||||
{
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
|
||||
}
|
||||
|
||||
static const char* test_filename = "/sdcard/hello.txt";
|
||||
|
||||
TEST_CASE("Mount fails cleanly without card inserted", "[fatfs][ignore]")
|
||||
{
|
||||
size_t heap_size;
|
||||
HEAP_SIZE_CAPTURE(heap_size);
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 5
|
||||
};
|
||||
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
printf("Initializing card, attempt %d\n", i);
|
||||
esp_err_t err = esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, NULL);
|
||||
printf("err=%d\n", err);
|
||||
TEST_ESP_ERR(ESP_ERR_TIMEOUT, err);
|
||||
}
|
||||
HEAP_SIZE_CHECK(heap_size, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can format partition", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
|
||||
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
|
||||
test_fatfs_read_file(test_filename);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can create and write file", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can read file", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
|
||||
test_fatfs_read_file(test_filename);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can read file with pread()", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_create_file_with_text(test_filename, fatfs_test_hello_str);
|
||||
test_fatfs_pread_file(test_filename);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) pwrite() works well", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_pwrite_file(test_filename);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) overwrite and append file", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_overwrite_append(test_filename);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can lseek", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_lseek("/sdcard/seek.txt");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can truncate", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_truncate_file("/sdcard/truncate.txt");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can ftruncate", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_ftruncate_file("/sdcard/ftrunc.txt");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) stat returns correct values", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_stat("/sdcard/stat.txt", "/sdcard");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) utime sets modification time", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_utime("/sdcard/utime.txt", "/sdcard");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) unlink removes a file", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_unlink("/sdcard/unlink.txt");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) link copies a file, rename moves a file", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_link_rename("/sdcard/link");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can create and remove directories", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_mkdir_rmdir("/sdcard/dir");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) can opendir root directory of FS", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_can_opendir("/sdcard");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_opendir_readdir_rewinddir("/sdcard/dir");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) multiple tasks can use same volume", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_concurrent("/sdcard/f");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);
|
||||
|
||||
TEST_CASE("(SD) write/read speed test", "[fatfs][sdmmc]")
|
||||
{
|
||||
size_t heap_size;
|
||||
HEAP_SIZE_CAPTURE(heap_size);
|
||||
|
||||
const size_t buf_size = 16 * 1024;
|
||||
uint32_t* buf = (uint32_t*) calloc(1, buf_size);
|
||||
esp_fill_random(buf, buf_size);
|
||||
const size_t file_size = 1 * 1024 * 1024;
|
||||
|
||||
sdmmc_speed_test(buf, 4 * 1024, file_size, true);
|
||||
sdmmc_speed_test(buf, 8 * 1024, file_size, true);
|
||||
sdmmc_speed_test(buf, 16 * 1024, file_size, true);
|
||||
|
||||
sdmmc_speed_test(buf, 4 * 1024, file_size, false);
|
||||
sdmmc_speed_test(buf, 8 * 1024, file_size, false);
|
||||
sdmmc_speed_test(buf, 16 * 1024, file_size, false);
|
||||
|
||||
free(buf);
|
||||
|
||||
HEAP_SIZE_CHECK(heap_size, 0);
|
||||
}
|
||||
|
||||
static void sdmmc_speed_test(void *buf, size_t buf_size, size_t file_size, bool write)
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = write,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 64 * 1024
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card));
|
||||
|
||||
test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount("/sdcard", card));
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) mount two FAT partitions, SDMMC and WL, at the same time", "[fatfs][sdmmc]")
|
||||
{
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5
|
||||
};
|
||||
|
||||
const char* filename_sd = "/sdcard/sd.txt";
|
||||
const char* filename_wl = "/spiflash/wl.txt";
|
||||
const char* str_sd = "this is sd\n";
|
||||
const char* str_wl = "this is spiflash\n";
|
||||
|
||||
/* Erase flash before the first use */
|
||||
const esp_partition_t *test_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
|
||||
TEST_ASSERT_NOT_NULL(test_partition);
|
||||
esp_partition_erase_range(test_partition, 0, test_partition->size);
|
||||
|
||||
/* Mount FATFS in SD can WL at the same time. Create a file on each FS */
|
||||
wl_handle_t wl_handle = WL_INVALID_HANDLE;
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
|
||||
unlink(filename_sd);
|
||||
unlink(filename_wl);
|
||||
test_fatfs_create_file_with_text(filename_sd, str_sd);
|
||||
test_fatfs_create_file_with_text(filename_wl, str_wl);
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle));
|
||||
test_teardown_sdmmc(card);
|
||||
|
||||
/* Check that the file "sd.txt" was created on FS in SD, and has the right data */
|
||||
test_setup_sdmmc(&card);
|
||||
TEST_ASSERT_NULL(fopen(filename_wl, "r"));
|
||||
FILE* f = fopen(filename_sd, "r");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
char buf[64];
|
||||
TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
|
||||
TEST_ASSERT_EQUAL(0, strcmp(buf, str_sd));
|
||||
fclose(f);
|
||||
test_teardown_sdmmc(card);
|
||||
|
||||
/* Check that the file "wl.txt" was created on FS in WL, and has the right data */
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", NULL, &mount_config, &wl_handle));
|
||||
TEST_ASSERT_NULL(fopen(filename_sd, "r"));
|
||||
f = fopen(filename_wl, "r");
|
||||
TEST_ASSERT_NOT_NULL(f);
|
||||
TEST_ASSERT_NOT_NULL(fgets(buf, sizeof(buf) - 1, f));
|
||||
TEST_ASSERT_EQUAL(0, strcmp(buf, str_wl));
|
||||
fclose(f);
|
||||
TEST_ESP_OK(esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle));
|
||||
}
|
||||
|
||||
/*
|
||||
* In FatFs menuconfig, set CONFIG_FATFS_API_ENCODING to UTF-8 and set the
|
||||
* Codepage to CP936 (Simplified Chinese) in order to run the following tests.
|
||||
* Ensure that the text editor is UTF-8 compatible when compiling these tests.
|
||||
*/
|
||||
#if defined(CONFIG_FATFS_API_ENCODING_UTF_8) && (CONFIG_FATFS_CODEPAGE == 936)
|
||||
|
||||
static const char* test_filename_utf_8 = "/sdcard/测试文件.txt";
|
||||
|
||||
TEST_CASE("(SD) can read file using UTF-8 encoded strings", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_create_file_with_text(test_filename_utf_8, fatfs_test_hello_str_utf);
|
||||
test_fatfs_read_file_utf_8(test_filename_utf_8);
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF-8 encoded strings", "[fatfs][ignore]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_opendir_readdir_rewinddir_utf_8("/sdcard/目录");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
#endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936
|
||||
|
||||
TEST_CASE("(SD) can get partition info", "[fatfs][sdmmc]")
|
||||
{
|
||||
sdmmc_card_t *card = NULL;
|
||||
test_setup_sdmmc(&card);
|
||||
test_fatfs_info("/sdcard", "/sdcard/test.txt");
|
||||
test_teardown_sdmmc(card);
|
||||
}
|
||||
|
||||
#endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
|
||||
@@ -0,0 +1,199 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/unistd.h>
|
||||
#include "unity.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_random.h"
|
||||
#include "esp_vfs.h"
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/sdmmc_defs.h"
|
||||
#include "sdmmc_cmd.h"
|
||||
#include "ff.h"
|
||||
#include "test_fatfs_common.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
#if CONFIG_IDF_TARGET_ESP32
|
||||
#define SDSPI_MISO_PIN 2
|
||||
#define SDSPI_MOSI_PIN 15
|
||||
#define SDSPI_CLK_PIN 14
|
||||
#define SDSPI_CS_PIN 13
|
||||
#elif CONFIG_IDF_TARGET_ESP32S2
|
||||
// Adapted for internal test board ESP-32-S3-USB-OTG-Ev-BOARD_V1.0 (with ESP32-S2-MINI-1 module)
|
||||
#define SDSPI_MISO_PIN 37
|
||||
#define SDSPI_MOSI_PIN 35
|
||||
#define SDSPI_CLK_PIN 36
|
||||
#define SDSPI_CS_PIN 34
|
||||
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6
|
||||
#define SDSPI_MISO_PIN 6
|
||||
#define SDSPI_MOSI_PIN 4
|
||||
#define SDSPI_CLK_PIN 5
|
||||
#define SDSPI_CS_PIN 1
|
||||
#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
|
||||
#elif CONFIG_IDF_TARGET_ESP32H2
|
||||
#define SDSPI_MISO_PIN 0
|
||||
#define SDSPI_MOSI_PIN 5
|
||||
#define SDSPI_CLK_PIN 4
|
||||
#define SDSPI_CS_PIN 1
|
||||
#define SPI_DMA_CHAN SPI_DMA_CH_AUTO
|
||||
#endif
|
||||
|
||||
#ifndef SPI_DMA_CHAN
|
||||
#define SPI_DMA_CHAN 1
|
||||
#endif //SPI_DMA_CHAN
|
||||
#define SDSPI_HOST_ID SPI2_HOST
|
||||
|
||||
|
||||
typedef struct sdspi_mem {
|
||||
size_t heap_size;
|
||||
uint32_t* buf;
|
||||
} sdspi_mem_t;
|
||||
|
||||
static const char* s_test_filename = "/sdcard/hello.txt";
|
||||
static void sdspi_speed_test(void *buf, size_t buf_size, size_t file_size, bool write);
|
||||
|
||||
static void test_setup_sdspi(sdspi_mem_t* mem)
|
||||
{
|
||||
HEAP_SIZE_CAPTURE(mem->heap_size);
|
||||
|
||||
const size_t buf_size = 16 * 1024;
|
||||
mem->buf = (uint32_t*) calloc(1, buf_size);
|
||||
esp_fill_random(mem->buf, buf_size);
|
||||
|
||||
spi_bus_config_t bus_cfg = {
|
||||
.mosi_io_num = SDSPI_MOSI_PIN,
|
||||
.miso_io_num = SDSPI_MISO_PIN,
|
||||
.sclk_io_num = SDSPI_CLK_PIN,
|
||||
.quadwp_io_num = -1,
|
||||
.quadhd_io_num = -1,
|
||||
.max_transfer_sz = 4000,
|
||||
};
|
||||
esp_err_t err = spi_bus_initialize(SDSPI_HOST_ID, &bus_cfg, SPI_DMA_CHAN);
|
||||
TEST_ESP_OK(err);
|
||||
}
|
||||
|
||||
static void test_teardown_sdspi(sdspi_mem_t* mem)
|
||||
{
|
||||
free(mem->buf);
|
||||
spi_bus_free(SDSPI_HOST_ID);
|
||||
HEAP_SIZE_CHECK(mem->heap_size, 0);
|
||||
}
|
||||
|
||||
TEST_CASE("(SDSPI) write/read speed test", "[fatfs][sdspi]")
|
||||
{
|
||||
sdspi_mem_t mem;
|
||||
size_t file_size = 1 * 1024 * 1024;
|
||||
|
||||
test_setup_sdspi(&mem);
|
||||
|
||||
sdspi_speed_test(mem.buf, 4 * 1024, file_size, true);
|
||||
sdspi_speed_test(mem.buf, 8 * 1024, file_size, true);
|
||||
sdspi_speed_test(mem.buf, 16 * 1024, file_size, true);
|
||||
|
||||
sdspi_speed_test(mem.buf, 4 * 1024, file_size, false);
|
||||
sdspi_speed_test(mem.buf, 8 * 1024, file_size, false);
|
||||
sdspi_speed_test(mem.buf, 16 * 1024, file_size, false);
|
||||
|
||||
test_teardown_sdspi(&mem);
|
||||
}
|
||||
|
||||
static void sdspi_speed_test(void *buf, size_t buf_size, size_t file_size, bool write)
|
||||
{
|
||||
const char path[] = "/sdcard";
|
||||
sdmmc_card_t *card;
|
||||
card = NULL;
|
||||
sdspi_device_config_t device_cfg = {
|
||||
.gpio_cs = SDSPI_CS_PIN,
|
||||
.host_id = SDSPI_HOST_ID,
|
||||
.gpio_cd = SDSPI_SLOT_NO_CD,
|
||||
.gpio_wp = SDSPI_SLOT_NO_WP,
|
||||
.gpio_int = SDSPI_SLOT_NO_INT,
|
||||
};
|
||||
|
||||
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
||||
host.slot = SDSPI_HOST_ID;
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = write,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 64 * 1024
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
|
||||
|
||||
test_fatfs_rw_speed("/sdcard/4mb.bin", buf, buf_size, file_size, write);
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
|
||||
}
|
||||
|
||||
TEST_CASE("(SDSPI) can get partition info", "[fatfs][sdspi]")
|
||||
{
|
||||
sdspi_mem_t mem;
|
||||
|
||||
test_setup_sdspi(&mem);
|
||||
|
||||
const char path[] = "/sdcard";
|
||||
sdmmc_card_t *card;
|
||||
card = NULL;
|
||||
sdspi_device_config_t device_cfg = {
|
||||
.gpio_cs = SDSPI_CS_PIN,
|
||||
.host_id = SDSPI_HOST_ID,
|
||||
.gpio_cd = SDSPI_SLOT_NO_CD,
|
||||
.gpio_wp = SDSPI_SLOT_NO_WP,
|
||||
.gpio_int = SDSPI_SLOT_NO_INT,
|
||||
};
|
||||
|
||||
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
||||
host.slot = SDSPI_HOST_ID;
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 64 * 1024
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
|
||||
|
||||
test_fatfs_info("/sdcard", "/sdcard/test.txt");
|
||||
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
|
||||
|
||||
test_teardown_sdspi(&mem);
|
||||
}
|
||||
|
||||
TEST_CASE("(SDSPI) can format card", "[fatfs][sdspi]")
|
||||
{
|
||||
sdspi_mem_t mem;
|
||||
test_setup_sdspi(&mem);
|
||||
|
||||
const char path[] = "/sdcard";
|
||||
sdmmc_card_t *card;
|
||||
card = NULL;
|
||||
sdspi_device_config_t device_cfg = {
|
||||
.gpio_cs = SDSPI_CS_PIN,
|
||||
.host_id = SDSPI_HOST_ID,
|
||||
.gpio_cd = SDSPI_SLOT_NO_CD,
|
||||
.gpio_wp = SDSPI_SLOT_NO_WP,
|
||||
.gpio_int = SDSPI_SLOT_NO_INT,
|
||||
};
|
||||
|
||||
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
|
||||
host.slot = SDSPI_HOST_ID;
|
||||
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 5,
|
||||
.allocation_unit_size = 64 * 1024
|
||||
};
|
||||
TEST_ESP_OK(esp_vfs_fat_sdspi_mount(path, &host, &device_cfg, &mount_config, &card));
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_format("/sdcard", card));
|
||||
test_fatfs_create_file_with_text(s_test_filename, fatfs_test_hello_str);
|
||||
test_fatfs_read_file(s_test_filename);
|
||||
TEST_ESP_OK(esp_vfs_fat_sdcard_unmount(path, card));
|
||||
test_teardown_sdspi(&mem);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
factory, app, factory, 0x10000, 1M,
|
||||
storage, data, fat, , 528k,
|
||||
|
@@ -0,0 +1,75 @@
|
||||
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: CC0-1.0
|
||||
|
||||
import pytest
|
||||
from pytest_embedded import Dut
|
||||
|
||||
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.sdcard_sdmode
|
||||
@pytest.mark.parametrize(
|
||||
'config',
|
||||
[
|
||||
'default',
|
||||
'release',
|
||||
]
|
||||
)
|
||||
def test_fatfs_sdcard_generic_sdmmc(dut: Dut) -> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests')
|
||||
dut.write('')
|
||||
dut.expect_exact('Enter test for running.')
|
||||
dut.write('[sdmmc]')
|
||||
dut.expect_unity_test_output(timeout=120)
|
||||
|
||||
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.esp32s2
|
||||
@pytest.mark.esp32c3
|
||||
@pytest.mark.sdcard_spimode
|
||||
@pytest.mark.parametrize(
|
||||
'config',
|
||||
[
|
||||
'default',
|
||||
'release',
|
||||
]
|
||||
)
|
||||
def test_fatfs_sdcard_generic_sdspi(dut: Dut) -> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests')
|
||||
dut.write('')
|
||||
dut.expect_exact('Enter test for running.')
|
||||
dut.write('[sdspi]')
|
||||
dut.expect_unity_test_output(timeout=120)
|
||||
|
||||
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.sdcard_sdmode
|
||||
@pytest.mark.psram
|
||||
@pytest.mark.parametrize(
|
||||
'config',
|
||||
[
|
||||
'psram',
|
||||
]
|
||||
)
|
||||
def test_fatfs_sdcard_psram_sdmmc(dut: Dut) -> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests')
|
||||
dut.write('')
|
||||
dut.expect_exact('Enter test for running.')
|
||||
dut.write('[sdmmc]')
|
||||
dut.expect_unity_test_output(timeout=120)
|
||||
|
||||
|
||||
@pytest.mark.esp32
|
||||
@pytest.mark.sdcard_spimode
|
||||
@pytest.mark.psram
|
||||
@pytest.mark.parametrize(
|
||||
'config',
|
||||
[
|
||||
'psram',
|
||||
]
|
||||
)
|
||||
def test_fatfs_sdcard_psram_sdspi(dut: Dut) -> None:
|
||||
dut.expect_exact('Press ENTER to see the list of tests')
|
||||
dut.write('')
|
||||
dut.expect_exact('Enter test for running.')
|
||||
dut.write('[sdspi]')
|
||||
dut.expect_unity_test_output(timeout=120)
|
||||
@@ -0,0 +1,3 @@
|
||||
CONFIG_SPIRAM=y
|
||||
CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=0
|
||||
CONFIG_FATFS_ALLOC_PREFER_EXTRAM=y
|
||||
@@ -0,0 +1,2 @@
|
||||
CONFIG_COMPILER_OPTIMIZATION_SIZE=y
|
||||
CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y
|
||||
@@ -0,0 +1,19 @@
|
||||
# General options for additional checks
|
||||
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
|
||||
CONFIG_COMPILER_WARN_WRITE_STRINGS=y
|
||||
CONFIG_BOOTLOADER_LOG_LEVEL_WARN=y
|
||||
CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK=y
|
||||
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
|
||||
CONFIG_COMPILER_STACK_CHECK=y
|
||||
|
||||
# disable task watchdog since this app uses an interactive menu
|
||||
CONFIG_ESP_TASK_WDT_INIT=n
|
||||
|
||||
# some tests verify file name encoding
|
||||
CONFIG_FATFS_API_ENCODING_UTF_8=y
|
||||
CONFIG_FATFS_CODEPAGE_936=y
|
||||
|
||||
# some of the tests verify concurrent operation of FAT partitions in
|
||||
# an SD card and in Flash, so need to use a custom partition table.
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
Reference in New Issue
Block a user