Page erase problem.
Posted: Sat Jan 03, 2015 4:53 pm
I want to store some "rarely changing" data in flash of my STM32F030R cpu.
Unlock:
and then write the data:
Simple! Works!
...... once: you need to erase the page if the page is not empty to begin with.
The "if" tests for the "erase needed". I expect the first instruction in "flash_wait_nb ()" to lock, waiting for the flash operation to finish, just like when programming the data the fetch of the next instruction will lock the CPU until it is finished. I also must confess that interrupts are enabled, so there might be interrupts that hang waiting for the flash operation to finish. So? Things should continue as soon as possible after the flash module finishes the program or erase.
I've added all sorts of "print" statements to debug this. The AR register does not seem to change by my assignment. That is expected, as it is write only.
I've checked a few places for example code, but everybody seems to be following this pattern and claims that it works...
Unlock:
Code: Select all
if (FLASH->CR & FLASH_CR_LOCK) {
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xcdef89ab;
}
Code: Select all
FLASH->CR |= FLASH_CR_PG;
for (i=0;i<len;i++)
((unsigned short *)FLASH_EE_REGION)[i] = ((unsigned short *)data)[i];
FLASH->CR |= FLASH_CR_LOCK;...... once: you need to erase the page if the page is not empty to begin with.
Code: Select all
if (i != len) {
flash_wait_nb ();
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = FLASH_EE_REGION;
FLASH->CR |= FLASH_CR_STRT;
flash_wait_nb ();
FLASH->CR &= ~FLASH_CR_PER;
}
I've added all sorts of "print" statements to debug this. The AR register does not seem to change by my assignment. That is expected, as it is write only.
I've checked a few places for example code, but everybody seems to be following this pattern and claims that it works...