Skip to content

stm32g0: Add flash bank swap support - #20060

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
ArrestedLightning:fix/stm32g0-bank-swap-api
Sep 5, 2026
Merged

stm32g0: Add flash bank swap support#20060
xiaoxiang781216 merged 1 commit into
apache:masterfrom
ArrestedLightning:fix/stm32g0-bank-swap-api

Conversation

@jsanchez-2g

Copy link
Copy Markdown
Contributor

Summary

Add STM32G0 flash APIs required to switch dual-bank memory mapping and reload
flash option bytes:

  • stm32_flash_swapbanks() toggles FLASH_OPTR_NSWAP_BANK
  • stm32_flash_optload() launches an option-byte reload
  • Bank swapping returns -EPERM when FLASH_SECR_BOOT_LOCK is active
  • The swap operation is a no-op on single-bank devices

Reloading option bytes after changing the bank mapping normally causes an
immediate system reset.

Validation

  • git diff --check: passed
  • tools/checkpatch.sh -g HEAD^..HEAD: passed
  • STM32G071 single-bank configuration with CONFIG_STM32_PROGMEM=y:
    • Flash driver compiled successfully
    • Both APIs exported
    • Single-bank no-op path compiled
  • STM32G0B1 dual-bank configuration with CONFIG_STM32_PROGMEM=y:
    • Flash driver compiled successfully
    • Both APIs exported
    • Dual-bank swap path compiled

Hardware validation

Tested the equivalent existing implementation on a 2G BMS Golgi STM32G0B1
target using two consecutive RS-485 firmware updates.

Observed option-byte states:

  1. Initial USB DFU image:
    • FLASH_OPTR=fef9feaa
    • nSWAP_BANK=1
  2. After the first firmware update:
    • FLASH_OPTR=fee9feaa
    • nSWAP_BANK=0
  3. After the second update, initiated while the banks were swapped:
    • FLASH_OPTR=fef9feaa
    • nSWAP_BANK=1

Both updates completed successfully. After each update, the target rebooted,
mounted its filesystem, initialized its communication interfaces and BMS
hardware, and resumed normal operation.

The hardware image used the product-tree implementation from which these APIs
were ported. The submitted code was rebased and compile-validated against
current Apache NuttX master.

Add APIs to toggle the dual-bank flash mapping and reload the option bytes. Reject bank swapping when BOOT_LOCK is enabled and leave the swap operation as a no-op on single-bank devices.

Assisted-by: OpenAI Codex <codex@openai.com>
Signed-off-by: jsanchez-2g <jsanchez@2g-eng.com>
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small labels Sep 4, 2026
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@xiaoxiang781216
xiaoxiang781216 merged commit 6b5673a into apache:master Sep 5, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants