From 71becdc4035d01bf07b3d8043884fa8815d55942 Mon Sep 17 00:00:00 2001 From: Henrik Hose Date: Sat, 30 Mar 2024 16:12:40 +0100 Subject: [PATCH] [flash] STM32G4 flash erase dual bank BKER bit Co-authored-by: Niklas Hauser --- src/modm/platform/flash/stm32/flash.cpp.in | 14 +++++++++++++- src/modm/platform/flash/stm32/module.lb | 20 ++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/modm/platform/flash/stm32/flash.cpp.in b/src/modm/platform/flash/stm32/flash.cpp.in index 1cacf2e493..98192cae7f 100644 --- a/src/modm/platform/flash/stm32/flash.cpp.in +++ b/src/modm/platform/flash/stm32/flash.cpp.in @@ -91,9 +91,21 @@ Flash::erase(uint8_t index) FLASH->CR = FLASH_CR_STRT | FLASH_CR_SER | uint32_t(size) | ((index << FLASH_CR_SNB_Pos) & FLASH_CR_SNB_Msk); %% else -%% if family in ["g0","g4"] +%% if family in ["g0", "g4"] +%% if dual_bank + uint32_t cr = FLASH_CR_STRT | FLASH_CR_PER; + uint32_t page = index; + if (FLASH->OPTR & {{dual_bank}} and index >= (Size/2 >> 11)) + { + // second bank index starts back at zero again + page -= (Size/2 >> 11); + cr |= FLASH_CR_BKER; + } + FLASH->CR = cr | ((page << FLASH_CR_PNB_Pos) & FLASH_CR_PNB_Msk); +%% else FLASH->CR = FLASH_CR_STRT | FLASH_CR_PER | ((index << FLASH_CR_PNB_Pos) & FLASH_CR_PNB_Msk); +%% endif %% else FLASH->CR &= ~FLASH_CR_STRT; FLASH->CR |= FLASH_CR_PER; diff --git a/src/modm/platform/flash/stm32/module.lb b/src/modm/platform/flash/stm32/module.lb index 8cc3db884e..400f14b6ca 100644 --- a/src/modm/platform/flash/stm32/module.lb +++ b/src/modm/platform/flash/stm32/module.lb @@ -31,26 +31,29 @@ def build(env): memories = listify(env[":target"].get_driver("core")["memory"]) flash = next(filter(lambda m: m["name"] == "flash", memories)) + dual_bank = False family = target.family + busy_bit = "FLASH_SR_BSY" + ftype = "page" if target.family in ["f4"]: block_shift = 17 ftype = "sector" - busy_bit = "FLASH_SR_BSY" elif target.family in ["f1"]: if target.name in ["05", "07"] or int(flash["size"]) >= 262144: block_shift = 11 else: block_shift = 10 - ftype = "page" - busy_bit = "FLASH_SR_BSY" elif target.family in ["g0"]: - block_shift = 11 - ftype = "page" busy_bit = "FLASH_SR_BSY1" + block_shift = 11 + if target.name[0] in ["b", "c"]: + dual_bank = "FLASH_OPTR_DUAL_BANK" elif target.family in ["g4"]: block_shift = 11 - ftype = "page" - busy_bit = "FLASH_SR_BSY" + # Only Cat3 devices have dual bank, so g47x or g48x + if target.name[0] in ["7", "8"]: + dual_bank = "FLASH_OPTR_DBANK" + block_shift = "(FLASH->OPTR & FLASH_OPTR_DBANK ? 11 : 12)" env.substitutions = { "start": int(flash["start"], 16), @@ -59,7 +62,8 @@ def build(env): "shift": block_shift, "has_sectors": ftype == "sector", "busy_bit": busy_bit, - "family": family + "family": family, + "dual_bank": dual_bank } env.outbasepath = "modm/src/modm/platform/flash" env.template("flash.hpp.in")