diff --git a/cpu/sam0_common/include/cpu_conf.h b/cpu/sam0_common/include/cpu_conf.h index e954eb139a..48c1658bcb 100644 --- a/cpu/sam0_common/include/cpu_conf.h +++ b/cpu/sam0_common/include/cpu_conf.h @@ -42,9 +42,15 @@ extern "C" { * @{ */ /* a flashpage in RIOT is mapped to a flash row on the SAM0s */ -#define FLASHPAGE_SIZE (256U) +#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) +#define FLASHPAGE_NUMOF (FLASH_NB_OF_PAGES / 4) +/* The minimum block size which can be written is 16B. However, the erase + * block is always FLASHPAGE_SIZE (SAM0 row). + */ +#define FLASHPAGE_RAW_BLOCKSIZE (16) +/* Writing should be always 4 byte aligned */ +#define FLASHPAGE_RAW_ALIGNMENT (4) /** @} */ #ifdef __cplusplus diff --git a/cpu/sam0_common/periph/flashpage.c b/cpu/sam0_common/periph/flashpage.c index a9e72c69a0..ee499d6ba8 100644 --- a/cpu/sam0_common/periph/flashpage.c +++ b/cpu/sam0_common/periph/flashpage.c @@ -58,19 +58,18 @@ static void _lock(void) void flashpage_write_raw(void *target_addr, void *data, size_t len) { - /* sam[dlr]21 can only write whole flash pages. - * Mind the use of the ATMEL FLASH_PAGE_SIZE (64B) define vs. - * FLASHPAGE_SIZE (256B) as defined by RIOT. - * The actual minimal block size for writing is 16B, thus we - * assert we don't write less than that. + /* The actual minimal block size for writing is 16B, thus we + * assert we write on multiples and no less of that length. */ - assert(!(len % (FLASH_PAGE_SIZE / 4))); + assert(!(len % FLASHPAGE_RAW_BLOCKSIZE)); /* ensure 4 byte aligned writes */ - assert(!(((unsigned)target_addr & 0x3) || ((unsigned)data & 0x3))); + assert(!(((unsigned)target_addr % FLASHPAGE_RAW_ALIGNMENT) || + ((unsigned)data % FLASHPAGE_RAW_ALIGNMENT))); /* ensure the length doesn't exceed the actual flash size */ - assert((target_addr + len) < (CPU_FLASH_BASE + (FLASHPAGE_SIZE * FLASHPAGE_NUMOF))); + assert(((uint8_t*)target_addr + len) < + (CPU_FLASH_BASE + (FLASHPAGE_SIZE * FLASHPAGE_NUMOF))); uint32_t *dst = (uint32_t *)target_addr; uint32_t *data_addr = (uint32_t *)data;