Skip to content

Commit faa9632

Browse files
author
Nathan Seidle
committed
Block updates close to working.
1 parent 0a4b707 commit faa9632

File tree

3 files changed

+90
-16
lines changed

3 files changed

+90
-16
lines changed

libraries/EEPROM/examples/Example2_AllFunctions/Example2_AllFunctions.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ void setup()
5353

5454
Serial.printf("Write byte time: %dms\n", endTime - startTime);
5555

56+
startTime = millis();
57+
EEPROM.write(randomLocation, myValue1); //(location, data)
58+
endTime = millis();
59+
60+
Serial.printf("Write identical byte to same location (should be 0): %dms\n", endTime - startTime);
61+
5662
byte response1 = EEPROM.read(randomLocation);
5763
byte response2 = EEPROM.read(randomLocation + 1);
5864
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue1, response1);
@@ -160,8 +166,9 @@ void setup()
160166
Serial.println("String test");
161167

162168
//String write test
163-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
164-
String myString = "How are you today?";
169+
dont do string.Do char *array with phone #
170+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
171+
String myString = "How are you today?";
165172
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
166173
EEPROM.put(randomLocation, myString);
167174

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,72 @@ void writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
153153
AP3_FLASH_EEPROM_SIZE);
154154
}
155155

156-
//ap3_EEPROM EEPROM;
156+
void EEPROMClass::writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize)
157+
{
158+
//Error check
159+
if (eepromLocation + blockSize >= AP3_FLASH_EEPROM_SIZE)
160+
{
161+
blockSize = AP3_FLASH_EEPROM_SIZE - eepromLocation;
162+
}
163+
164+
//First we have to read the contents of current "EEPROM" to SRAM, one byte at a time
165+
uint8_t eepromContents[AP3_FLASH_EEPROM_SIZE];
166+
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
167+
{
168+
eepromContents[x] = *(uint8_t *)(AP3_FLASH_EEPROM_START + x);
169+
}
170+
171+
//Write the caller's new data into the byte array
172+
for (int x = 0; x < blockSize; x++)
173+
{
174+
eepromContents[eepromLocation + x] = dataToWrite[x];
175+
}
176+
177+
//Only update flash with new data.
178+
//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.
179+
bool theSame = true;
180+
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x++)
181+
{
182+
if (eepromContents[x] != *(uint8_t *)(AP3_FLASH_EEPROM_START + x))
183+
{
184+
theSame = false;
185+
break;
186+
}
187+
}
188+
if (theSame == true)
189+
return;
190+
191+
//Then we erase an 8K page
192+
am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
193+
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START + eepromLocation),
194+
AM_HAL_FLASH_ADDR2PAGE(AP3_FLASH_EEPROM_START + eepromLocation));
195+
196+
//Flash is written in 32bit words so we split the byte array into 4 byte chunks
197+
uint32_t flashContent[AP3_FLASH_EEPROM_SIZE / 4];
198+
uint16_t spot = 0;
199+
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x += 4)
200+
{
201+
flashContent[spot] = (uint32_t)eepromContents[x + 3] << (8 * 3);
202+
flashContent[spot] |= (uint32_t)eepromContents[x + 2] << (8 * 2);
203+
flashContent[spot] |= (uint32_t)eepromContents[x + 1] << (8 * 1);
204+
flashContent[spot] |= (uint32_t)eepromContents[x + 0] << (8 * 0);
205+
206+
spot++;
207+
}
208+
209+
// Serial.println("");
210+
// Serial.print("EEPROM Contents:");
211+
// for (uint16_t x = 0; x < 32; x++)
212+
// {
213+
// if (x % 8 == 0)
214+
// Serial.println();
215+
// Serial.printf("0x%08X ", eepromContentWords[x]);
216+
// }
217+
// Serial.println();
218+
219+
// //Then we write the contents of the array back
220+
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
221+
flashContent,
222+
(uint32_t *)AP3_FLASH_EEPROM_START,
223+
AP3_FLASH_EEPROM_SIZE / 4);
224+
}

libraries/EEPROM/src/EEPROM.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
Error : EEPROM start address must be divisble by 8192
6565
#endif
6666

67-
//By limiting EEPROM size to 1024, we reduce the amount of SRAM required and
67+
//By limiting EEPROM size to 1024 bytes, we reduce the amount of SRAM required and
6868
//time needed to mask in individual bytes and words into flash. It can be increased
6969
//to 8096 if needed
70-
#define AP3_FLASH_EEPROM_SIZE 1024
70+
#define AP3_FLASH_EEPROM_SIZE 1024 //In bytes
7171

7272
uint8_t
7373
read(uint16_t eepromLocation);
@@ -167,11 +167,16 @@ struct EEPROMClass
167167
void write(int idx, uint8_t val) { (EERef(idx)) = val; }
168168
void update(int idx, uint8_t val) { EERef(idx).update(val); }
169169
void erase();
170+
void writeBlockToEEPROM(uint16_t eepromLocation, const uint8_t *dataToWrite, uint16_t blockSize);
170171

171172
//STL and C++11 iteration capability.
172-
EEPtr begin() { return 0x00; }
173+
EEPtr
174+
begin()
175+
{
176+
return 0x00;
177+
}
173178
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
174-
uint16_t length() { return AP3_FLASH_EEPROM_SIZE + 1; }
179+
uint16_t length() { return AP3_FLASH_EEPROM_SIZE; }
175180

176181
//Functionality to 'get' and 'put' objects to and from EEPROM.
177182
template <typename T>
@@ -185,18 +190,12 @@ struct EEPROMClass
185190
}
186191

187192
template <typename T>
188-
const T &put(int idx, const T &t)
193+
const T &put(int idx, const T &t) //Address, data
189194
{
190195
const uint8_t *ptr = (const uint8_t *)&t;
191196

192-
//TODO - Write Artemis compatible function for block write
193-
//#ifdef __arm__
194-
// eeprom_write_block(ptr, (void *)idx, sizeof(T));
195-
//#else
196-
EEPtr e = idx;
197-
for (int count = sizeof(T); count; --count, ++e)
198-
(*e).update(*ptr++);
199-
//#endif
197+
writeBlockToEEPROM(idx, ptr, sizeof(T)); //Address, data, sizeOfData
198+
200199
return t;
201200
}
202201
};

0 commit comments

Comments
 (0)