Skip to content

Commit

Permalink
[bugfix] STM32G4 flash erase dual bank BKER bit
Browse files Browse the repository at this point in the history
  • Loading branch information
hshose committed Mar 20, 2024
1 parent 3a44b5a commit 7eabed0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/modm/platform/flash/stm32/flash.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ Flash::erase(uint8_t index)
((index << FLASH_CR_SNB_Pos) & FLASH_CR_SNB_Msk);
%% else
%% if family in ["g0","g4"]
%% if dual_bank
%% if family == "g0"
const bool index_is_on_second_bank{index >= 256};
%% else
const bool index_is_on_second_bank{index >= (Size/2048/2)};
%% endif
const uint8_t page = index_is_on_second_bank ? index - Size/2048/2 : index;
FLASH->CR = FLASH_CR_STRT | FLASH_CR_PER |
((index_is_on_second_bank << FLASH_CR_BKER_Pos) & FLASH_CR_BKER_Msk) |
((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;
Expand Down
15 changes: 13 additions & 2 deletions src/modm/platform/flash/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ def prepare(module, options):

module.depends(":cmsis:device", ":platform:rcc",
":architecture:register")

if device.identifier.family in ["g4"]:
module.add_option(
EnumerationOption(
name="dual_bank",
description="Flash is configured in single or dual bank mode",
enumeration=["yes", "no"],
default="yes"))
return True

def build(env):
target = env[":target"].identifier
memories = listify(env[":target"].get_driver("core")["memory"])
flash = next(filter(lambda m: m["name"] == "flash", memories))

dual_bank = False
family = target.family
if target.family in ["f4"]:
block_shift = 17
Expand All @@ -47,10 +55,12 @@ def build(env):
block_shift = 11
ftype = "page"
busy_bit = "FLASH_SR_BSY1"
dual_bank = int(flash["size"]) > 128*1024
elif target.family in ["g4"]:
block_shift = 11
ftype = "page"
busy_bit = "FLASH_SR_BSY"
dual_bank = ( env["modm:platform:flash:dual_bank"] == "yes")

env.substitutions = {
"start": int(flash["start"], 16),
Expand All @@ -59,7 +69,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")
Expand Down

0 comments on commit 7eabed0

Please sign in to comment.