Maybe I'm misunderstanding your input but this specific line only ensures that I erase every page before I start writing into it. 0x59000 \ 512 == flash page 712 (712* 512 == flash address location 0x59000) .
if (FlashBufferHead % 512 == 0)
My 'record buffer' starts at the beginning of a page but is multiple pages in size.
I do not want to write the records for an entire page at once because this means I need to buffer them elsewhere untill I have enough to fit.
I want to avoid creating another buffer to store records into before writing them into flash.
From my understanding the only requirement for writing bytes into flash is that we need to write at least 32 bytes at a time.
And for reading we need to read at least 16 bytes at a time.
We can't only erase a page during initialisation. We need to be able to erase\overwrite older records over time.
In my code I request to write 100 bytes at a time.
So what I expect that happens is ( confirmed this in debug
--run0
Flash page 0x59000 is erased (because FlashBufferHead point to the beginning of a page).
100 bytes in written. 0x59000
FlashBufferHead gets value 128.
--run1
100 bytes in written. 0x59080
But here we get the issue that when I read the values back from flash.
These are different as mentioned in the topic start. We did already erase this part of the flash during run 0.
FlashBufferHead gets value 256.
--run2
100 bytes in written. 0x59100
FlashBufferHead gets value 384.
--run3
100 bytes in written. 0x59180
FlashBufferHead gets value 512..
--run4
Flash page 0x59200 is erased (because FlashBufferHead locates to the next page)
100 bytes in written. 0x59200
FlashBufferHead gets value 640..