Skip to content

Commit

Permalink
tests/mtd_flashpage: add test for AUX slot
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Feb 28, 2024
1 parent e3819ca commit d91c581
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/drivers/mtd_flashpage/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ USEMODULE += mtd_flashpage
USEMODULE += mtd_write_page
USEMODULE += embunit

FEATURES_OPTIONAL += periph_flashpage_aux

# define an auxiliary slot on the internal flash
# NOTE: This should typically be set by the board as it can not be changed in the field.
# This is only defined here for the sake of the test.
SLOT_AUX_LEN := 0x8000

include $(RIOTBASE)/Makefile.include
5 changes: 5 additions & 0 deletions tests/drivers/mtd_flashpage/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BOARD_INSUFFICIENT_MEMORY := \
nucleo-l011k4 \
samd10-xmini \
stk3200 \
#
78 changes: 78 additions & 0 deletions tests/drivers/mtd_flashpage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* @file
*/
#include <stdbool.h>
#include <string.h>
#include <errno.h>

Expand Down Expand Up @@ -217,6 +218,80 @@ static void test_mtd_write_read_page(void)
#endif
}

#ifdef MODULE_PERIPH_FLASHPAGE_AUX
static bool mem_is_all_set(const uint8_t *buf, uint8_t c, size_t n)
{
for (const uint8_t *end = buf + n; buf != end; ++buf) {
if (*buf != c) {
return false;
}
}

return true;
}

static void test_mtd_aux_slot(void)
{
mtd_dev_t *dev_aux = mtd_aux;

mtd_init(dev_aux);

uint32_t sector = dev_aux->sector_count - 2;

static uint8_t buffer[FLASHPAGE_SIZE];

uint32_t page_0 = dev_aux->pages_per_sector * sector;
uint32_t page_1 = dev_aux->pages_per_sector * (sector + 1);
uint32_t page_size = dev_aux->page_size;

/* write dummy data to sectors */
memset(buffer, 0x23, dev_aux->page_size);
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev_aux, buffer, page_0, 0, page_size), 0);
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev_aux, buffer, page_1, 0, page_size), 0);

/* erase two sectors and check if they have been erased */
TEST_ASSERT_EQUAL_INT(mtd_erase_sector(dev_aux, sector, 2), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, 0, page_size), 0);
TEST_ASSERT(mem_is_all_set(buffer, 0xFF, page_size) || mem_is_all_set(buffer, 0x00, page_size));
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_1, 0, page_size), 0);
TEST_ASSERT(mem_is_all_set(buffer, 0xFF, page_size) || mem_is_all_set(buffer, 0x00, page_size));

/* write test data & read it back */
const char test_str[] = "0123456789";
uint32_t offset = 5;

TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev_aux, test_str, page_0, offset, sizeof(test_str)), 0);

Check warning on line 263 in tests/drivers/mtd_flashpage/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* write across page boundary */
offset = page_size - sizeof(test_str) / 2;
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev_aux, test_str, page_0, offset, sizeof(test_str)), 0);

Check warning on line 269 in tests/drivers/mtd_flashpage/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* write across sector boundary */
offset = page_size - sizeof(test_str) / 2
+ (dev_aux->pages_per_sector - 1) * page_size;
TEST_ASSERT_EQUAL_INT(mtd_write_page_raw(dev_aux, test_str, page_0, offset, sizeof(test_str)), 0);

Check warning on line 276 in tests/drivers/mtd_flashpage/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);

/* overwrite first test string, rely on MTD for read-modify-write */
const char test_str_2[] = "Hello World!";
offset = 5;
TEST_ASSERT_EQUAL_INT(mtd_write_page(dev_aux, test_str_2, page_0, offset, sizeof(test_str_2)), 0);

Check warning on line 283 in tests/drivers/mtd_flashpage/main.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, offset, sizeof(test_str_2)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str_2, buffer, sizeof(test_str_2)), 0);

/* test write_page across sectors */
offset = dev_aux->pages_per_sector * dev_aux->page_size - 2;
TEST_ASSERT_EQUAL_INT(mtd_write_page(dev_aux, test_str, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(mtd_read_page(dev_aux, buffer, page_0, offset, sizeof(test_str)), 0);
TEST_ASSERT_EQUAL_INT(memcmp(test_str, buffer, sizeof(test_str)), 0);
}
#endif

Test *tests_mtd_flashpage_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
Expand All @@ -225,6 +300,9 @@ Test *tests_mtd_flashpage_tests(void)
new_TestFixture(test_mtd_write_erase),
new_TestFixture(test_mtd_write_read),
new_TestFixture(test_mtd_write_read_page),
#ifdef MODULE_PERIPH_FLASHPAGE_AUX
new_TestFixture(test_mtd_aux_slot),
#endif
};

EMB_UNIT_TESTCALLER(mtd_flashpage_tests, setup, teardown, fixtures);
Expand Down

0 comments on commit d91c581

Please sign in to comment.