Repository navigation
fix(stm32): a flash unit cut by a power loss no longer stops every boot - #365
Merged
Merged
Conversation
[Medium impact] The PR appears safe to merge; no actionable defects were found.
|
On H7 a flash word whose program or erase was cut by a reset or a power loss fails its ECC check, and reading it raises a bus error. STM32Flash read through the plain copy of the base class, so the first read of such a word ended in the fault handler, on every boot. STM32Flash::Read now reads flash word by flash word. For each word it masks all interrupts (FAULTMASK), sets CCR.BFHFNMIGN so that the bus error raises no exception, and tests DBECCERR afterwards. A word with the flag set is filled with 0 in the buffer, and Read returns ErrorCode::OK. With the bus error ignored the load does not deliver the contents of the word but what earlier loads left behind; right after a read of blank flash that is mostly 0xFFFFFFFF, which a reader takes for erased flash. Zeros are neither erased flash nor complete data, so DatabaseRaw and DatabaseRawSequential handle the word as the half-written unit it is, with their existing power-loss logic. A double error caused by real damage reads the same and is not told apart. The comparison before programming in Write reads the same way and returns ErrorCode::CHECK_ERR without programming when the target word cannot be read; the word can be written again after an erase. The ECC flags stay set once raised, and the HAL takes a flag that is still set for a failed program or erase: after one read of a word with a corrected single error, the next HAL_FLASH_Program returned HAL_ERROR without programming. The driver clears SNECCERR and DBECCERR after each flash word it reads and before each erase or program. Other families keep the plain copy.
PowerCutFlash gains a third mode that models ECC Flash such as H7: the cut unit, or the whole page of a cut erase, cannot be read afterwards. Read fills it with 0 and returns OK as STM32Flash does, programming it returns CHECK_ERR, and a completed erase makes it readable again. RunPowerCutCases runs every cut in the three modes. DatabaseRaw and DatabaseRawSequential pass the new mode without a source change: no cut leads to a program of an unreadable unit, and none to a second program of a written unit.
Jiu-xiao
force-pushed
the
fix/h7-flash-ecc-read
branch
from
November 16, 2026 01:25
9618628 to
e71a670
Compare
…n NMI On G0, G4, L4, L5, WB, WL, U0, U5, WBA, H5 and U3 a Flash unit whose program or erase was cut by a power loss raises an NMI when it is read. The NMI cannot be masked, and the NMI_Handler CubeMX generates is an endless loop, so a database that reads such a unit at start-up stops there on every boot. libxr_stm32_flash_ecc_nmi() (stm32_flash_nmi.h, callable from C) clears the double error flag and the single error flag and returns true when the double error flag was set; NMI_Handler calls it in its user code section and returns on true. The interrupted read then goes on and delivers the half-written contents of the unit, which DatabaseRaw and DatabaseRawSequential handle with their existing power-loss logic. The single error flag is cleared as well because a later double error raises no NMI while it is set. On H5 the instruction cache is invalidated, as its reference manual asks. Three register layouts are recognised by their CMSIS names (one ECCR, with ECCD2/ECCC2 and ECC2R where they exist; H5; U3); with any other device header the function returns false. Measured on an STM32G431: one double word is busy for 85.9 us. A cut between about 17 us and 41.9 us leaves the low 36 bits written and the high 28 bits blank; about half of these cells raise the NMI, the others only set the single error flag. With the handler returning, the load delivers that content and never reads as blank. The written flag of DatabaseRaw was either unwritten or complete in 96 of 96 cuts. A page erase cut at 10 % of its time or later reads blank without a flag.
…s covered The H7R/S HAL offers byte, half-word, word, double-word and quad-word programming. STM32Flash took the smallest type a HAL defines, so on H7R/S MIN_WRITE_SIZE was 1 and every byte was a forced write of the whole 128-bit flash word with its ECC; a second byte into the same flash word writes a word that is not blank, which RM0477 says can leave an inconsistent ECC. Write also sized its buffer as MIN_WRITE_SIZE / 4 words, zero here, and copied one byte into it. When a HAL defines both the byte and the quad-word type, the driver now uses the quad-word: MIN_WRITE_SIZE is 16 and the program type is FLASH_TYPEPROGRAM_QUADWORD. Only the H7R/S HAL defines both. H7R/S reports a double ECC error of the user flash with a bus error like H7 (RM0477), but its flags are SNECCERRF and DBECCERRF in FLASH_ISR, cleared through FLASH_ICR, so the H7 path did not recognise it and Read stayed the plain copy. LIBXR_STM32_FLASH_ECC_BUS_ERROR is now also defined for these names, and the two flag helpers use FLASH_ISR and FLASH_ICR there. Read, the comparison in Write and the read of one flash word are the same code as on H7. Compiled against the H7S3, H7R3, H7S7 and H7R7 headers; not run on hardware.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On STM32 families whose flash has ECC, a unit whose program or erase was cut by a reset or a power loss fails its ECC check. Reading it raises a bus error on H7 and H7R/S and an NMI on G0, G4, L4, L5, WB, WL, U0, U5, WBA, H5 and U3.
DatabaseRawreads the cut unit on every start, so the firmware ended in a fault handler on every boot until the flash was erased from outside.This pull request makes the read survive on both groups. The two databases then take the unit for the half-written unit it is and recover with their existing power-loss logic; their sources and the
Flashinterface are not changed.STM32Flash::Readignores the bus error for one flash word and tests the double error flaglibxr_stm32_flash_ecc_nmi(), called fromNMI_Handler, clears the flags and returnsH7 and H7R/S
What a cut leaves
Measured on an STM32H723 board (MC02) with a bare-metal bench that starts a program or an erase from RAM and issues a system reset after a chosen delay:
DBECCERRset (299 of 458 cuts in the sweep); earlier cuts read as blank, later ones as the complete data.0xFFFFFFFFin 52 of 52 cut units. A reader that looks only at the data takes the unit for erased flash.Change (
STM32Flash, selected byFLASH_SR_DBECCERRorFLASH_ISR_DBECCERRF, andSCB_CCR_BFHFNMIGN_Msk)Readis overridden and reads flash word by flash word. For each word it masks all interrupts (FAULTMASK), setsCCR.BFHFNMIGNso that the bus error raises no exception, and testsDBECCERRafterwards. A word with the flag set is filled with 0 in the buffer.ReadreturnsErrorCode::OK, as before.Writereads the same way and returnsErrorCode::CHECK_ERRwithout programming when the target word cannot be read. The word can be written again after an erase.SNECCERRandDBECCERRare cleared after each flash word that is read and before each erase or program. This is a separate defect that the hardware test exposed and that dev has without this change: the flags stay set once raised, and the H7 HAL takes a flag that is still set for a failed operation. After one read of a word with a corrected single error, the nextHAL_FLASH_ProgramreturnedHAL_ERROR(pFlash.ErrorCode=0x02000000) without programming, and the database stopped inREQUIRE.DBECCERRFandSNECCERRFinFLASH_ISR, cleared throughFLASH_ICR. Its flash word is 128 bits with one ECC code (RM0477). Its HAL does not test the ECC flags after an operation, so the clearing changes nothing for it.STM32Flashtook the smallest type a HAL defines.MIN_WRITE_SIZEwas therefore 1, every byte was a forced write of the whole 128-bit flash word with its ECC (a second byte into the same flash word writes a word that is not blank, which RM0477 says can leave an inconsistent ECC), andWritecopied one byte into a buffer ofMIN_WRITE_SIZE / 4= 0 words. When a HAL defines both the byte and the quad-word type, the driver now uses the quad-word:MIN_WRITE_SIZEis 16 and the program type isFLASH_TYPEPROGRAM_QUADWORD. Of the 19 ST HALs checked only H7R/S defines both, so no other family changes.Zeros are neither erased flash nor complete data.
DatabaseRawalready treats such content as a write that a power loss cut: non-erased bytes after the chain lead to a recycle, a flag that is not erased reads as written, and a block header or checksum that does not match makes the block invalid.DatabaseRawSequentialdoes the same through its header and checksum.NMI families
What a cut leaves
Measured on an STM32G431 with the same kind of bench; its
NMI_HandlerclearsECCDandECCCand returns:ECCC. A cut from 43.3 us on reads as the complete data.DatabaseRaw(all ones, low nibble of the last byte cleared) lies in the second half. In 96 cuts, 75 of them between 40.25 us and 44.75 us in steps of 0.375 us, the cell was either unwritten (no ECC flag, accepts the same program again in 26 of 26) or complete; no intermediate state.PROGERR(9 of 9); programming all zeros over it is accepted and reads back as zeros without a flag (8 of 8).So on G4 the reader needs no signal from the driver and no zero fill: what it reads is neither blank nor complete.
Change
driver/st/stm32_flash_nmi.hand.c:bool libxr_stm32_flash_ecc_nmi(void), callable from C. When the double error flag is set it clears that flag and the single error flag and returnstrue; otherwise it returnsfalse. The single error flag is cleared as well because a later double error raises no NMI while it is set (RM0351). On H5 the instruction cache is invalidated, as RM0481 asks.ECCR(G0, G4, L4, L5, WB, WL, U0, U5, WBA; withECCD2/ECCC2andECC2Rwhere they exist), H5 (ECCDETR/ECCCORR), U3 (ECCDR/ECCCR). With any other device header the function returnsfalse.Nothing changes for a project until its
NMI_Handlerhas this line. Other NMI causes keep the existing handling.Tests
PowerCutFlashhas a third mode that models the H7 case: the cut unit, or the whole page of a cut erase, cannot be read afterwards;Readfills it with 0 and returnsOK, programming it returnsCHECK_ERR, and a completed erase makes it readable again.RunPowerCutCasesruns every cut in the three modes. The G4 case, half-written content, is the existing second mode. Both databases pass all three without a source change.Behaviour changes
STM32Flash::Readon H7 and H7R/S, word with an ECC double errorOK, the word reads as 0; all interrupts are masked for the copy of one flash wordSTM32Flash::Writeon H7 and H7R/S, target word with an ECC double errorCHECK_ERR, nothing is programmedSTM32Flasherase / program on H7 after a read that set an ECC flagFAILEDDatabaseRaw::InitandDatabaseRawSequential::Initon H7 and H7R/S, unit cut by a power lossSTM32Flash::MIN_WRITE_SIZEand program type on H7R/SDatabaseRaw<STM32Flash::MIN_WRITE_SIZE>gets a different layout thereNMI_Handlercalls the new functionNMI_HandlerNMI_HandlerCost:
stm32_flash.ogrows from 880 to 1128 bytes of text on STM32H723 and from 872 to 1136 on STM32H7S3 (-Os); in the bench (64 MHz, D-cache on) the read of one 32-byte word takes 119 cycles against 47 for a plain copy. The NMI function is 28 bytes on G4 and 60 on H5.Verification
test_linux_canskipped, as on dev).driver/st/stm32_flash.cppcompiles without a diagnostic against the HAL headers of STM32H723, H743, H745 (CM7) and H7B3 (GNU Arm 14.2.1), and against ST's HAL and CMSIS headers of STM32H7S3, H7R3, H7S7 and H7R7, where theReadoverride is present. A complete STM32H723 firmware (FreeRTOS, CubeMX project of the MC02 BSP) builds with this branch.driver/st/stm32_flash_nmi.ccompiles with-Wall -Wextra -Werroragainst 18 CMSIS device headers: G0B1, G071, G431, G474, L476, L4R5, L552, U575, WB55, WL55, U083, WBA52, U385, H563 and H503 take a branch that touches the expected registers; F103, F407 and H723 returnfalse.STM32FlashandDatabaseRaw<32>, independent watchdog reset after a pseudo-random time, the database has to hold the acknowledged updates or one more), made with the first revision of this branch, in whichReadreturnedCHECK_ERRandDatabaseRawaccepted it duringInit; the read sequence and the flag clearing are the same now, the runs were not repeated. Run 1, before the ECC flag clearing: 45 cuts pass, then the start after cut 46 stops inREQUIREwithFLASH_SR1=0x02000000. Run 2, with the flag clearing: 100 of 100 cuts pass, 1540 updates; a cut left an ECC-error unit for the database to read in one of the 101 starts.STM32FlashandDatabaseRaw<8>on 16 pages,NMI_Handlerwith the line above, independent watchdog reset after 0.25 ms to 50 ms): 50 of 50 cuts pass, 265 updates, 51 starts all complete. The function handled 11 NMIs in 6 starts, all while the test's own log was read throughSTM32Flash::Read; the database met none.Not covered
NMI_Handler; none is changed here.SR2/CCR2) and the H743, H745 and H7B3 builds are compiled only. The STM32 build matrix was not run.DatabaseRawstays in the main block until the next recycle and is zero-filled on every read until then.ECCCsuppressing a later NMI was not tested.Part of xrobot-org/XRobot-Dev-Roadmap#34