Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 21ccb0f

Browse files
committedFeb 7, 2021
Write to EEPROM with any byte size/alignment
1 parent 1dc5e51 commit 21ccb0f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed
 

‎libraries/EEPROM/src/EEPROM.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ class EEPROMClass : public FlashIAPBlockDevice {
4646
FlashIAPBlockDevice::read((uint8_t*)scratch, 0, _cfg.sram_bytes); // keep all of flash in sram in case we need to erase
4747

4848
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
49-
memcpy(scratch + idx/4, data, size);
49+
50+
if(idx % 4 == 0)
51+
{
52+
memcpy(scratch + idx/4, data, size); //We have byte alignment
53+
}
54+
else
55+
{
56+
//Move data to byte alignment
57+
uint8_t alignedData[size + (idx % 4)];
58+
memcpy(alignedData + (idx % 4), data, size + (idx % 4)); //Shift data
59+
60+
memcpy(scratch + (idx / 4), alignedData, size + (idx % 4)); //Paint aligned data back onto scratch
61+
}
5062

5163
erase();
5264
int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size);

0 commit comments

Comments
 (0)
Please sign in to comment.