From 0078025d7b94f52c56cfac9aa00747d9b526c026 Mon Sep 17 00:00:00 2001 From: "radek.tandler" Date: Tue, 18 Apr 2023 14:46:20 +0200 Subject: [PATCH] fatfs: host test migrated to CMake and esp_partition emulation for linux --- components/fatfs/CMakeLists.txt | 42 +++++--- components/fatfs/host_test/CMakeLists.txt | 10 ++ components/fatfs/host_test/README.md | 2 + .../fatfs/host_test/main/CMakeLists.txt | 6 ++ components/fatfs/host_test/main/main.cpp | 7 ++ .../fatfs/host_test/main/test_fatfs.cpp | 95 +++++++++++++++++++ .../fatfs/host_test/partition_table.csv | 6 ++ .../fatfs/host_test/pytest_fatfs_linux.py | 10 ++ components/fatfs/host_test/sdkconfig.defaults | 12 +++ 9 files changed, 178 insertions(+), 12 deletions(-) create mode 100644 components/fatfs/host_test/CMakeLists.txt create mode 100644 components/fatfs/host_test/README.md create mode 100644 components/fatfs/host_test/main/CMakeLists.txt create mode 100644 components/fatfs/host_test/main/main.cpp create mode 100644 components/fatfs/host_test/main/test_fatfs.cpp create mode 100644 components/fatfs/host_test/partition_table.csv create mode 100644 components/fatfs/host_test/pytest_fatfs_linux.py create mode 100644 components/fatfs/host_test/sdkconfig.defaults diff --git a/components/fatfs/CMakeLists.txt b/components/fatfs/CMakeLists.txt index 9ff1d79257..b6a288e0d0 100644 --- a/components/fatfs/CMakeLists.txt +++ b/components/fatfs/CMakeLists.txt @@ -1,16 +1,34 @@ +idf_build_get_property(target IDF_TARGET) + set(srcs "diskio/diskio.c" - "diskio/diskio_rawflash.c" - "diskio/diskio_sdmmc.c" - "diskio/diskio_wl.c" - "src/ff.c" - "port/freertos/ffsystem.c" - "src/ffunicode.c" - "vfs/vfs_fat.c" - "vfs/vfs_fat_sdmmc.c" - "vfs/vfs_fat_spiflash.c") + "diskio/diskio_rawflash.c" + "diskio/diskio_wl.c" + "src/ff.c" + "src/ffunicode.c") + +set(include_dirs "diskio" "src") + +set(requires "wear_levelling") + +# for linux, we do not have support for vfs and sdmmc, for real targets, add respective sources +if(${target} STREQUAL "linux") + list(APPEND srcs "port/linux/ffsystem.c") +else() + list(APPEND srcs "port/freertos/ffsystem.c" + "diskio/diskio_sdmmc.c" + "vfs/vfs_fat.c" + "vfs/vfs_fat_sdmmc.c" + "vfs/vfs_fat_spiflash.c") + + list(APPEND include_dirs "vfs") + + list(APPEND requires "sdmmc") + + list(APPEND priv_requires "vfs") +endif() idf_component_register(SRCS ${srcs} - INCLUDE_DIRS diskio vfs src - REQUIRES wear_levelling sdmmc - PRIV_REQUIRES vfs + INCLUDE_DIRS ${include_dirs} + REQUIRES ${requires} + PRIV_REQUIRES ${priv_requires} ) diff --git a/components/fatfs/host_test/CMakeLists.txt b/components/fatfs/host_test/CMakeLists.txt new file mode 100644 index 0000000000..48267da8d3 --- /dev/null +++ b/components/fatfs/host_test/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +set(COMPONENTS main) +# Freertos is included via common components. However, CATCH isn't compatible with the FreeRTOS component yet, hence +# using the FreeRTOS mock component. +# target. +list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") + +project(fatfs_host_test) diff --git a/components/fatfs/host_test/README.md b/components/fatfs/host_test/README.md new file mode 100644 index 0000000000..37c142df16 --- /dev/null +++ b/components/fatfs/host_test/README.md @@ -0,0 +1,2 @@ +| Supported Targets | Linux | +| ----------------- | ----- | diff --git a/components/fatfs/host_test/main/CMakeLists.txt b/components/fatfs/host_test/main/CMakeLists.txt new file mode 100644 index 0000000000..ef8aeb43db --- /dev/null +++ b/components/fatfs/host_test/main/CMakeLists.txt @@ -0,0 +1,6 @@ +idf_component_register(SRCS "main.cpp" + "test_fatfs.cpp" + INCLUDE_DIRS "$ENV{IDF_PATH}/tools/catch" + REQUIRES fatfs + WHOLE_ARCHIVE + ) diff --git a/components/fatfs/host_test/main/main.cpp b/components/fatfs/host_test/main/main.cpp new file mode 100644 index 0000000000..cd66dc3083 --- /dev/null +++ b/components/fatfs/host_test/main/main.cpp @@ -0,0 +1,7 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#define CATCH_CONFIG_MAIN +#include "catch.hpp" diff --git a/components/fatfs/host_test/main/test_fatfs.cpp b/components/fatfs/host_test/main/test_fatfs.cpp new file mode 100644 index 0000000000..1bb51cb9a4 --- /dev/null +++ b/components/fatfs/host_test/main/test_fatfs.cpp @@ -0,0 +1,95 @@ +/* + * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +#include + +#include "ff.h" +#include "esp_partition.h" +#include "wear_levelling.h" +#include "diskio_impl.h" +#include "diskio_wl.h" + +#include "catch.hpp" + +TEST_CASE("create volume, open file, write and read back data", "[fatfs]") +{ + FRESULT fr_result; + BYTE pdrv; + FATFS fs; + FIL file; + UINT bw; + + esp_err_t esp_result; + + const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, "storage"); + + // Mount wear-levelled partition + wl_handle_t wl_handle; + esp_result = wl_mount(partition, &wl_handle); + REQUIRE(esp_result == ESP_OK); + + // Get a physical drive + esp_result = ff_diskio_get_drive(&pdrv); + REQUIRE(esp_result == ESP_OK); + + // Register physical drive as wear-levelled partition + esp_result = ff_diskio_register_wl_partition(pdrv, wl_handle); + + // Create FAT volume on the entire disk + LBA_t part_list[] = {100, 0, 0, 0}; + BYTE work_area[FF_MAX_SS]; + + fr_result = f_fdisk(pdrv, part_list, work_area); + REQUIRE(fr_result == FR_OK); + const MKFS_PARM opt = {(BYTE)FM_ANY, 0, 0, 0, 0}; + fr_result = f_mkfs("", &opt, work_area, sizeof(work_area)); // Use default volume + + // Mount the volume + fr_result = f_mount(&fs, "", 0); + REQUIRE(fr_result == FR_OK); + + // Open, write and read data + fr_result = f_open(&file, "test.txt", FA_OPEN_ALWAYS | FA_READ | FA_WRITE); + REQUIRE(fr_result == FR_OK); + + // Generate data + uint32_t data_size = 100000; + + char *data = (char*) malloc(data_size); + char *read = (char*) malloc(data_size); + + for(uint32_t i = 0; i < data_size; i += sizeof(i)) + { + *((uint32_t*)(data + i)) = i; + } + + // Write generated data + fr_result = f_write(&file, data, data_size, &bw); + REQUIRE(fr_result == FR_OK); + REQUIRE(bw == data_size); + + // Move to beginning of file + fr_result = f_lseek(&file, 0); + REQUIRE(fr_result == FR_OK); + + // Read written data + fr_result = f_read(&file, read, data_size, &bw); + REQUIRE(fr_result == FR_OK); + REQUIRE(bw == data_size); + + REQUIRE(memcmp(data, read, data_size) == 0); + + // Close file + fr_result = f_close(&file); + REQUIRE(fr_result == FR_OK); + + // Unmount default volume + fr_result = f_mount(0, "", 0); + REQUIRE(fr_result == FR_OK); + + free(read); + free(data); +} diff --git a/components/fatfs/host_test/partition_table.csv b/components/fatfs/host_test/partition_table.csv new file mode 100644 index 0000000000..1c79321a10 --- /dev/null +++ b/components/fatfs/host_test/partition_table.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +storage, data, fat, , 1M, diff --git a/components/fatfs/host_test/pytest_fatfs_linux.py b/components/fatfs/host_test/pytest_fatfs_linux.py new file mode 100644 index 0000000000..7b12c361a1 --- /dev/null +++ b/components/fatfs/host_test/pytest_fatfs_linux.py @@ -0,0 +1,10 @@ +# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD +# SPDX-License-Identifier: Unlicense OR CC0-1.0 +import pytest +from pytest_embedded import Dut + + +@pytest.mark.linux +@pytest.mark.host_test +def test_fatfs_linux(dut: Dut) -> None: + dut.expect_exact('All tests passed', timeout=120) diff --git a/components/fatfs/host_test/sdkconfig.defaults b/components/fatfs/host_test/sdkconfig.defaults new file mode 100644 index 0000000000..b86c4111cd --- /dev/null +++ b/components/fatfs/host_test/sdkconfig.defaults @@ -0,0 +1,12 @@ +CONFIG_IDF_TARGET="linux" +CONFIG_COMPILER_CXX_EXCEPTIONS=y +CONFIG_UNITY_ENABLE_IDF_TEST_RUNNER=n +CONFIG_WL_SECTOR_SIZE=4096 +CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_PARTITION_TABLE_OFFSET=0x8000 +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partition_table.csv" +CONFIG_ESPTOOLPY_FLASHSIZE="8MB" +CONFIG_MMU_PAGE_SIZE=0X10000 +CONFIG_ESP_PARTITION_ENABLE_STATS=y +CONFIG_FATFS_VOLUME_COUNT=2