From a0054654eecfb3723fc238706d736553ce706bb8 Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Fri, 28 Sep 2018 11:25:47 +0200 Subject: [PATCH 1/3] sam0 flashpage_write: fix writes translation from RIOT to CPU pages --- cpu/sam0_common/include/cpu_conf.h | 6 ++++-- cpu/sam0_common/periph/flashpage.c | 11 ++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/cpu/sam0_common/include/cpu_conf.h b/cpu/sam0_common/include/cpu_conf.h index 48c1658bcb..18894358de 100644 --- a/cpu/sam0_common/include/cpu_conf.h +++ b/cpu/sam0_common/include/cpu_conf.h @@ -43,8 +43,10 @@ extern "C" { */ /* a flashpage in RIOT is mapped to a flash row on the SAM0s */ #define FLASHPAGE_SIZE (256U) -/* one SAM0 row contains 4 SAM0 pages -> 4x the amount of RIOT flashpages */ -#define FLASHPAGE_NUMOF (FLASH_NB_OF_PAGES / 4) +/* one SAM0 row contains 4 SAM0 pages, so 4 SAM0 pages contain the amount of a RIOT flashpage */ +#define FLASHPAGE_PAGES_PER_ROW (4) +/* number of RIOT flashpages on device */ +#define FLASHPAGE_NUMOF (FLASH_NB_OF_PAGES / FLASHPAGE_PAGES_PER_ROW) /* The minimum block size which can be written is 16B. However, the erase * block is always FLASHPAGE_SIZE (SAM0 row). */ diff --git a/cpu/sam0_common/periph/flashpage.c b/cpu/sam0_common/periph/flashpage.c index 6c7d9495ab..c52cbd2d30 100644 --- a/cpu/sam0_common/periph/flashpage.c +++ b/cpu/sam0_common/periph/flashpage.c @@ -103,7 +103,16 @@ void flashpage_write(int page, const void *data) /* write data to page */ if (data != NULL) { - flashpage_write_raw(page_addr, data, FLASHPAGE_SIZE); + /* One RIOT page is FLASHPAGE_PAGES_PER_ROW SAM0 flash pages (a row) as + * defined in the file cpu/sam0_common/include/cpu_conf.h, therefore we + * have to split the write into FLASHPAGE_PAGES_PER_ROW raw calls + * underneath, each writing a physical page in chunks of 4 bytes (see + * flashpage_write_raw) + * The erasing is done once as a full row is always reased. + */ + for (unsigned curpage = 0; curpage < FLASHPAGE_PAGES_PER_ROW; curpage++) { + flashpage_write_raw(page_addr + (curpage * NVMCTRL_PAGE_SIZE / 4), (void *) ((uint32_t *) data + (curpage * NVMCTRL_PAGE_SIZE / 4)), NVMCTRL_PAGE_SIZE); + } } } From 50f2078fcda5b3ad13cb88c579570e5aec55a741 Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Thu, 20 Dec 2018 21:18:52 +0100 Subject: [PATCH 2/3] tests/periph_flashpage: test_last_raw must erase page before write --- tests/periph_flashpage/main.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/periph_flashpage/main.c b/tests/periph_flashpage/main.c index d517625c58..84f995d3fa 100644 --- a/tests/periph_flashpage/main.c +++ b/tests/periph_flashpage/main.c @@ -335,6 +335,9 @@ static int cmd_test_last_raw(int argc, char **argv) /* try to align */ memcpy(raw_buf, "test12344321tset", 16); + /* erase the page first */ + flashpage_write(((int)FLASHPAGE_NUMOF - 2), NULL); + flashpage_write_raw((void*) ((int)CPU_FLASH_BASE + (int)FLASHPAGE_SIZE * ((int)FLASHPAGE_NUMOF - 2)), raw_buf, strlen(raw_buf)); puts("wrote raw short buffer to last flash page"); From b8b8ffd16304c2501f7a78d5866b29ce37c48063 Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Fri, 21 Dec 2018 11:48:16 +0100 Subject: [PATCH 3/3] sam0 flashpage_write: correct assert for last byte of flash + style --- cpu/sam0_common/include/cpu_conf.h | 4 +++- cpu/sam0_common/periph/flashpage.c | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cpu/sam0_common/include/cpu_conf.h b/cpu/sam0_common/include/cpu_conf.h index 18894358de..449c941c9d 100644 --- a/cpu/sam0_common/include/cpu_conf.h +++ b/cpu/sam0_common/include/cpu_conf.h @@ -43,7 +43,9 @@ extern "C" { */ /* a flashpage in RIOT is mapped to a flash row on the SAM0s */ #define FLASHPAGE_SIZE (256U) -/* one SAM0 row contains 4 SAM0 pages, so 4 SAM0 pages contain the amount of a RIOT flashpage */ +/* one SAM0 row contains 4 SAM0 pages, so 4 SAM0 pages contain + * the amount of a RIOT flashpage + */ #define FLASHPAGE_PAGES_PER_ROW (4) /* number of RIOT flashpages on device */ #define FLASHPAGE_NUMOF (FLASH_NB_OF_PAGES / FLASHPAGE_PAGES_PER_ROW) diff --git a/cpu/sam0_common/periph/flashpage.c b/cpu/sam0_common/periph/flashpage.c index c52cbd2d30..eb192b732f 100644 --- a/cpu/sam0_common/periph/flashpage.c +++ b/cpu/sam0_common/periph/flashpage.c @@ -68,7 +68,7 @@ void flashpage_write_raw(void *target_addr, const void *data, size_t len) ((unsigned)data % FLASHPAGE_RAW_ALIGNMENT))); /* ensure the length doesn't exceed the actual flash size */ - assert(((unsigned)target_addr + len) < + assert(((unsigned)target_addr + len) <= (CPU_FLASH_BASE + (FLASHPAGE_SIZE * FLASHPAGE_NUMOF))); uint32_t *dst = (uint32_t *)target_addr; @@ -111,7 +111,9 @@ void flashpage_write(int page, const void *data) * The erasing is done once as a full row is always reased. */ for (unsigned curpage = 0; curpage < FLASHPAGE_PAGES_PER_ROW; curpage++) { - flashpage_write_raw(page_addr + (curpage * NVMCTRL_PAGE_SIZE / 4), (void *) ((uint32_t *) data + (curpage * NVMCTRL_PAGE_SIZE / 4)), NVMCTRL_PAGE_SIZE); + flashpage_write_raw(page_addr + (curpage * NVMCTRL_PAGE_SIZE / 4), + (void *) ((uint32_t *) data + (curpage * NVMCTRL_PAGE_SIZE / 4)), + NVMCTRL_PAGE_SIZE); } }