cancel
Showing results for 
Search instead for 
Did you mean: 

Illegal Flash Memory writes possible?

hartmann
Associate
Posted on February 09, 2010 at 13:02

Illegal Flash Memory writes possible?

2 REPLIES 2
hartmann
Associate
Posted on May 17, 2011 at 13:39

so, finally I've got the bug 🙂 and it was in my code.

The problem is, that after programming the Programm bit in the FLASH_CR register is still set and even if the FPEC is deactivated by setting the LOCK bit int the FLASH_CR register all further write to the memory will still be possible. So you must clear the programming bit to protect from accidently write operations to the flash.

IMHO this is not exactly clear if you read the programming manual, further informations would be nice here.

best regards

thomas
Posted on May 17, 2011 at 13:39

Yes, the general rule of thumb is to bracket any changes you make to the register, so you leave it in the state you found it.

FLASH->KEYR = KEY1;

FLASH->KEYR = KEY2;

FLASH->CR |= CR_PG;

// Flash Stuff, write one word, multiple words, or the whole enchilada

FLASH->CR &= ~CR_PG;

FLASH->KEYR = 0;

The Locking/Unlocking control access to the registers, they do not enable/disable the flash controller. The idea being that if your code runs amok it is not going to enable the controller, set it up, and trash itself. Or at least make the probability of that occurring pretty small.

It is also possible to reprogram the entire flash memory while PG is enabled, it does not need to be toggled for every word written to memory, the library examples are not very efficient in this regard.

-Clive

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..