From 33aabdfde6e98309831f9b9d3075ad05903155b3 Mon Sep 17 00:00:00 2001 From: Hiroyuki OYAMA Date: Thu, 13 Jun 2024 11:12:33 +0900 Subject: [PATCH] Automatic identification of SD cards in tests. Fixed #34 --- CMakeLists.txt | 2 - EXAMPLE.md | 2 - examples/benchmark/CMakeLists.txt | 3 -- examples/benchmark/main.c | 20 ++++----- tests/CMakeLists.txt | 6 --- tests/multicore.c | 22 ++++------ tests/test_blockdevice.c | 42 +++++++++++++------ .../test_copy_between_different_filesystems.c | 17 ++++---- 8 files changed, 53 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f06db75..51ab2c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,5 @@ cmake_minimum_required(VERSION 3.13...3.27) -option(WITHOUT_BLOCKDEVICE_SD "without SD block device in demo and tests") - include(vendor/pico_sdk_import.cmake) include(pico_vfs.cmake) set(FAMILY rp2040) diff --git a/EXAMPLE.md b/EXAMPLE.md index 48c5f13..65b2e61 100644 --- a/EXAMPLE.md +++ b/EXAMPLE.md @@ -21,8 +21,6 @@ mkdir build; cd build/ PICO_SDK_PATH=/path/to/pico-sdk cmake .. make hello fs_init_example benchmark logger ``` -If no SD card device is connected, the `-DWITHOUT_BLOCKDEVICE_SD` option can be specified to skip the SD card manipulation procedure from the demo and unit tests. - ### Circuit Diagram If an SD card is to be used, a separate circuit must be connected via SPI. As an example, the schematic using the [Adafruit MicroSD card breakout board+](https://www.adafruit.com/product/254) is as follows diff --git a/examples/benchmark/CMakeLists.txt b/examples/benchmark/CMakeLists.txt index 1b942ec..6fa622e 100644 --- a/examples/benchmark/CMakeLists.txt +++ b/examples/benchmark/CMakeLists.txt @@ -9,8 +9,5 @@ target_link_libraries(benchmark PRIVATE filesystem_littlefs filesystem_vfs ) -if(WITHOUT_BLOCKDEVICE_SD) - target_compile_definitions(benchmark PRIVATE WITHOUT_BLOCKDEVICE_SD=1) -endif() pico_enable_stdio_usb(benchmark 1) pico_add_extra_outputs(benchmark) diff --git a/examples/benchmark/main.c b/examples/benchmark/main.c index e784953..a2357a1 100644 --- a/examples/benchmark/main.c +++ b/examples/benchmark/main.c @@ -27,23 +27,12 @@ struct combination_map { filesystem_t *filesystem; }; -#if !defined(WITHOUT_BLOCKDEVICE_SD) #define NUM_COMBINATION 4 -#else -#define NUM_COMBINATION 2 -#endif - static struct combination_map combination[NUM_COMBINATION]; static void init_filesystem_combination(void) { blockdevice_t *flash = blockdevice_flash_create(0.5 * 1024 * 1024, 0); - filesystem_t *fat = filesystem_fat_create(); - filesystem_t *littlefs = filesystem_littlefs_create(500, 16); - combination[0] = (struct combination_map){.device = flash, .filesystem = fat}; - combination[1] = (struct combination_map){.device = flash, .filesystem = littlefs}; - -#if !defined(WITHOUT_BLOCKDEVICE_SD) blockdevice_t *sd = blockdevice_sd_create(spi0, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_RX_PIN, @@ -51,9 +40,12 @@ static void init_filesystem_combination(void) { PICO_DEFAULT_SPI_CSN_PIN, 24 * MHZ, false); + filesystem_t *fat = filesystem_fat_create(); + filesystem_t *littlefs = filesystem_littlefs_create(500, 16); + combination[0] = (struct combination_map){.device = flash, .filesystem = fat}; + combination[1] = (struct combination_map){.device = flash, .filesystem = littlefs}; combination[2] = (struct combination_map){.device = sd, .filesystem = fat}; combination[3] = (struct combination_map){.device = sd, .filesystem = littlefs}; -#endif } static uint32_t xor_rand(uint32_t *seed) { @@ -158,6 +150,10 @@ int main(void) { printf("Test of %s on %s:\n", setting.filesystem->name, setting.device->name); int err = fs_format(setting.filesystem, setting.device); + if (err == -1 && errno == 5005) { + printf("skip, device not connected\n"); + continue; + } if (err == -1) { printf("fs_format error: %s\n", strerror(errno)); return -1; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index df36c2e..b8c2c01 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -8,9 +8,6 @@ add_executable(tests test_stdio.c test_copy_between_different_filesystems.c ) -if(WITHOUT_BLOCKDEVICE_SD) - target_compile_definitions(tests PRIVATE WITHOUT_BLOCKDEVICE_SD=1) -endif() target_compile_options(tests PRIVATE -Werror -Wall -Wextra -Wnull-dereference) target_link_libraries(tests PRIVATE pico_stdlib @@ -28,9 +25,6 @@ pico_enable_stdio_usb(tests 1) add_executable(multicore multicore.c) -if(WITHOUT_BLOCKDEVICE_SD) - target_compile_definitions(multicore PRIVATE WITHOUT_BLOCKDEVICE_SD=1) -endif() target_compile_options(multicore PRIVATE -Werror -Wall -Wextra -Wnull-dereference) target_link_libraries(multicore PRIVATE pico_stdlib diff --git a/tests/multicore.c b/tests/multicore.c index 60cfda1..818eb22 100644 --- a/tests/multicore.c +++ b/tests/multicore.c @@ -30,19 +30,12 @@ struct combination_map { const char *label; }; -#if defined(WITHOUT_BLOCKDEVICE_SD) -#define NUM_COMBINATION 2 -#else #define NUM_COMBINATION 4 -#endif - - static struct combination_map combination[NUM_COMBINATION] = {0}; static void init_filesystem_combination(void) { blockdevice_t *flash = blockdevice_flash_create(PICO_FLASH_SIZE_BYTES - PICO_FS_DEFAULT_SIZE, FLASH_LENGTH_ALL); -#if !defined(WITHOUT_BLOCKDEVICE_SD) blockdevice_t *sd = blockdevice_sd_create(spi0, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_RX_PIN, @@ -50,7 +43,6 @@ static void init_filesystem_combination(void) { PICO_DEFAULT_SPI_CSN_PIN, 24 * MHZ, true); -#endif filesystem_t *fat = filesystem_fat_create(); filesystem_t *littlefs = filesystem_littlefs_create(500, 16); @@ -60,23 +52,19 @@ static void init_filesystem_combination(void) { combination[1] = (struct combination_map){ .device = flash, .filesystem = fat, .label = "FAT on Flash" }; -#if !defined(WITHOUT_BLOCKDEVICE_SD) combination[2] = (struct combination_map){ .device = sd, .filesystem = littlefs, .label = "littlefs on SD card" }; combination[3] = (struct combination_map){ .device = sd, .filesystem = fat, .label = "FAT on SD card" }; -#endif } static void cleanup_combination(void) { filesystem_littlefs_free(combination[0].filesystem); filesystem_fat_free(combination[1].filesystem); blockdevice_flash_free(combination[0].device); -#if !defined(WITHOUT_BLOCKDEVICE_SD) blockdevice_sd_free(combination[2].device); -#endif } static size_t test_printf(const char *format, ...) { @@ -306,11 +294,14 @@ static void test_write_while_read_two_files(void) { } -static void setup(filesystem_t *fs, blockdevice_t *device) { +static bool setup(filesystem_t *fs, blockdevice_t *device) { int err = fs_format(fs, device); + if (err == -1 && errno == 5005) + return false; assert(err == 0); err = fs_mount("/", fs, device); assert(err == 0); + return true; } static void cleanup() { @@ -327,7 +318,10 @@ int main(void) { struct combination_map *setting = &combination[i]; printf("%s:\n", setting->label); - setup(setting->filesystem, setting->device); + if (!setup(setting->filesystem, setting->device)) { + printf("skip, device not connected\n"); + continue; + } test_write_read_two_files(); test_write_while_read_two_files(); diff --git a/tests/test_blockdevice.c b/tests/test_blockdevice.c index cdcf046..1beca45 100644 --- a/tests/test_blockdevice.c +++ b/tests/test_blockdevice.c @@ -17,7 +17,6 @@ #define LOOPBACK_STORAGE_SIZE 1024 #define LOOPBACK_BLOCK_SIZE 512 - #include static void print_hex(const char *label, const void *buffer, size_t length) { const uint8_t *buf = buffer; @@ -60,6 +59,20 @@ static void cleanup(blockdevice_t *device) { device->erase(device, 0, length); } +static bool is_sd_card_connected(blockdevice_t *device) { + int err = device->deinit(device); + assert(err == BD_ERROR_OK); + + err = device->init(device); + if (err == BD_ERROR_OK) + return true; + else if (err == -5005) + return false; + else + assert(err == BD_ERROR_OK); + return false; +} + static void test_api_init(blockdevice_t *device) { test_printf("init"); @@ -160,7 +173,6 @@ void test_blockdevice(void) { cleanup(flash); blockdevice_flash_free(flash); -#if !defined(WITHOUT_BLOCKDEVICE_SD) printf("Block device SPI SD card:\n"); blockdevice_t *sd = blockdevice_sd_create(spi0, PICO_DEFAULT_SPI_TX_PIN, @@ -170,18 +182,22 @@ void test_blockdevice(void) { 10 * MHZ, false); assert(sd != NULL); - setup(sd); - - test_api_init(sd); - test_api_erase_program_read(sd); - test_api_trim(sd); - test_api_sync(sd); - test_api_size(sd); - test_api_attribute(sd); - - cleanup(sd); + if (is_sd_card_connected(sd)) { + setup(sd); + + test_api_init(sd); + test_api_erase_program_read(sd); + test_api_trim(sd); + test_api_sync(sd); + test_api_size(sd); + test_api_attribute(sd); + + cleanup(sd); + } else { + test_printf("init"); + printf("skip, device not connected\n"); + } blockdevice_sd_free(sd); -#endif printf("Block device Heap memory:\n"); blockdevice_t *heap = blockdevice_heap_create(HEAP_STORAGE_SIZE); diff --git a/tests/test_copy_between_different_filesystems.c b/tests/test_copy_between_different_filesystems.c index d51003e..abb7e17 100644 --- a/tests/test_copy_between_different_filesystems.c +++ b/tests/test_copy_between_different_filesystems.c @@ -20,19 +20,13 @@ struct combination_map { filesystem_t *filesystem2; }; -#if defined(WITHOUT_BLOCKDEVICE_SD) -#define NUM_COMBINATION 3 -#else #define NUM_COMBINATION 6 -#endif - static struct combination_map combination[NUM_COMBINATION]; static void init_filesystem_combination(void) { blockdevice_t *flash1 = blockdevice_flash_create(1 * 1024 * 1024, 512 * 1024); blockdevice_t *flash2 = blockdevice_flash_create(1 * 1024 * 1024 + 512 * 1024, 0); -#if !defined(WITHOUT_BLOCKDEVICE_SD) blockdevice_t *sd = blockdevice_sd_create(spi0, PICO_DEFAULT_SPI_TX_PIN, PICO_DEFAULT_SPI_RX_PIN, @@ -40,7 +34,6 @@ static void init_filesystem_combination(void) { PICO_DEFAULT_SPI_CSN_PIN, 24 * MHZ, true); -#endif filesystem_t *fat1 = filesystem_fat_create(); filesystem_t *fat2 = filesystem_fat_create(); filesystem_t *littlefs1 = filesystem_littlefs_create(500, 16); @@ -55,7 +48,6 @@ static void init_filesystem_combination(void) { combination[2] = (struct combination_map){ .device1 = flash1, .filesystem1 = littlefs1, .device2 = flash2, .filesystem2 = littlefs2, }; -#if !defined(WITHOUT_BLOCKDEVICE_SD) combination[3] = (struct combination_map){ .device1 = flash1, .filesystem1 = fat1, .device2 = sd, .filesystem2 = fat2, }; @@ -65,7 +57,6 @@ static void init_filesystem_combination(void) { combination[5] = (struct combination_map){ .device1 = flash1, .filesystem1 = littlefs1, .device2 = sd, .filesystem2 = littlefs2, }; -#endif } #define TEST_FILE_SIZE 100 * 1024; @@ -155,8 +146,16 @@ void test_copy_between_different_filesystems(void) { setting.filesystem2->name, setting.device2->name); int err = fs_format(setting.filesystem1, setting.device1); + if (err == -1 && errno == 5005) { + printf("skip, device not connected\n"); + continue; + } assert(err == 0); err = fs_format(setting.filesystem2, setting.device2); + if (err == -1 && errno == 5005) { + printf("skip, device not connected\n"); + continue; + } assert(err == 0); err = fs_mount("/a", setting.filesystem1, setting.device1);