2024-09-28 06:01 PM - edited 2024-09-28 06:02 PM
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?
Solved! Go to Solution.
2024-10-21 08:08 PM
I will create a response which answers all the questions based on what I ended up doing:
1) I created a new memory area
MEMORY
{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 63K
STORAGE_FLASH (rx) : ORIGIN = ORIGIN(FLASH) + LENGTH(FLASH), LENGTH = 1K
}
and assigned a new section inside it for memory initialization:
.permanent :
{
. = ALIGN(4);
KEEP(*(.permanent))
. = ALIGN(4);
} >STORAGE_FLASH
And I created a new symbol to track where to write:
__flashStorageBegin = ORIGIN(STORAGE_FLASH);
2) No need to unlock option bytes
3) Page start is aligned with "FLASH" area, since page is 1kB long for this MCU, all pages are a multiple of 1024 bytes from FLASH origin (0x8000000+1024*n)
In the FLASH_AR register you should only write a page start address.
4)You can write the same half-word multilple times without erasing the page mulitple time as long as you don't write 1s.
Extra information from other question I did:
Since you have to write to half-words, you should only write to even addresses.