Self-flashing STM32G030F
Posted: Wed Apr 06, 2022 11:48 am
HI, I'm saving a few parameters in flash (in this case normally only updated once when being installed in the field).
Then I encountered this Heisenbug. With debugging enabled it worked, when disabled it didn't. Turns out its that one delay that does the trick... (the debug code is if (debug) {chprintf (...); chThdsleepmilliseconds (30); /* allow the debug info to flush*/ } )
Does anybody know what I'm doing wrong? According my interpretation of the datasheet, I think that waiting for the flash to be not-busy should be enough to continue with "erase done, clear the PER bit"...
The symptom is that the store of the parameters results in "0" in the flash. I'm guessing that it would do this when not properly erased.
(I'm using 64 bits of flash memory, while in fact I need 2-3 bits for one parameter (stored as 32 bits) and about 10 bits for the other (also stored as 32bits). So "option bytes" would also be possible for this one application, but this whole structure is copied from other projects, so way less hassle to do it like this).
Then I encountered this Heisenbug. With debugging enabled it worked, when disabled it didn't. Turns out its that one delay that does the trick... (the debug code is if (debug) {chprintf (...); chThdsleepmilliseconds (30); /* allow the debug info to flush*/ } )
Code: Select all
#define flash_wait_nb() while ( FLASH->SR & FLASH_SR_BSY1)
...
// we've determined that an erase is required:
flash_wait_nb ();
FLASH->CR = FLASH_CR_PER | (pagenr << 3) ;
FLASH->CR |= FLASH_CR_STRT;
flash_wait_nb ();
chThdSleepMilliseconds (30); // XXX This delay makes it work!
FLASH->CR &= ~FLASH_CR_PER;
Does anybody know what I'm doing wrong? According my interpretation of the datasheet, I think that waiting for the flash to be not-busy should be enough to continue with "erase done, clear the PER bit"...
The symptom is that the store of the parameters results in "0" in the flash. I'm guessing that it would do this when not properly erased.
(I'm using 64 bits of flash memory, while in fact I need 2-3 bits for one parameter (stored as 32 bits) and about 10 bits for the other (also stored as 32bits). So "option bytes" would also be possible for this one application, but this whole structure is copied from other projects, so way less hassle to do it like this).