Skip to content

Commit

Permalink
tests/spiffs: use emulated MTD as test mockup
Browse files Browse the repository at this point in the history
  • Loading branch information
gschorcht committed Apr 3, 2023
1 parent 6f360e0 commit bf98644
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 79 deletions.
1 change: 1 addition & 0 deletions tests/pkg_spiffs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include ../Makefile.tests_common

USEMODULE += spiffs
USEMODULE += embunit
USEMODULE += mtd_emulated

include $(RIOTBASE)/Makefile.include
86 changes: 7 additions & 79 deletions tests/pkg_spiffs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
* CONFIG_USE_HARDWARE_MTD is defined (add CFLAGS=-DCONFIG_USE_HARDWARE_MTD to
* the command line to enable it */
#if defined(MTD_0) && IS_ACTIVE(CONFIG_USE_HARDWARE_MTD)
#define USE_MTD_0

#define _dev (MTD_0)

#else

#include "mtd_emulated.h"

/* Test mock object implementing a simple RAM-based mtd */
#ifndef SECTOR_COUNT
#define SECTOR_COUNT 4
Expand All @@ -39,83 +43,10 @@
#define PAGE_SIZE 128
#endif

static uint8_t dummy_memory[PAGE_PER_SECTOR * PAGE_SIZE * SECTOR_COUNT];

static int _init(mtd_dev_t *dev)
{
(void)dev;
return 0;
}

static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
{
(void)dev;

if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
memcpy(buff, dummy_memory + addr, size);

return 0;
}

static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
{
(void)dev;

if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
if (size > PAGE_SIZE) {
return -EOVERFLOW;
}
memcpy(dummy_memory + addr, buff, size);

return 0;
}

static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size)
{
(void)dev;

if (size % (PAGE_PER_SECTOR * PAGE_SIZE) != 0) {
return -EOVERFLOW;
}
if (addr % (PAGE_PER_SECTOR * PAGE_SIZE) != 0) {
return -EOVERFLOW;
}
if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
memset(dummy_memory + addr, 0xff, size);

return 0;
}

static int _power(mtd_dev_t *dev, enum mtd_power_state power)
{
(void)dev;
(void)power;
return 0;
}
MTD_EMULATED_DEV(0, SECTOR_COUNT, PAGE_PER_SECTOR, PAGE_SIZE);

static const mtd_desc_t driver = {
.init = _init,
.read = _read,
.write = _write,
.erase = _erase,
.power = _power,
};
#define _dev (&mtd_emulated_dev0.base)

static mtd_dev_t dev = {
.driver = &driver,
.sector_count = SECTOR_COUNT,
.pages_per_sector = PAGE_PER_SECTOR,
.page_size = PAGE_SIZE,
.write_size = 1,
};

static mtd_dev_t *_dev = (mtd_dev_t*) &dev;
#endif /* MTD_0 */

static struct spiffs_desc spiffs_desc = {
Expand Down Expand Up @@ -439,9 +370,6 @@ static void tests_spiffs_partition(void)

Test *tests_spiffs(void)
{
#ifndef USE_MTD_0
memset(dummy_memory, 0xff, sizeof(dummy_memory));
#endif
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(tests_spiffs_format),
new_TestFixture(tests_spiffs_mount_umount),
Expand Down

0 comments on commit bf98644

Please sign in to comment.