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 6ff163d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
18 changes: 12 additions & 6 deletions examples/nucleo_g474re/json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ main()
MODM_LOG_INFO << "+++++++++++++++++++++++++++\n" << modm::endl;

// data::person alice_struct = {"Alice", 50};
data::person alice_struct = {"Alice", 1.85, 50};
data::person alice_struct = {"Alice", 1.85, 80};
json alice_json = alice_struct;

// basic JSON character string usage
Expand Down Expand Up @@ -112,7 +112,8 @@ main()
MODM_LOG_ERROR << "\nRequested flash end page exceeds flash [" << page_start << ", "
<< end_page << ")" << modm::endl;
MODM_LOG_INFO.flush();
while (1);
while (1)
;
}

// erase the pages before programming
Expand All @@ -122,14 +123,16 @@ main()
if (not Flash::unlock())
{
MODM_LOG_INFO << "Flash unlock failed!" << modm::endl;
while (1);
while (1)
;
}
for (size_t page{page_start}; page < end_page; page++) err |= Flash::erase(page);
if (err != 0)
{
MODM_LOG_ERROR << "\nThere was an error while erasing flash!" << modm::endl;
MODM_LOG_INFO.flush();
while (1);
while (1)
;
}

// pad the data with zeros to fit flash words
Expand All @@ -152,11 +155,13 @@ main()
memcpy(&outdata, &alice_binary[ii], sizeof(Flash::MaxWordType));
err |= Flash::program(flash_write_base_addr + ii, outdata);
}

if (err != 0)
{
MODM_LOG_ERROR << "\nThere was an error while programming flash!" << modm::endl;
MODM_LOG_INFO.flush();
while (1);
while (1)
;
}
MODM_LOG_INFO << "Writing complete! " << modm::endl;
MODM_LOG_INFO.flush();
Expand Down Expand Up @@ -184,6 +189,7 @@ main()
MODM_LOG_INFO.flush();
}

while (1);
while (1)
;
return 0;
}
1 change: 1 addition & 0 deletions examples/nucleo_g474re/json/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<options>
<option name="modm:build:build.path">../../../build/nucleo_g474re/json</option>
<option name="modm:platform:cortex-m:linkerscript.flash_reserved">1024*16</option>
<option name="modm:platform:flash:dual_bank">yes</option>
</options>
<modules>
<module>modm:debug</module>
Expand Down
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="no"))
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 6ff163d

Please sign in to comment.