From f85594eb55d7d190e066e0fc6572b8b88631e7d0 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 9 Nov 2020 16:44:06 +0100 Subject: [PATCH] kinetis: Adapt to flashpage/flashpage_pagewise API --- cpu/kinetis/Kconfig | 4 +- cpu/kinetis/Makefile.features | 2 +- cpu/kinetis/include/cpu_conf_kinetis_k.h | 8 ++-- cpu/kinetis/include/cpu_conf_kinetis_w.h | 6 +-- cpu/kinetis/periph/flashpage.c | 53 ++++++++++-------------- 5 files changed, 33 insertions(+), 40 deletions(-) diff --git a/cpu/kinetis/Kconfig b/cpu/kinetis/Kconfig index 6cd7ddef75..b42e2b16e8 100644 --- a/cpu/kinetis/Kconfig +++ b/cpu/kinetis/Kconfig @@ -21,7 +21,7 @@ config CPU_FAM_K bool select CPU_COMMON_KINETIS select HAS_PERIPH_FLASHPAGE - select HAS_PERIPH_FLASHPAGE_RAW + select HAS_PERIPH_FLASHPAGE_PAGEWISE select HAS_PERIPH_MCG config CPU_FAM_L @@ -33,7 +33,7 @@ config CPU_FAM_W bool select CPU_COMMON_KINETIS select HAS_PERIPH_FLASHPAGE - select HAS_PERIPH_FLASHPAGE_RAW + select HAS_PERIPH_FLASHPAGE_PAGEWISE select HAS_PERIPH_MCG ## CPU Models diff --git a/cpu/kinetis/Makefile.features b/cpu/kinetis/Makefile.features index 8be2f95809..33484b682f 100644 --- a/cpu/kinetis/Makefile.features +++ b/cpu/kinetis/Makefile.features @@ -21,7 +21,7 @@ include $(LAST_MAKEFILEDIR)/kinetis-info.mk ifneq (,$(filter k w,$(CPU_FAM))) FEATURES_PROVIDED += periph_flashpage - FEATURES_PROVIDED += periph_flashpage_raw + FEATURES_PROVIDED += periph_flashpage_pagewise endif ifeq (ea,$(CPU_FAM)) diff --git a/cpu/kinetis/include/cpu_conf_kinetis_k.h b/cpu/kinetis/include/cpu_conf_kinetis_k.h index af124b1a73..b91bafff76 100644 --- a/cpu/kinetis/include/cpu_conf_kinetis_k.h +++ b/cpu/kinetis/include/cpu_conf_kinetis_k.h @@ -146,12 +146,12 @@ /* The minimum block size which can be written is 8B (Phrase). However, the * erase block is always FLASHPAGE_SIZE. */ -#define FLASHPAGE_RAW_PHRASE (8U) -#define FLASHPAGE_RAW_BLOCKSIZE FLASHPAGE_RAW_PHRASE +#define FLASHPAGE_BLOCK_PHRASE (8U) +#define FLASHPAGE_WRITE_BLOCK_SIZE FLASHPAGE_BLOCK_PHRASE /* Writing should be always 8 bytes aligned */ -#define FLASHPAGE_RAW_ALIGNMENT FLASHPAGE_RAW_PHRASE +#define FLASHPAGE_WRITE_BLOCK_ALIGNMENT FLASHPAGE_BLOCK_PHRASE /* Section erase and programming must be 16 bytes aligned */ -#define FLASHPAGE_RAW_SECTION_ALIGNMENT (16U) +#define FLASHPAGE_BLOCK_SECTION_ALIGNMENT (16U) /** @} */ #ifdef __cplusplus diff --git a/cpu/kinetis/include/cpu_conf_kinetis_w.h b/cpu/kinetis/include/cpu_conf_kinetis_w.h index 7cd06bfc84..89fec6c092 100644 --- a/cpu/kinetis/include/cpu_conf_kinetis_w.h +++ b/cpu/kinetis/include/cpu_conf_kinetis_w.h @@ -87,11 +87,11 @@ /* The minimum block size which can be written is 4B. However, the erase * block is always FLASHPAGE_SIZE. */ -#define FLASHPAGE_RAW_BLOCKSIZE (4U) +#define FLASHPAGE_WRITE_BLOCK_SIZE (4U) /* Writing should be always 4 bytes aligned */ -#define FLASHPAGE_RAW_ALIGNMENT (4U) +#define FLASHPAGE_WRITE_BLOCK_ALIGNMENT (4U) /* Section erase and programming must be 8 bytes aligned */ -#define FLASHPAGE_RAW_SECTION_ALIGNMENT (8U) +#define FLASHPAGE_BLOCK_SECTION_ALIGNMENT (8U) /** @} */ #ifdef __cplusplus diff --git a/cpu/kinetis/periph/flashpage.c b/cpu/kinetis/periph/flashpage.c index 193b963299..cd3d0e0680 100644 --- a/cpu/kinetis/periph/flashpage.c +++ b/cpu/kinetis/periph/flashpage.c @@ -132,12 +132,12 @@ static int _check_errors(void) static int _verify_erased(uint32_t address, size_t len) { /* Ensures verifies are aligned */ - assert(!(address % FLASHPAGE_RAW_SECTION_ALIGNMENT)); + assert(!(address % FLASHPAGE_BLOCK_SECTION_ALIGNMENT)); - /* We verify FLASHPAGE_RAW_SECTION_ALIGNMENT Bytes at a time, we - round up before adjusting to FLASHPAGE_RAW_SECTION_ALIGNMENT */ - uint32_t num = (len + (FLASHPAGE_RAW_SECTION_ALIGNMENT - 1)) / - FLASHPAGE_RAW_SECTION_ALIGNMENT; + /* We verify FLASHPAGE_BLOCK_SECTION_ALIGNMENT Bytes at a time, we + round up before adjusting to FLASHPAGE_BLOCK_SECTION_ALIGNMENT */ + uint32_t num = (len + (FLASHPAGE_BLOCK_SECTION_ALIGNMENT - 1)) / + FLASHPAGE_BLOCK_SECTION_ALIGNMENT; /* Setup command and run */ FCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_VERIFY_SECTION, address); @@ -150,7 +150,7 @@ static int _verify_erased(uint32_t address, size_t len) static int _sector_erase(uint32_t address) { /* Ensures erases are aligned */ - assert(!(address % FLASHPAGE_RAW_SECTION_ALIGNMENT)); + assert(!(address % FLASHPAGE_BLOCK_SECTION_ALIGNMENT)); /* Setup command and run */ FCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_ERASE_SECTOR, address); @@ -163,7 +163,7 @@ static int _flash_write(uint32_t address, uint32_t *data) { /* Setup command and run */ FCCOBx[1] = *data++; - if (FLASHPAGE_RAW_BLOCKSIZE == 8U) { + if (FLASHPAGE_WRITE_BLOCK_SIZE == 8U) { FCCOBx[2] = *data; FCCOBx[0] = BYTES_JOIN_TO_WORD_1_3(FTFx_PROGRAM_PHRASE, address); } @@ -175,21 +175,29 @@ static int _flash_write(uint32_t address, uint32_t *data) return _check_errors(); } -void flashpage_write_raw(void *target_addr, const void *data, size_t len) +void flashpage_erase(unsigned page) { - /* Assert multiples of FLASHPAGE_RAW_BLOCKSIZE are written and no less of + assert(page < (int)FLASHPAGE_NUMOF); + + /* ERASE sector */ + _sector_erase((uint32_t)flashpage_addr(page)); +} + +void flashpage_write(void *target_addr, const void *data, size_t len) +{ + /* Assert multiples of FLASHPAGE_WRITE_BLOCK_SIZE are written and no less of that length. */ - assert(!(len % FLASHPAGE_RAW_BLOCKSIZE)); + assert(!(len % FLASHPAGE_WRITE_BLOCK_SIZE)); /* Ensure writes are aligned */ - assert(!(((unsigned)target_addr % FLASHPAGE_RAW_ALIGNMENT) || - ((unsigned)data % FLASHPAGE_RAW_ALIGNMENT))); + assert(!(((unsigned)target_addr % FLASHPAGE_WRITE_BLOCK_ALIGNMENT) || + ((unsigned)data % FLASHPAGE_WRITE_BLOCK_ALIGNMENT))); /* Ensure the length doesn't exceed the actual flash size */ assert(((unsigned)target_addr + len) < (CPU_FLASH_BASE + (FLASHPAGE_SIZE * FLASHPAGE_NUMOF)) + 1); -#ifdef FLASHPAGE_RAW_PHRASE +#ifdef FLASHPAGE_BLOCK_PHRASE const uint64_t *data_addr = data; uint64_t *dst = target_addr; #else @@ -203,24 +211,9 @@ void flashpage_write_raw(void *target_addr, const void *data, size_t len) return; } - /* Write to flash FLASHPAGE_RAW_BLOCKSIZE bytes at a time */ - for (size_t i = 0; i < (len / FLASHPAGE_RAW_BLOCKSIZE); i++) { + /* Write to flash FLASHPAGE_WRITE_BLOCK_SIZE bytes at a time */ + for (size_t i = 0; i < (len / FLASHPAGE_WRITE_BLOCK_SIZE); i++) { _flash_write((uint32_t) dst++, (uint32_t *) data_addr++); } } - -void flashpage_write(int page, const void *data) -{ - assert(page < (int)FLASHPAGE_NUMOF); - - uint32_t *page_addr = flashpage_addr(page); - - /* ERASE sector */ - _sector_erase((uint32_t)page_addr); - - /* WRITE sector */ - if (data != NULL) { - flashpage_write_raw(page_addr, data, FLASHPAGE_SIZE); - } -}