Hi Lukas,
Thank you for your reply.
Actually, I have already written a piece of code (very similar to yours) for flash erase. It works fine when I use it on a simple program, but I need to use it under a Real Time Operating System, and when I do the execution jumps to an address where it should not go. It is weird because it also works fine when I use step mode (when I run it with the RTOS).
Do you know if there may be some side effects because of using an RTOS (such as interrupts or something)?
Here is my code for erasing a Low address space block, assuming the block has been enabled before.
Many thanks !
/* erase a block from low adress space
* block : 0..9 */
void eraseBlock (int block) {
/* start erase sequence */
FLASH.MCR.B.ERS = 1;
/* block selection for erase */
FLASH.LMS.B.LSEL = 0b0000000000 | (1 << block);
/* erase interlock write */
*(unsigned int *)(0x4000 * block) = 0xffffffff;
/* high voltage enable */
FLASH.MCR.B.EHV = 1;
/* wait for done and program/erase good */
while(!FLASH.MCR.B.DONE) {
}
while (!FLASH.MCR.B.PEG) {
}
/* high voltage disable */
FLASH.MCR.B.EHV = 0;
/* end of erase sequence */
FLASH.MCR.B.ERS = 0;
/* diselect block */
FLASH.LMS.B.LSEL = 0b0000000000;
}