Skip to content

Commit 56bf34a

Browse files
authored
Merge pull request #50 from sparkfun/FixEEPROM
Fix EEPROM to work with SDK v2.2
2 parents 410fe55 + 0d6a506 commit 56bf34a

File tree

2 files changed

+57
-66
lines changed

2 files changed

+57
-66
lines changed

libraries/EEPROM/src/EEPROM.cpp

+53-63
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ ap3_EEPROM::ap3_EEPROM()
5252
//affecting other bytes in this flash word
5353
void ap3_EEPROM::write(uint16_t eepromLocation, uint8_t dataToWrite)
5454
{
55-
uint32_t flashLocation = FLASH_EEPROM_START + eepromLocation;
55+
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
5656
writeWordToFlash(flashLocation, (uint32_t)dataToWrite | 0xFFFFFF00);
5757
}
5858

5959
//Read a byte from a given location in "EEPROM"
6060
uint8_t ap3_EEPROM::read(uint16_t eepromLocation)
6161
{
62-
uint32_t flashLocation = FLASH_EEPROM_START + eepromLocation;
62+
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
6363
return (*(uint8_t *)flashLocation);
6464
}
6565

@@ -70,39 +70,39 @@ uint8_t ap3_EEPROM::read(uint16_t eepromLocation)
7070

7171
void ap3_EEPROM::get(uint16_t eepromLocation, uint8_t &dataToGet)
7272
{
73-
dataToGet = *(uint8_t *)(FLASH_EEPROM_START + eepromLocation);
73+
dataToGet = *(uint8_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
7474
}
7575
void ap3_EEPROM::get(uint16_t eepromLocation, uint16_t &dataToGet)
7676
{
77-
dataToGet = *(uint16_t *)(FLASH_EEPROM_START + eepromLocation);
77+
dataToGet = *(uint16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
7878
}
7979
void ap3_EEPROM::get(uint16_t eepromLocation, int16_t &dataToGet)
8080
{
81-
dataToGet = *(int16_t *)(FLASH_EEPROM_START + eepromLocation);
81+
dataToGet = *(int16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
8282
}
8383
void ap3_EEPROM::get(uint16_t eepromLocation, int &dataToGet)
8484
{
85-
dataToGet = *(int *)(FLASH_EEPROM_START + eepromLocation);
85+
dataToGet = *(int *)(AP3_FLASH_EEPROM_START + eepromLocation);
8686
}
8787
void ap3_EEPROM::get(uint16_t eepromLocation, unsigned int &dataToGet)
8888
{
89-
dataToGet = *(unsigned int *)(FLASH_EEPROM_START + eepromLocation);
89+
dataToGet = *(unsigned int *)(AP3_FLASH_EEPROM_START + eepromLocation);
9090
}
9191
void ap3_EEPROM::get(uint16_t eepromLocation, int32_t &dataToGet)
9292
{
93-
dataToGet = *(int32_t *)(FLASH_EEPROM_START + eepromLocation);
93+
dataToGet = *(int32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
9494
}
9595
void ap3_EEPROM::get(uint16_t eepromLocation, uint32_t &dataToGet)
9696
{
97-
dataToGet = *(uint32_t *)(FLASH_EEPROM_START + eepromLocation);
97+
dataToGet = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
9898
}
9999
void ap3_EEPROM::get(uint16_t eepromLocation, float &dataToGet)
100100
{
101101
union {
102102
float f;
103103
uint32_t b;
104104
} temp;
105-
temp.b = *(uint32_t *)(FLASH_EEPROM_START + eepromLocation);
105+
temp.b = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
106106

107107
dataToGet = temp.f;
108108
}
@@ -113,8 +113,8 @@ void ap3_EEPROM::get(uint16_t eepromLocation, double &dataToGet)
113113
double lf;
114114
uint32_t b[2];
115115
} temp;
116-
temp.b[1] = *(uint32_t *)(FLASH_EEPROM_START + eepromLocation); //LSB;
117-
temp.b[0] = *(uint32_t *)(FLASH_EEPROM_START + eepromLocation + 4) << 32; //MSB;
116+
temp.b[1] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation); //LSB;
117+
temp.b[0] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation + 4) << 32; //MSB;
118118
dataToGet = temp.lf;
119119
}
120120

@@ -125,31 +125,31 @@ void ap3_EEPROM::get(uint16_t eepromLocation, double &dataToGet)
125125

126126
void ap3_EEPROM::put(uint16_t eepromLocation, uint8_t dataToWrite)
127127
{
128-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFFFF00);
128+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFFFF00);
129129
}
130130
void ap3_EEPROM::put(uint16_t eepromLocation, uint16_t dataToWrite)
131131
{
132-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
132+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
133133
}
134134
void ap3_EEPROM::put(uint16_t eepromLocation, int16_t dataToWrite)
135135
{
136-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
136+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
137137
}
138138
void ap3_EEPROM::put(uint16_t eepromLocation, int dataToWrite) //ints are 32 bit on M4F
139139
{
140-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
140+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
141141
}
142142
void ap3_EEPROM::put(uint16_t eepromLocation, unsigned int dataToWrite) //ints are 32 bit on M4F
143143
{
144-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
144+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
145145
}
146146
void ap3_EEPROM::put(uint16_t eepromLocation, int32_t dataToWrite)
147147
{
148-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (int32_t)dataToWrite);
148+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (int32_t)dataToWrite);
149149
}
150150
void ap3_EEPROM::put(uint16_t eepromLocation, uint32_t dataToWrite)
151151
{
152-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
152+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
153153
}
154154
void ap3_EEPROM::put(uint16_t eepromLocation, float dataToWrite)
155155
{
@@ -159,7 +159,7 @@ void ap3_EEPROM::put(uint16_t eepromLocation, float dataToWrite)
159159
} temp;
160160
temp.f = dataToWrite;
161161

162-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b);
162+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b);
163163
}
164164

165165
void ap3_EEPROM::put(uint16_t eepromLocation, double dataToWrite) //64 bits
@@ -170,8 +170,8 @@ void ap3_EEPROM::put(uint16_t eepromLocation, double dataToWrite) //64 bits
170170
} temp;
171171
temp.lf = dataToWrite;
172172

173-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b[1]); //LSB
174-
writeWordToFlash((FLASH_EEPROM_START + eepromLocation + 4), (uint32_t)temp.b[0]); //MSB
173+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b[1]); //LSB
174+
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation + 4), (uint32_t)temp.b[0]); //MSB
175175
}
176176

177177
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
@@ -217,12 +217,17 @@ void ap3_EEPROM::update(uint16_t eepromLocation, double dataToWrite) //64 bits
217217

218218
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
219219

220+
uint16_t ap3_EEPROM::length()
221+
{
222+
return (AP3_FLASH_EEPROM_SIZE);
223+
}
224+
220225
//Erase 8k page encapsulating the EEPROM section
221226
void ap3_EEPROM::erase()
222227
{
223228
am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
224-
AM_HAL_FLASH_ADDR2INST(FLASH_EEPROM_START),
225-
AM_HAL_FLASH_ADDR2PAGE(FLASH_EEPROM_START));
229+
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START),
230+
AM_HAL_FLASH_ADDR2PAGE(AP3_FLASH_EEPROM_START));
226231
}
227232

228233
//This is the main helper function
@@ -238,11 +243,11 @@ void ap3_EEPROM::erase()
238243
void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
239244
{
240245
//Error check
241-
if (flashLocation >= FLASH_EEPROM_START + FLASH_EEPROM_SIZE)
246+
if (flashLocation >= AP3_FLASH_EEPROM_START + AP3_FLASH_EEPROM_SIZE)
242247
{
243248
return;
244249
}
245-
if (flashLocation < FLASH_EEPROM_START)
250+
if (flashLocation < AP3_FLASH_EEPROM_START)
246251
{
247252
return;
248253
}
@@ -254,11 +259,11 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
254259
}
255260

256261
//First we have to read the contents of current "EEPROM" to SRAM
257-
uint32_t tempContents[FLASH_EEPROM_SIZE / 4];
262+
uint32_t tempContents[AP3_FLASH_EEPROM_SIZE / 4];
258263
uint16_t spot = 0;
259-
for (uint16_t x = 0; x < FLASH_EEPROM_SIZE; x += 4)
264+
for (uint16_t x = 0; x < AP3_FLASH_EEPROM_SIZE; x += 4)
260265
{
261-
tempContents[spot++] = *(uint32_t *)(FLASH_EEPROM_START + x);
266+
tempContents[spot++] = *(uint32_t *)(AP3_FLASH_EEPROM_START + x);
262267
}
263268

264269
//Then we erase an 8K page
@@ -268,52 +273,37 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
268273

269274
//Zero out this word(s)
270275
uint8_t byteOffset = (flashLocation % 4);
271-
uint16_t wordLocation = (flashLocation - FLASH_EEPROM_START) / 4;
272-
if (byteOffset == 0)
273-
{
274-
//Easy - reset this word to 1s
275-
tempContents[wordLocation] = 0xFFFFFFFF;
276-
}
277-
else
278-
{
279-
//Reset the upper bytes of the first word to 1s
280-
tempContents[wordLocation] |= 0xFFFFFFFF << (byteOffset * 8);
281-
282-
//Reset the lower bytes of the second word to 1s
283-
tempContents[wordLocation + 1] |= 0xFFFFFFFF >> ((4 - byteOffset) * 8);
284-
}
285-
286-
//Then we write the contents of the array back
287-
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
288-
tempContents,
289-
(uint32_t *)FLASH_EEPROM_START,
290-
FLASH_EEPROM_SIZE);
276+
uint16_t wordLocation = (flashLocation - AP3_FLASH_EEPROM_START) / 4;
291277

278+
//Mask in the new data into the array
292279
if (byteOffset == 0)
293280
{
294281
//Easy - update this word with new word
295-
am_hal_flash_reprogram_ui32(AM_HAL_FLASH_PROGRAM_KEY,
296-
dataToWrite,
297-
(uint32_t *)flashLocation);
282+
tempContents[wordLocation] = dataToWrite;
298283
}
299284
else
300285
{
301-
//Update the upper bytes of this word with new data
286+
//Clear the upper bytes of the first word to 0s
287+
tempContents[wordLocation] &= ~(0xFFFFFFFF << (byteOffset * 8));
288+
289+
//Clear the lower bytes of the second word to 0s
290+
tempContents[wordLocation + 1] &= ~(0xFFFFFFFF >> ((4 - byteOffset) * 8));
291+
292+
//OR in upper bytes of this word with new data
302293
uint32_t dataToWriteFirstWord = dataToWrite << (byteOffset * 8);
303-
dataToWriteFirstWord |= 0xFFFFFFFF >> ((4 - byteOffset) * 8);
304294

305-
//Update the lower bytes of the following word with new data
295+
//OR in the lower bytes of the following word with new data
306296
uint32_t dataToWriteSecondWord = dataToWrite >> ((4 - byteOffset) * 8);
307-
dataToWriteSecondWord |= 0xFFFFFFFF << (byteOffset * 8);
308297

309-
am_hal_flash_reprogram_ui32(AM_HAL_FLASH_PROGRAM_KEY,
310-
dataToWriteFirstWord,
311-
(uint32_t *)(flashLocation - byteOffset));
312-
313-
am_hal_flash_reprogram_ui32(AM_HAL_FLASH_PROGRAM_KEY,
314-
dataToWriteSecondWord,
315-
(uint32_t *)(flashLocation + (4 - byteOffset)));
298+
tempContents[wordLocation] |= dataToWriteFirstWord;
299+
tempContents[wordLocation + 1] |= dataToWriteSecondWord;
316300
}
301+
302+
//Then we write the contents of the array back
303+
am_hal_flash_program_main(AM_HAL_FLASH_PROGRAM_KEY,
304+
tempContents,
305+
(uint32_t *)AP3_FLASH_EEPROM_START,
306+
AP3_FLASH_EEPROM_SIZE);
317307
}
318308

319309
ap3_EEPROM EEPROM;

libraries/EEPROM/src/EEPROM.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@
4747
//user code space from 0xC000 to 0xFE000.
4848
//The SparkFun Apollo3 linker script has been modified to limit user code space to less than 0xFE000
4949

50-
#define FLASH_EEPROM_START 0xFE000
50+
#define AP3_FLASH_EEPROM_START 0xFE000
5151

52-
#if FLASH_EEPROM_START % 8192
52+
#if AP3_FLASH_EEPROM_START % 8192
5353
Error : EEPROM start address must be divisble by 8192
5454
#endif
5555

5656
//By limiting EEPROM size to 1024, we reduce the amount of SRAM required and
5757
//time needed to mask in individual bytes and words into flash. It can be increased
5858
//to 8096 if needed
59-
#define FLASH_EEPROM_SIZE 1024
59+
#define AP3_FLASH_EEPROM_SIZE 1024
6060

6161
//class TwoWire : public Stream, public IOMaster{}
6262

@@ -126,6 +126,7 @@ Error : EEPROM start address must be divisble by 8192
126126

127127
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
128128

129+
uint16_t length();
129130
void erase(); //Erase entire EEPROM
130131

131132
private:

0 commit comments

Comments
 (0)