Skip to content

Commit 0a4b707

Browse files
committed
Re-write of EEPROM functions
This uses the more common EEPROM methods currently built into Arduino. The approach is a lot more complex but handles any size variable and struct more gracefully than previous approach. I originally hit this problem when trying to record a struct. This new approach works flawlessly. Tested against examples and various attempts to break it.
1 parent 51dd1a3 commit 0a4b707

File tree

4 files changed

+172
-243
lines changed

4 files changed

+172
-243
lines changed

libraries/EEPROM/examples/Example1_GetPut/Example1_GetPut.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
void setup()
2727
{
28-
Serial.begin(9600);
28+
Serial.begin(115200);
2929
Serial.println("EEPROM Examples");
3030

3131
byte myValue1 = 200;

libraries/EEPROM/examples/Example2_AllFunctions/Example2_AllFunctions.ino

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
void setup()
2525
{
26-
Serial.begin(9600);
26+
Serial.begin(115200);
2727
Serial.println("EEPROM Examples");
2828

2929
randomSeed(analogRead(A0));
@@ -106,8 +106,8 @@ void setup()
106106
uint32_t myValue8 = 241544;
107107
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
108108

109-
EEPROM.update(randomLocation, myValue7);
110-
EEPROM.update(randomLocation + 4, myValue8);
109+
EEPROM.put(randomLocation, myValue7);
110+
EEPROM.put(randomLocation + 4, myValue8);
111111

112112
int32_t response7;
113113
uint32_t response8;
@@ -124,8 +124,8 @@ void setup()
124124
float myValue10 = 5.22;
125125
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
126126

127-
EEPROM.update(randomLocation, myValue9);
128-
EEPROM.update(randomLocation + 4, myValue10);
127+
EEPROM.put(randomLocation, myValue9);
128+
EEPROM.put(randomLocation + 4, myValue10);
129129

130130
float response9;
131131
float response10;
@@ -145,8 +145,8 @@ void setup()
145145
double myValue12 = 384.95734987;
146146
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
147147

148-
EEPROM.update(randomLocation, myValue11);
149-
EEPROM.update(randomLocation + 8, myValue12);
148+
EEPROM.put(randomLocation, myValue11);
149+
EEPROM.put(randomLocation + 8, myValue12);
150150

151151
double response11;
152152
double response12;
@@ -156,7 +156,22 @@ void setup()
156156
Serial.printf("Location %d should be %lf: %lf\n", randomLocation + 8, myValue12, response12);
157157
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
158158

159-
Serial.println();
159+
Serial.println("");
160+
Serial.println("String test");
161+
162+
//String write test
163+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
164+
String myString = "How are you today?";
165+
randomLocation = random(0, AP3_FLASH_EEPROM_SIZE);
166+
EEPROM.put(randomLocation, myString);
167+
168+
String readMy;
169+
EEPROM.get(randomLocation, readMy);
170+
Serial.printf("Location %d string should read 'How are you today?': ", randomLocation);
171+
Serial.println(readMy);
172+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
173+
174+
Serial.println("");
160175
Serial.print("Flash Contents:");
161176
for (uint16_t x = 0; x < 8 * 4; x += 4)
162177
{

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 11 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -40,190 +40,37 @@
4040
#include "EEPROM.h"
4141
#include "Arduino.h"
4242

43-
//Constructor
44-
ap3_EEPROM::ap3_EEPROM()
45-
{
46-
}
47-
4843
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
4944

5045
//Write a byte to a given "EEPROM" location
5146
//Automatically masks user's byte into flash without
5247
//affecting other bytes in this flash word
53-
void ap3_EEPROM::write(uint16_t eepromLocation, uint8_t dataToWrite)
48+
void write(uint16_t eepromLocation, uint8_t dataToWrite)
5449
{
5550
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
5651
writeWordToFlash(flashLocation, (uint32_t)dataToWrite | 0xFFFFFF00);
5752
}
5853

5954
//Read a byte from a given location in "EEPROM"
60-
uint8_t ap3_EEPROM::read(uint16_t eepromLocation)
55+
uint8_t read(uint16_t eepromLocation)
6156
{
6257
uint32_t flashLocation = AP3_FLASH_EEPROM_START + eepromLocation;
6358
return (*(uint8_t *)flashLocation);
6459
}
6560

66-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
67-
68-
//Get method is overloaded with the following variable types
69-
//char, byte, int, unsigned int, long, unsigned long, float, double?
70-
71-
void ap3_EEPROM::get(uint16_t eepromLocation, uint8_t &dataToGet)
72-
{
73-
dataToGet = *(uint8_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
74-
}
75-
void ap3_EEPROM::get(uint16_t eepromLocation, uint16_t &dataToGet)
76-
{
77-
dataToGet = *(uint16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
78-
}
79-
void ap3_EEPROM::get(uint16_t eepromLocation, int16_t &dataToGet)
80-
{
81-
dataToGet = *(int16_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
82-
}
83-
void ap3_EEPROM::get(uint16_t eepromLocation, int &dataToGet)
84-
{
85-
dataToGet = *(int *)(AP3_FLASH_EEPROM_START + eepromLocation);
86-
}
87-
void ap3_EEPROM::get(uint16_t eepromLocation, unsigned int &dataToGet)
88-
{
89-
dataToGet = *(unsigned int *)(AP3_FLASH_EEPROM_START + eepromLocation);
90-
}
91-
void ap3_EEPROM::get(uint16_t eepromLocation, int32_t &dataToGet)
92-
{
93-
dataToGet = *(int32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
94-
}
95-
void ap3_EEPROM::get(uint16_t eepromLocation, uint32_t &dataToGet)
96-
{
97-
dataToGet = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
98-
}
99-
void ap3_EEPROM::get(uint16_t eepromLocation, float &dataToGet)
100-
{
101-
union {
102-
float f;
103-
uint32_t b;
104-
} temp;
105-
temp.b = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation);
106-
107-
dataToGet = temp.f;
108-
}
109-
110-
void ap3_EEPROM::get(uint16_t eepromLocation, double &dataToGet)
111-
{
112-
union {
113-
double lf;
114-
uint32_t b[2];
115-
} temp;
116-
temp.b[1] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation); //LSB;
117-
temp.b[0] = *(uint32_t *)(AP3_FLASH_EEPROM_START + eepromLocation + 4); //MSB;
118-
dataToGet = temp.lf;
119-
}
120-
121-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
122-
123-
//Put method is overloaded with the following variable types
124-
//char, byte, int, unsigned int, long, unsigned long, float, double?
125-
126-
void ap3_EEPROM::put(uint16_t eepromLocation, uint8_t dataToWrite)
127-
{
128-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFFFF00);
129-
}
130-
void ap3_EEPROM::put(uint16_t eepromLocation, uint16_t dataToWrite)
131-
{
132-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
133-
}
134-
void ap3_EEPROM::put(uint16_t eepromLocation, int16_t dataToWrite)
135-
{
136-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite | 0xFFFF0000);
137-
}
138-
void ap3_EEPROM::put(uint16_t eepromLocation, int dataToWrite) //ints are 32 bit on M4F
139-
{
140-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
141-
}
142-
void ap3_EEPROM::put(uint16_t eepromLocation, unsigned int dataToWrite) //ints are 32 bit on M4F
61+
//Write a new byte to a given location in "EEROM" only if new data is there
62+
void update(uint16_t eepromLocation, uint8_t dataToWrite)
14363
{
144-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
145-
}
146-
void ap3_EEPROM::put(uint16_t eepromLocation, int32_t dataToWrite)
147-
{
148-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (int32_t)dataToWrite);
149-
}
150-
void ap3_EEPROM::put(uint16_t eepromLocation, uint32_t dataToWrite)
151-
{
152-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)dataToWrite);
153-
}
154-
void ap3_EEPROM::put(uint16_t eepromLocation, float dataToWrite)
155-
{
156-
union {
157-
float f;
158-
uint32_t b;
159-
} temp;
160-
temp.f = dataToWrite;
161-
162-
writeWordToFlash((AP3_FLASH_EEPROM_START + eepromLocation), (uint32_t)temp.b);
163-
}
164-
165-
void ap3_EEPROM::put(uint16_t eepromLocation, double dataToWrite) //64 bits
166-
{
167-
union {
168-
double lf;
169-
uint32_t b[2];
170-
} temp;
171-
temp.lf = dataToWrite;
172-
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
175-
}
176-
177-
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
178-
179-
//The update functions simply call the put functions
180-
//Put automatically checks to see if a spot needs updating
181-
void ap3_EEPROM::update(uint16_t eepromLocation, uint8_t dataToWrite)
182-
{
183-
put(eepromLocation, dataToWrite);
184-
}
185-
void ap3_EEPROM::update(uint16_t eepromLocation, uint16_t dataToWrite)
186-
{
187-
put(eepromLocation, dataToWrite);
188-
}
189-
void ap3_EEPROM::update(uint16_t eepromLocation, int16_t dataToWrite)
190-
{
191-
put(eepromLocation, dataToWrite);
192-
}
193-
void ap3_EEPROM::update(uint16_t eepromLocation, int dataToWrite) //ints are 32 bit on M4F
194-
{
195-
put(eepromLocation, dataToWrite);
196-
}
197-
void ap3_EEPROM::update(uint16_t eepromLocation, unsigned int dataToWrite) //ints are 32 bit on M4F
198-
{
199-
put(eepromLocation, dataToWrite);
200-
}
201-
void ap3_EEPROM::update(uint16_t eepromLocation, int32_t dataToWrite)
202-
{
203-
put(eepromLocation, dataToWrite);
204-
}
205-
void ap3_EEPROM::update(uint16_t eepromLocation, uint32_t dataToWrite)
206-
{
207-
put(eepromLocation, dataToWrite);
208-
}
209-
void ap3_EEPROM::update(uint16_t eepromLocation, float dataToWrite)
210-
{
211-
put(eepromLocation, dataToWrite);
212-
}
213-
void ap3_EEPROM::update(uint16_t eepromLocation, double dataToWrite) //64 bits
214-
{
215-
put(eepromLocation, dataToWrite);
64+
if (read(eepromLocation) != dataToWrite)
65+
{
66+
write(eepromLocation, dataToWrite);
67+
}
21668
}
21769

21870
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
21971

220-
uint16_t ap3_EEPROM::length()
221-
{
222-
return (AP3_FLASH_EEPROM_SIZE);
223-
}
224-
22572
//Erase 8k page encapsulating the EEPROM section
226-
void ap3_EEPROM::erase()
73+
void EEPROMClass::erase()
22774
{
22875
am_hal_flash_page_erase(AM_HAL_FLASH_PROGRAM_KEY,
22976
AM_HAL_FLASH_ADDR2INST(AP3_FLASH_EEPROM_START),
@@ -240,7 +87,7 @@ void ap3_EEPROM::erase()
24087
//4) Write SRAM back into flash
24188
//5) Write user's data onto the spot with recently created 0xFFs
24289
//Note - this code assumes EEPROM temp space is contained in one page
243-
void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
90+
void writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
24491
{
24592
//Error check
24693
if (flashLocation >= AP3_FLASH_EEPROM_START + AP3_FLASH_EEPROM_SIZE)
@@ -306,4 +153,4 @@ void ap3_EEPROM::writeWordToFlash(uint32_t flashLocation, uint32_t dataToWrite)
306153
AP3_FLASH_EEPROM_SIZE);
307154
}
308155

309-
ap3_EEPROM EEPROM;
156+
//ap3_EEPROM EEPROM;

0 commit comments

Comments
 (0)