Questions about saving data to flash
I am working with an STM32D103C8T6 and I want to store some data on the flash memory.
I have some idea on how to do It but not sure about some details.
My procedure is the following:
1- Add a new section in linker at a defined address near the end.
I understand this is done so main program never overrides my data. If not please correct me
ex:
.permament 0x800EA60 :
{
KEEP(*(.permament))
}>FLASH
2- Unlock flash and option bytes with
FLASH_CR and FLASH_KEYR registers.
3- Clear page inside the area I destined for my permanent data
4- Write my data in 16 bits packages
eg:
*(__IO uint16_t*)(addr) = data;
I have the following questions:
Regarding step 1:
Is this necessary? Does this guarantee I won't overwrite the main program or is it any other way to do it?
Regarding step 2:
Do I need to unlock option bytes or is this ?
Regarding step 3:
Where does a page start? Do I need to specify the addres in FLAS_AR register to the begining of a page?
Regarding step 4:
I know I cannot write 1s but can I write ceros anytime?
For example, can I do the following?:
Let's say I have 4 bytes with all 1s, I then write in the first byte a 3.
Some time later I write in the second byte a 5.
Later I change second first byte to 1.
Can I do this or do I need to clear the page after each write?
