Skip to content

stm32u5: skip programming quad-words that are erased and stay erased - #944

Merged
danielinux merged 2 commits into
wolfSSL:masterfrom
LinuxJedi:stm32u5-skip-erased-qwords
Oct 16, 2026
Merged

danielinux merged 2 commits into
wolfSSL:masterfrom
LinuxJedi:stm32u5-skip-erased-qwords

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

On STM32U5, programming takes about 120 us per 16-byte quad-word at 160 MHz, whatever the data. On an STM32U585 an 8 KB page takes 61.6 ms to program and 0.87 ms to erase. The PKCS#11 store (src/pkcs11_store.c) commits whole 8 KB sectors, each twice (the backup sector, then the destination), and those sectors are mostly erased. Most of the commit time therefore goes into programming 0xFF.

hal_flash_write() now skips a quad-word when both the new data and the current flash content are all 0xFF. The flash ends up the same, and the quad-word stays erased, so it can still be programmed later.

Testing on an STM32U585 board (TZEN=1, stm32u5-wolfcrypt-tz.config):

  • An 8 KB page that is half erased, with every fifth quad-word of the other half also erased, programs in 25.4 ms instead of 61.6 ms. It reads back as written, with no error flags in FLASH_NSSR. The skipped quad-words can be programmed afterwards (2 KB aligned, 3 unaligned bytes, and a single quad-word between programmed ones), and they read back correctly.
  • PKCS#11 store with the pinned wolfPKCS11, through the NSC calls: creating 8 token objects (certificates, EC and AES keys) drops from 26.6 s to 3.4 s, and destroying four of them from 22.1 s to 2.7 s. The objects reload and verify after reboot. These numbers need stm32u5: accept secure alias addresses in hal_flash_write/erase #943, without which the keyvault is never written on STM32U5; the change itself is independent of it.
  • With Write only changed token objects to storage wolfPKCS11#257 as well (it stops rewriting every object on each create), creating the 8 objects takes 0.81 s in total.

Not covered: the update swap path. With this config I could not get a staged update applied on unmodified master either, so I had no baseline to compare against.

Programming takes about 120 us per 16-byte unit at 160 MHz, so writing
an 8 KB sector takes about 62 ms whatever it holds. The PKCS#11 store
commits whole sectors, twice each with the backup sector, and they are
mostly erased.

Skip a unit when both the new data and the flash are all 0xFF. The flash
content is the same, and the unit can still be programmed later.
Copilot AI balanced review requested due to automatic review settings October 16, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new branch lacks automated regression coverage, and firmware-update swap behavior remains unvalidated.

1 open finding
What changed in this PR

Optimizes STM32U5 flash writes to reduce time spent programming mostly erased sectors, including PKCS#11 storage commits.

Changes:

  • Skips 16-byte quad-words when both requested data and existing flash are all 0xFF.
  • Advances past skipped units without enabling flash programming.
File Description
hal/​stm32u5.c Adds the erased-quad-word skip to hal_flash_write().

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread hal/stm32u5.c
Readback cannot tell a skipped unit from one programmed with 0xFF, so
the mock hal_flash_wait_complete() now counts program operations
(waits with FLASH_CR_PG set). New tests check that erased data over
erased flash is not programmed (aligned and unaligned), that only the
units with data are programmed when erased and data units are mixed,
and that units are still programmed for data inside an erased unit,
for 0xFF over flash that is not erased, and for 0xFF in a unit that
holds data outside the request. The three skip tests fail on the HAL
without the skip.
@danielinux
danielinux merged commit 7f9f960 into wolfSSL:master Oct 16, 2026
490 of 491 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants