diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 31c98b08a0..f86459b0c2 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -136,6 +136,10 @@ ifneq (,$(filter pcf857%,$(USEMODULE))) USEMODULE += pcf857x endif +ifneq (,$(filter periph_flashpage_aux,$(FEATURES_USED))) + FEATURES_REQUIRED += periph_flashpage_pagewise +endif + ifneq (,$(filter periph_ptp_timer periph_ptp_speed_adjustment,$(FEATURES_USED))) FEATURES_REQUIRED += periph_ptp endif diff --git a/drivers/include/mtd_flashpage.h b/drivers/include/mtd_flashpage.h index 5c2db79efb..b8a14b41ea 100644 --- a/drivers/include/mtd_flashpage.h +++ b/drivers/include/mtd_flashpage.h @@ -35,7 +35,7 @@ extern "C" #endif /** - * @brief Macro helper to initialize a mtd_t with flash-age driver + * @brief Macro helper to initialize a mtd_t with flashpage driver */ #define MTD_FLASHPAGE_INIT_VAL(_pages_per_sector) { \ .base = { \ @@ -47,6 +47,23 @@ extern "C" }, \ } +/** + * @brief Macro helper to initialize a mtd_t with a portion of the flash + * + * @param[in] start Start address of the flash section + * @param[in] len Size of the flash section in bytes + */ +#define MTD_FLASHPAGE_AUX_INIT_VAL(start, len) { \ + .base = { \ + .driver = &mtd_flashpage_driver, \ + .sector_count = len / FLASHPAGE_SIZE, \ + .pages_per_sector = 1, \ + .page_size = FLASHPAGE_SIZE, \ + .write_size = 1, \ + }, \ + .offset = start / FLASHPAGE_SIZE, \ +} + /** * @brief Flashpage MTD device operations table */ @@ -62,6 +79,40 @@ typedef struct { flash */ } mtd_flashpage_t; +#if CONFIG_SLOT_AUX_LEN || DOXYGEN +/** + * @brief MTD device representing the auxiliary flash slot + */ +extern mtd_flashpage_t mtd_flash_aux_slot; + +/** + * @brief Generic MTD device backed by the auxiliary flash slot + */ +extern mtd_dev_t *mtd_aux; +#endif + +/** + * @brief Size of the auxiliary slot on the internal flash + * Must align with the flash page size. + * + * @note Don't set this config value directly! + * Instead set `SLOT_AUX_LEN` in the board's `Makefile.include` + * + * This value is fixed and can not be changed in the field / via firmware + * updates. Changing this value requires re-flashing of riotboot. + * + */ +#ifndef CONFIG_SLOT_AUX_LEN +#define CONFIG_SLOT_AUX_LEN 0 +#endif + +/** + * @brief Default MTD offset for the AUX slot + */ +#ifndef CONFIG_SLOT_AUX_MTD_OFFSET +#define CONFIG_SLOT_AUX_MTD_OFFSET 1 +#endif + #ifdef __cplusplus } #endif diff --git a/drivers/mtd_flashpage/mtd_flashpage.c b/drivers/mtd_flashpage/mtd_flashpage.c index f7bf1efd31..4e2b72f41b 100644 --- a/drivers/mtd_flashpage/mtd_flashpage.c +++ b/drivers/mtd_flashpage/mtd_flashpage.c @@ -159,3 +159,13 @@ const mtd_desc_t mtd_flashpage_driver = { .write_page = _write_page, .erase_sector = _erase_sector, }; + +#if CONFIG_SLOT_AUX_LEN +mtd_flashpage_t mtd_flash_aux_slot = MTD_FLASHPAGE_AUX_INIT_VAL(CONFIG_SLOT_AUX_OFFSET, + CONFIG_SLOT_AUX_LEN); +MTD_XFA_ADD(mtd_flash_aux_slot, CONFIG_SLOT_AUX_MTD_OFFSET); +mtd_dev_t *mtd_aux = &mtd_flash_aux_slot.base; + +static_assert(CONFIG_SLOT_AUX_OFFSET % FLASHPAGE_SIZE == 0, "AUX slot must align with page"); +static_assert(CONFIG_SLOT_AUX_LEN % FLASHPAGE_SIZE == 0, "AUX slot must align with page"); +#endif