Skip to content

Commit

Permalink
Automatic identification of SD cards in tests. Fixed #34
Browse files Browse the repository at this point in the history
  • Loading branch information
oyama committed Jun 13, 2024
1 parent 8ade63f commit 33aabdf
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 61 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 0 additions & 2 deletions EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions examples/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 8 additions & 12 deletions examples/benchmark/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,25 @@ 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,
PICO_DEFAULT_SPI_SCK_PIN,
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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
22 changes: 8 additions & 14 deletions tests/multicore.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,19 @@ 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,
PICO_DEFAULT_SPI_SCK_PIN,
PICO_DEFAULT_SPI_CSN_PIN,
24 * MHZ,
true);
#endif
filesystem_t *fat = filesystem_fat_create();
filesystem_t *littlefs = filesystem_littlefs_create(500, 16);

Expand All @@ -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, ...) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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();
Expand Down
42 changes: 29 additions & 13 deletions tests/test_blockdevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#define LOOPBACK_STORAGE_SIZE 1024
#define LOOPBACK_BLOCK_SIZE 512


#include <ctype.h>
static void print_hex(const char *label, const void *buffer, size_t length) {
const uint8_t *buf = buffer;
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions tests/test_copy_between_different_filesystems.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,20 @@ 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,
PICO_DEFAULT_SPI_SCK_PIN,
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);
Expand All @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 33aabdf

Please sign in to comment.