&~2047 means 'round down to the nearest 2048' - you need 2048 because that's the page size. What it's doing is setting the bottom 11 bits of the number to 0.
So, (x+2047)&~2047 rounds up to the nearest 2048.
The EEPROM emulation really isn't that bad. Think of it this way:
have one block of 2048 bytes
split it into 4 byte records (we have to write 4 bytes at once)
In each record, the first (top) byte can be a number that's the 'address', and the bottom 3 bytes is data
To write, you just write a new record on the end of the block (any unwritten value will be all 1s, so the value 0xFFFFFFFF)
To read, go through the whole block from the end backwards, looking for the record with the 'address' you want. If the same address was written more than once it doesn't matter, because we're only looking for the last one
When you write and the block is full, run through the block working out all the current values, erase it, and write them back in.
I just had a go at doing this - just use the code below. At some point (if it works ok) I'll have a go at packing it into a module:
var f =require("STM32F1Flash");
f.unlock();// unlock the memory
var baseAddr =(process.memory().flash_binary_end+2047)&~2047;
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
&~2047
means 'round down to the nearest 2048' - you need 2048 because that's the page size. What it's doing is setting the bottom 11 bits of the number to 0.So,
(x+2047)&~2047
rounds up to the nearest 2048.The EEPROM emulation really isn't that bad. Think of it this way:
0xFFFFFFFF
)I just had a go at doing this - just use the code below. At some point (if it works ok) I'll have a go at packing it into a module: