Skip to content

Commit

Permalink
[flash] STM32G4 flash erase dual bank BKER bit
Browse files Browse the repository at this point in the history
Co-authored-by: Niklas Hauser <niklas.hauser@rwth-aachen.de>
  • Loading branch information
salkinium committed Mar 30, 2024
1 parent 98a99ce commit 088a1e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/modm/platform/flash/stm32/flash.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 12 additions & 8 deletions src/modm/platform/flash/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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")
Expand Down

0 comments on commit 088a1e0

Please sign in to comment.