From 7309c9cfb04c5982eb7c8b90e1ca8cf5cfb34e7f Mon Sep 17 00:00:00 2001 From: Ollrogge Date: Wed, 13 Oct 2021 22:25:54 +0200 Subject: [PATCH] periph/flashpage: extend API --- drivers/include/periph/flashpage.h | 10 ++++++++++ drivers/periph_common/flashpage.c | 17 +++++++++++++++++ tests/periph_flashpage/main.c | 19 +++++++++++-------- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/drivers/include/periph/flashpage.h b/drivers/include/periph/flashpage.h index c7cc85bca345c..ce78273f0370a 100644 --- a/drivers/include/periph/flashpage.h +++ b/drivers/include/periph/flashpage.h @@ -211,6 +211,16 @@ unsigned flashpage_page(const void *addr); */ void flashpage_erase(unsigned page); +/** + * @brief Get number of first free flashpage + */ +unsigned flashpage_first_free(void); + +/** + * @brief Get number of last free flashpage + */ +unsigned flashpage_last_free(void); + /** * @brief Write the given page with the given data * diff --git a/drivers/periph_common/flashpage.c b/drivers/periph_common/flashpage.c index 05252e1f6e3e8..675b30c21a1a0 100644 --- a/drivers/periph_common/flashpage.c +++ b/drivers/periph_common/flashpage.c @@ -22,6 +22,13 @@ #include "cpu.h" #include "assert.h" +/** + * @brief Memory markers, defined in the linker script + * @{ + */ +extern uint32_t _end_fw; +extern uint32_t _erom; + /* guard this file, must be done before including periph/flashpage.h * TODO: remove as soon as periph drivers can be build selectively */ #if defined(FLASHPAGE_SIZE) || defined(PERIPH_FLASHPAGE_CUSTOM_PAGESIZES) @@ -129,3 +136,13 @@ unsigned flashpage_page(const void *addr) return page - 1; } #endif /* PERIPH_FLASHPAGE_NEEDS_FLASHPAGE_PAGE */ + +unsigned flashpage_first_free(void) +{ + return flashpage_page(&_end_fw) + 1; +} + +unsigned flashpage_last_free(void) +{ + return flashpage_page(&_erom) - 1; +} diff --git a/tests/periph_flashpage/main.c b/tests/periph_flashpage/main.c index 324cb168d0c00..c992fccb1ecd1 100644 --- a/tests/periph_flashpage/main.c +++ b/tests/periph_flashpage/main.c @@ -115,24 +115,27 @@ static int cmd_info(int argc, char **argv) (void)argc; (void)argv; - printf("Flash start addr:\t0x%08x\n", (int)CPU_FLASH_BASE); + printf("Flash start addr:\t\t0x%08x\n", (int)CPU_FLASH_BASE); #ifdef FLASHPAGE_SIZE - printf("Page size:\t\t%i\n", (int)FLASHPAGE_SIZE); + printf("Page size:\t\t\t%i\n", (int)FLASHPAGE_SIZE); #else - puts("Page size:\t\tvariable"); + puts("Page size:\t\t\tvariable"); #endif - printf("Number of pages:\t%i\n", (int)FLASHPAGE_NUMOF); + printf("Number of pages:\t\t%i\n", (int)FLASHPAGE_NUMOF); #ifdef FLASHPAGE_RWWEE_NUMOF - printf("RWWEE Flash start addr:\t0x%08x\n", (int)CPU_FLASH_RWWEE_BASE); - printf("RWWEE Number of pages:\t%i\n", (int)FLASHPAGE_RWWEE_NUMOF); + printf("RWWEE Flash start addr:\t\t0x%08x\n", (int)CPU_FLASH_RWWEE_BASE); + printf("RWWEE Number of pages:\t\t%i\n", (int)FLASHPAGE_RWWEE_NUMOF); #endif #ifdef NVMCTRL_USER - printf("AUX page size:\t%i\n", FLASH_USER_PAGE_AUX_SIZE + sizeof(nvm_user_page_t)); - printf(" user area:\t%i\n", FLASH_USER_PAGE_AUX_SIZE); + printf("AUX page size:\t\t%i\n", FLASH_USER_PAGE_AUX_SIZE + sizeof(nvm_user_page_t)); + printf(" user area:\t\t%i\n", FLASH_USER_PAGE_AUX_SIZE); #endif + printf("Number of first free page: \t%u \n", flashpage_first_free()); + printf("Number of last free page: \t%u \n", flashpage_last_free()); + return 0; }