Skip to content

Commit 196d685

Browse files
author
Nathan Seidle
committed
Rewrite EEPROM block write to use one array instead of two (less SRAM!). Update comments.
Now works up to 2096 bytes of flash based EEPROM.
1 parent 3829af2 commit 196d685

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Page erase takes 15ms
2424
Writing a byte takes 19ms
2525
Writing a float across two words takes 19ms
26-
Update (no write) takes 1ms
26+
Update (no write) takes ~1ms
2727
2828
Development environment specifics:
2929
Arduino IDE 1.8.x
@@ -62,9 +62,10 @@ void EEPROMClass::erase()
6262
}
6363

6464
//1) Make copy of current flash contents into SRAM
65-
//2) Record user data into SRAM. Check if new data is different from flash.
66-
//3) Erase flash page (8k)
67-
//4) Write SRAM back into flash
65+
//2) Record user data into SRAM.
66+
//3) Check if new data is different from flash.
67+
//4) Erase flash page (8k)
68+
//5) Write SRAM back into flash
6869
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
6970
{
7071
//Error check
@@ -73,8 +74,14 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
7374
blockSize = AP3_FLASH_EEPROM_SIZE - eepromLocation;
7475
}
7576

76-
//First we have to read the contents of current "EEPROM" to SRAM, one byte at a time
77-
uint8_t eepromContents[AP3_FLASH_EEPROM_SIZE];
77+
//Read the contents of current "EEPROM" to SRAM
78+
//Flash is written in 32-bit words but user passes in array of bytes
79+
//Create an array of 32-bit words but reference it a byte at a time
80+
uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4];
81+
82+
//We can't read 32bits at a time because the way flash is oriented (little endian)
83+
//So we read a byte at a time
84+
uint8_t *eepromContents = (uint8_t *)flashContent;
7885
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
7986
{
8087
eepromContents[x] = *(uint8_t *)(AP3_FLASH_EEPROM_START + x);
@@ -86,8 +93,8 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
8693
eepromContents[eepromLocation + x] = dataToWrite[x];
8794
}
8895

89-
//Only update flash with new data.
90-
//Run a check here to see if the new data is the same as what's in flash. If it's the same, don't erase flash.
96+
//Run a check here to see if the new data is the same as what's in flash. If it's the same,
97+
//just return, don't erase flash.
9198
bool theSame = true;
9299
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
93100
{
@@ -105,19 +112,6 @@ void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uin
105112
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START + eepromLocation),
106113
AM_HAL_FLASH_ADDR2PAGE(AP3_FLASH_EEPROM_START + eepromLocation));
107114

108-
//Flash is written in 32-bit words so we split the byte array into 4 byte chunks
109-
uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4];
110-
uint16_t spot = 0;
111-
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x += 4)
112-
{
113-
flashContent[spot] = (uint32_t)eepromContents[x + 3] << (8 * 3);
114-
flashContent[spot] |= (uint32_t)eepromContents[x + 2] << (8 * 2);
115-
flashContent[spot] |= (uint32_t)eepromContents[x + 1] << (8 * 1);
116-
flashContent[spot] |= (uint32_t)eepromContents[x + 0] << (8 * 0);
117-
118-
spot++;
119-
}
120-
121115
//Then we write the contents of the array back
122116
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
123117
flashContent,

libraries/EEPROM/src/EEPROM.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
at 0xFE000;
2222
2323
Page erase takes 15ms
24-
Writing a byte takes 30ms
25-
Writing a float across two words takes 30ms
26-
Update (no write) takes 1ms
24+
Writing a byte takes 19ms
25+
Writing a float across two words takes 19ms
26+
Update (no write) takes ~1ms
2727
2828
Development environment specifics:
2929
Arduino IDE 1.8.x
@@ -66,7 +66,9 @@ Error : EEPROM start address must be divisble by 8192
6666

6767
//By limiting EEPROM size to 1024 bytes, we reduce the amount of SRAM required and
6868
//time needed to read/write words into flash. It can be increased
69-
//to 8096 if needed
69+
//to 2048 if needed
70+
//1024 = 19ms update time
71+
//2048 = 23ms update time
7072
const int AP3_FLASH_EEPROM_SIZE = 1024; //In bytes
7173

7274
uint8_t read(uint16_t eepromLocation);

0 commit comments

Comments
 (0)