So, the other day I implemented an option to save settings persistent in Flash. I used the ASF Framework and wrote at the userPage.
Now I want to store a bit more data (it doesn't fit into the userpage anymore, so I want to split the data into pieces and to maintain a certain order and aesthetics, I want to write each chunk beginning at the start of one page; Instead of emulating a cross-page-border write). Thats the theory so far.
I use the following code:
void test_writeNormalFlashPage(uint32_t*data, uint16_t wordcount, uint32_t destinationAddr)
uint32_t *psrc, *pdest ;
uint8_t timeout = 0;
psrc = (void*) (&(data));
pdest = (void*) destinationAddr ;
*pdest++ = *psrc++ ;
destinationAddr -= 0x80000000;
while(!flashc_erase_page(destinationAddr/64,true) && timeout < 250)
if(timeout >= 250)
void test_readNormalPageFlash(uint32_t* result, uint16_t wordcount, uint32_t fAddress)
uint32_t* source = (void*) fAddress;
uint32_t* desti = result;
*desti++ = *source++;
The Datasheet states the following:
Writing to an address A in the flash memory map will not update the
flash memory, but will instead update location A%64 in the page buffer. The PAGEN field in the
Flash Command (FCMD) register will at the same time be updated with the value A/64.
A page buffer writes a whole page, so if modulo 64 is used, the page size must be 64. PAGEN stores the index of the page, since it is 64 in size, the address is generated by dividing by 64. But if I choose a destination address 0x80001E00 (so 0x1E00 / 0x40 = 0x78) and issue a flash-write command, my data ends up at address 0x8000F000!
Why is my calculation incorrect? Or is something else fishy here?
All informations were gathered with the Atmel Studio debugger.
update: the "destinationAddr -= 0x80000000;" is obsolete, since the bits that would make the value too high are masked by hardware.