Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: add support for MTDs emulated in RAM #19443

Merged
merged 10 commits into from
Apr 12, 2023
Prev Previous commit
Next Next commit
tests/unittests/test-mtd: use emulated MTD as test mockup
  • Loading branch information
gschorcht committed Apr 12, 2023
commit 0cdcac96bc29d197b59259ae24432b52d96c000d
1 change: 1 addition & 0 deletions tests/unittests/tests-mtd/Makefile.include
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
USEMODULE += mtd
USEMODULE += mtd_emulated
USEMODULE += vfs
86 changes: 5 additions & 81 deletions tests/unittests/tests-mtd/tests-mtd.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#ifdef 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,89 +42,10 @@
#ifndef PAGE_SIZE
#define PAGE_SIZE 128
#endif
#ifndef WRITE_SIZE
#define WRITE_SIZE 1
#endif

static uint8_t dummy_memory[PAGE_PER_SECTOR * PAGE_SIZE * SECTOR_COUNT];

static int _init(mtd_dev_t *dev)
{
(void)dev;

memset(dummy_memory, 0xff, sizeof(dummy_memory));
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 (((addr % PAGE_SIZE) + 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,
};

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

static mtd_dev_t *dev = (mtd_dev_t*) &_dev;
#define dev (&mtd_emulated_dev0.base)

#endif /* MTD_0 */

Expand Down