From 6ff163ded6ec56642f32321b466fff3acb0ae954 Mon Sep 17 00:00:00 2001 From: Henrik Hose Date: Wed, 20 Mar 2024 20:53:39 +0100 Subject: [PATCH] [bugfix] STM32G4 flash erase dual bank BKER bit --- examples/nucleo_g474re/json/main.cpp | 18 ++++++++++++------ examples/nucleo_g474re/json/project.xml | 1 + src/modm/platform/flash/stm32/flash.cpp.in | 12 ++++++++++++ src/modm/platform/flash/stm32/module.lb | 15 +++++++++++++-- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/examples/nucleo_g474re/json/main.cpp b/examples/nucleo_g474re/json/main.cpp index 8f243dac58..272eca2815 100644 --- a/examples/nucleo_g474re/json/main.cpp +++ b/examples/nucleo_g474re/json/main.cpp @@ -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 @@ -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 @@ -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 @@ -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(); @@ -184,6 +189,7 @@ main() MODM_LOG_INFO.flush(); } - while (1); + while (1) + ; return 0; } \ No newline at end of file diff --git a/examples/nucleo_g474re/json/project.xml b/examples/nucleo_g474re/json/project.xml index 197d632b70..abbc58a448 100644 --- a/examples/nucleo_g474re/json/project.xml +++ b/examples/nucleo_g474re/json/project.xml @@ -3,6 +3,7 @@ + modm:debug diff --git a/src/modm/platform/flash/stm32/flash.cpp.in b/src/modm/platform/flash/stm32/flash.cpp.in index 1cacf2e493..b7084df210 100644 --- a/src/modm/platform/flash/stm32/flash.cpp.in +++ b/src/modm/platform/flash/stm32/flash.cpp.in @@ -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; diff --git a/src/modm/platform/flash/stm32/module.lb b/src/modm/platform/flash/stm32/module.lb index 8cc3db884e..54f8c381f3 100644 --- a/src/modm/platform/flash/stm32/module.lb +++ b/src/modm/platform/flash/stm32/module.lb @@ -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 @@ -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), @@ -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")