Skip to content

Commit af4e137

Browse files
committed
Block write works. Updated examples.
Write speed decreased from 30ms to 19ms due to block writing instead of multi-byte writes. Works with arbitrary length variables such as char arrays.
1 parent d1753ea commit af4e137

File tree

3 files changed

+17
-99
lines changed

3 files changed

+17
-99
lines changed

libraries/EEPROM/examples/Example2_AllFunctions/Example2_AllFunctions.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,38 @@ void setup()
149149
Serial.printf("Size of double: %d\n", sizeof(double));
150150
double myValue11 = -290.3485723409857;
151151
double myValue12 = 384.95734987;
152+
double myValue13 = 917.14159;
152153
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
153154

155+
startTime = millis();
154156
EEPROM.put(randomLocation, myValue11);
157+
endTime = millis();
158+
Serial.printf("Time to record 64-bits: %dms\n", endTime - startTime);
159+
155160
EEPROM.put(randomLocation + 8, myValue12);
161+
EEPROM.put(EEPROM.length() - sizeof(myValue13), myValue13); //Test end of EEPROM space
156162

157163
double response11;
158164
double response12;
165+
double response13;
159166
EEPROM.get(randomLocation, response11);
160167
EEPROM.get(randomLocation + 8, response12);
168+
EEPROM.get(EEPROM.length() - sizeof(myValue13), response13);
161169
Serial.printf("Location %d should be %lf: %lf\n", randomLocation, myValue11, response11);
162170
Serial.printf("Location %d should be %lf: %lf\n", randomLocation + 8, myValue12, response12);
171+
Serial.printf("Edge of EEPROM %d should be %lf: %lf\n", EEPROM.length() - sizeof(myValue13), myValue13, response13);
163172
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
164173

165174
Serial.println("");
166175
Serial.println("String test");
167176

168177
//String write test
169-
dont do string.Do char *array with phone #
170-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
171-
String myString = "How are you today?";
178+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
179+
char myString[19] = "How are you today?";
172180
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
173181
EEPROM.put(randomLocation, myString);
174182

175-
String readMy;
183+
char readMy[19];
176184
EEPROM.get(randomLocation, readMy);
177185
Serial.printf("Location %d string should read 'How are you today?': ", randomLocation);
178186
Serial.println(readMy);

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 5 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
Flash is 0x00 to 0xFF000. EEPROM writes will start at 0xFF000 - 8192 = 0xF2000.
2222
2323
Page erase takes 15ms
24-
Writing a byte takes 30ms
25-
Writing a float across two words takes 30ms
24+
Writing a byte takes 19ms
25+
Writing a float across two words takes 19ms
2626
Update (no write) takes 1ms
2727
2828
Development environment specifics:
@@ -40,11 +40,7 @@
4040
#include "EEPROM.h"
4141
#include "Arduino.h"
4242

43-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
44-
4543
//Write a byte to a given "EEPROM" location
46-
//Automatically masks user's byte into flash without
47-
//affecting other bytes in this flash word
4844
void write(uint16_t eepromLocation, uint8_t dataToWrite)
4945
{
5046
EEPROM.writeBlockToEEPROM(eepromLocation, &dataToWrite, 1);
@@ -57,17 +53,6 @@ uint8_t read(uint16_t eepromLocation)
5753
return (*(uint8_t *)flashLocation);
5854
}
5955

60-
//Write a new byte to a given location in "EEROM" only if new data is there
61-
void update(uint16_t eepromLocation, uint8_t dataToWrite)
62-
{
63-
if (read(eepromLocation) != dataToWrite)
64-
{
65-
write(eepromLocation, dataToWrite);
66-
}
67-
}
68-
69-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
70-
7156
//Erase 8k page encapsulating the EEPROM section
7257
void EEPROMClass::erase()
7358
{
@@ -76,82 +61,10 @@ void EEPROMClass::erase()
7661
AM_HAL_FLASH_ADDR2PAGE(AP3_FLASH_EEPROM_START));
7762
}
7863

79-
//This is the main helper function
80-
//Reprogram a given location with 32-bits
81-
//Flash is written to in words at locations that are %4=0
82-
//Span words if necessary
8364
//1) Make copy of current flash contents into SRAM
84-
//2) Turn user's requested spot into 0xFFs
65+
//2) Record user data into SRAM. Check if new data is different from flash.
8566
//3) Erase flash page (8k)
8667
//4) Write SRAM back into flash
87-
//5) Write user's data onto the spot with recently created 0xFFs
88-
//Note - this code assumes EEPROM temp space is contained in one page
89-
void writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
90-
{
91-
//Error check
92-
if (flashLocation >= AP3_FLASH_EEPROM_START + AP3_FLASH_EEPROM_SIZE)
93-
{
94-
return;
95-
}
96-
if (flashLocation < AP3_FLASH_EEPROM_START)
97-
{
98-
return;
99-
}
100-
101-
//Check to see if location needs updating
102-
if (*(uint32_t *)(flashLocation) == dataToWrite)
103-
{
104-
return;
105-
}
106-
107-
//First we have to read the contents of current "EEPROM" to SRAM
108-
uint32_t tempContents[AP3_FLASH_EEPROM_SIZE / 4];
109-
uint16_t spot = 0;
110-
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x += 4)
111-
{
112-
tempContents[spot++] = *(uint32_t *)(AP3_FLASH_EEPROM_START + x);
113-
}
114-
115-
//Then we erase an 8K page
116-
am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
117-
AM_HAL_FLASH_ADDR2INST(flashLocation),
118-
AM_HAL_FLASH_ADDR2PAGE(flashLocation));
119-
120-
//Zero out this word(s)
121-
uint8_t byteOffset = (flashLocation % 4);
122-
uint16_t wordLocation = (flashLocation - AP3_FLASH_EEPROM_START) / 4;
123-
124-
//Mask in the new data into the array
125-
if (byteOffset == 0)
126-
{
127-
//Easy - update this word with new word
128-
tempContents[wordLocation] = dataToWrite;
129-
}
130-
else
131-
{
132-
//Clear the upper bytes of the first word to 0s
133-
tempContents[wordLocation] &= ~(0xFFFFFFFF << (byteOffset * 8));
134-
135-
//Clear the lower bytes of the second word to 0s
136-
tempContents[wordLocation + 1] &= ~(0xFFFFFFFF >> ((4 - byteOffset) * 8));
137-
138-
//OR in upper bytes of this word with new data
139-
uint32_t dataToWriteFirstWord = dataToWrite << (byteOffset * 8);
140-
141-
//OR in the lower bytes of the following word with new data
142-
uint32_t dataToWriteSecondWord = dataToWrite >> ((4 - byteOffset) * 8);
143-
144-
tempContents[wordLocation] |= dataToWriteFirstWord;
145-
tempContents[wordLocation + 1] |= dataToWriteSecondWord;
146-
}
147-
148-
//Then we write the contents of the array back
149-
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
150-
tempContents,
151-
(uint32_t *)AP3_FLASH_EEPROM_START,
152-
AP3_FLASH_EEPROM_SIZE);
153-
}
154-
15568
void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
15669
{
15770
//Error check
@@ -168,7 +81,7 @@ void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dat
16881
}
16982

17083
//Write the caller's new data into the byte array
171-
for (int x = 0; x < blockSize; x++)
84+
for (uint16_t x = 0; x < blockSize; x++)
17285
{
17386
eepromContents[eepromLocation + x] = dataToWrite[x];
17487
}
@@ -192,7 +105,7 @@ void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dat
192105
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START + eepromLocation),
193106
AM_HAL_FLASH_ADDR2PAGE(AP3_FLASH_EEPROM_START + eepromLocation));
194107

195-
//Flash is written in 32bit words so we split the byte array into 4 byte chunks
108+
//Flash is written in 32-bit words so we split the byte array into 4 byte chunks
196109
uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4];
197110
uint16_t spot = 0;
198111
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x += 4)

libraries/EEPROM/src/EEPROM.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ Error : EEPROM start address must be divisble by 8192
7272
uint8_t
7373
read(uint16_t eepromLocation);
7474
void write(uint16_t eepromLocation, uint8_t dataToWrite);
75-
void update(uint16_t eepromLocation, uint8_t dataToWrite);
76-
void erase(); //Erase entire EEPROM
77-
void writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite);
7875

7976
struct EERef
8077
{

0 commit comments

Comments
 (0)