Skip to content

Commit 5ad5acf

Browse files
authored
Merge pull request #344 from sparkfun/refixEEPROM
Restore EEPROM support
2 parents fad6222 + d675e41 commit 5ad5acf

File tree

3 files changed

+244
-6
lines changed

3 files changed

+244
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
/*
2+
Reading and writing test of the EEPROM functions on the Artemis
3+
By: Nathan Seidle
4+
SparkFun Electronics
5+
Date: June 24th, 2019
6+
This example code is in the public domain.
7+
8+
SparkFun labored with love to create this code. Feel like supporting open source hardware?
9+
Buy a board from SparkFun! https://www.sparkfun.com/products/15376
10+
11+
Page erase takes 15ms
12+
Write byte takes 30ms - This is much longer than Arduino that takes 3.3ms
13+
Float write across two words takes 30ms
14+
Update (no write) takes 1ms
15+
*/
16+
17+
#include <EEPROM.h>
18+
19+
// Give a default if the variant does not define one.
20+
#ifndef A0
21+
#define A0 0
22+
#endif
23+
24+
void setup()
25+
{
26+
Serial.begin(115200);
27+
Serial.println("EEPROM Examples");
28+
29+
randomSeed(analogRead(A0));
30+
31+
EEPROM.init();
32+
33+
// You may choose to enable more or less EEPROM -
34+
// Default length is 1024 bytes (if setLength is not called)
35+
EEPROM.setLength(1024 * 1); // this would make the length 1080 bytes. Max is 4096.
36+
// Note: larger sizes will increase RAM usage and execution time
37+
38+
long startTime;
39+
long endTime;
40+
int randomLocation;
41+
42+
//Test erase time
43+
startTime = millis();
44+
EEPROM.erase();
45+
endTime = millis();
46+
Serial.printf("Time to erase all EEPROM: %dms\n", endTime - startTime);
47+
48+
//Byte sequential test
49+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
50+
Serial.println();
51+
Serial.println("8 bit tests");
52+
uint8_t myValue1 = 200;
53+
byte myValue2 = 23;
54+
randomLocation = random(0, EEPROM.length() - sizeof(myValue1));
55+
56+
startTime = millis();
57+
EEPROM.write(randomLocation, myValue1); //(location, data)
58+
endTime = millis();
59+
EEPROM.put(randomLocation + sizeof(myValue1), myValue2);
60+
61+
Serial.printf("Write byte time: %dms\n", endTime - startTime);
62+
63+
startTime = millis();
64+
EEPROM.write(randomLocation, myValue1); //(location, data)
65+
endTime = millis();
66+
67+
Serial.printf("Write identical byte to same location: %dms\n", endTime - startTime);
68+
69+
byte response1 = EEPROM.read(randomLocation);
70+
byte response2 = EEPROM.read(randomLocation + sizeof(myValue1));
71+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue1, response1);
72+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue1), myValue2, response2);
73+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
74+
75+
Serial.println();
76+
Serial.println("16 bit tests");
77+
78+
//int16_t and uint16_t sequential test
79+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
80+
uint16_t myValue3 = 3411;
81+
int16_t myValue4 = -366;
82+
randomLocation = random(0, EEPROM.length() - sizeof(myValue3));
83+
84+
EEPROM.put(randomLocation, myValue3);
85+
EEPROM.put(randomLocation + sizeof(myValue3), myValue4);
86+
87+
uint16_t response3;
88+
int16_t response4;
89+
EEPROM.get(randomLocation, response3);
90+
EEPROM.get(randomLocation + sizeof(myValue3), response4);
91+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue3, response3);
92+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue3), myValue4, response4);
93+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
94+
95+
Serial.println();
96+
Serial.println("32 bit tests");
97+
98+
//int and unsigned int (32) sequential test
99+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
100+
Serial.printf("Size of int: %d\n", sizeof(int));
101+
int myValue5 = -245000;
102+
unsigned int myValue6 = 400123;
103+
randomLocation = random(0, EEPROM.length() - sizeof(myValue5));
104+
105+
EEPROM.put(randomLocation, myValue5);
106+
EEPROM.put(randomLocation + sizeof(myValue5), myValue6);
107+
108+
int response5;
109+
unsigned int response6;
110+
EEPROM.get(randomLocation, response5);
111+
EEPROM.get(randomLocation + sizeof(myValue5), response6);
112+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue5, response5);
113+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue5), myValue6, response6);
114+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
115+
116+
//int32_t and uint32_t sequential test
117+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
118+
int32_t myValue7 = -341002;
119+
uint32_t myValue8 = 241544;
120+
randomLocation = random(0, EEPROM.length() - sizeof(myValue7));
121+
122+
EEPROM.put(randomLocation, myValue7);
123+
EEPROM.put(randomLocation + sizeof(myValue7), myValue8);
124+
125+
int32_t response7;
126+
uint32_t response8;
127+
EEPROM.get(randomLocation, response7);
128+
EEPROM.get(randomLocation + sizeof(myValue7), response8);
129+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation, myValue7, response7);
130+
Serial.printf("Location %d should be %d: %d\n\r", randomLocation + sizeof(myValue7), myValue8, response8);
131+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
132+
133+
//float (32) sequential test
134+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
135+
Serial.printf("Size of float: %d\n", sizeof(float));
136+
float myValue9 = -7.35;
137+
float myValue10 = 5.22;
138+
randomLocation = random(0, EEPROM.length() - sizeof(myValue9));
139+
140+
EEPROM.put(randomLocation, myValue9);
141+
EEPROM.put(randomLocation + sizeof(myValue9), myValue10);
142+
143+
float response9;
144+
float response10;
145+
EEPROM.get(randomLocation, response9);
146+
EEPROM.get(randomLocation + sizeof(myValue9), response10);
147+
Serial.printf("Location %d should be ", randomLocation);
148+
Serial.print(myValue9);
149+
Serial.print(": ");
150+
Serial.print(response9);
151+
Serial.println();
152+
Serial.printf("Location %d should be ", randomLocation + sizeof(myValue9));
153+
Serial.print(myValue10);
154+
Serial.print(": ");
155+
Serial.print(response10);
156+
Serial.println();
157+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
158+
159+
Serial.println();
160+
Serial.println("64 bit tests");
161+
162+
//double (64) sequential test
163+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
164+
Serial.printf("Size of double: %d\n", sizeof(double));
165+
double myValue11 = -290.3485723409857;
166+
double myValue12 = 384.95734987;
167+
double myValue13 = 917.14159;
168+
double myValue14 = 254.8877;
169+
randomLocation = random(0, EEPROM.length() - sizeof(myValue11));
170+
171+
startTime = millis();
172+
EEPROM.put(randomLocation, myValue11);
173+
endTime = millis();
174+
Serial.printf("Time to record 64-bits: %dms\n", endTime - startTime);
175+
176+
EEPROM.put(randomLocation + sizeof(myValue11), myValue12);
177+
EEPROM.put(EEPROM.length() - sizeof(myValue13), myValue13); //Test end of EEPROM space
178+
179+
double response11;
180+
double response12;
181+
double response13;
182+
EEPROM.get(randomLocation, response11);
183+
EEPROM.get(randomLocation + sizeof(myValue11), response12);
184+
EEPROM.get(EEPROM.length() - sizeof(myValue13), response13);
185+
186+
Serial.printf("Location %d should be ", randomLocation);
187+
Serial.print(myValue11, 13);
188+
Serial.print(": ");
189+
Serial.print(response11, 13);
190+
Serial.println();
191+
Serial.printf("Location %d should be ", randomLocation + sizeof(myValue11));
192+
Serial.print(myValue12, 8);
193+
Serial.print(": ");
194+
Serial.print(response12, 8);
195+
Serial.println();
196+
Serial.printf("Edge of EEPROM %d should be ", EEPROM.length() - sizeof(myValue13));
197+
Serial.print(myValue13, 5);
198+
Serial.print(": ");
199+
Serial.print(response13, 5);
200+
Serial.println();
201+
202+
double response14;
203+
EEPROM.put(EEPROM.length() - sizeof(myValue14), myValue14); //Test the re-write of a spot
204+
EEPROM.get(EEPROM.length() - sizeof(myValue14), response14);
205+
Serial.printf("Rewrite of %d should be ", EEPROM.length() - sizeof(myValue14));
206+
Serial.print(myValue14, 4);
207+
Serial.print(": ");
208+
Serial.print(response14, 4);
209+
Serial.println();
210+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
211+
212+
Serial.println();
213+
Serial.println("String test");
214+
215+
//String write test
216+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
217+
char myString[19] = "How are you today?";
218+
randomLocation = random(0, EEPROM.length() - sizeof(myString));
219+
EEPROM.put(randomLocation, myString);
220+
221+
char readMy[19];
222+
EEPROM.get(randomLocation, readMy);
223+
Serial.printf("Location %d string should read 'How are you today?': ", randomLocation);
224+
Serial.println(readMy);
225+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
226+
227+
}
228+
229+
void loop()
230+
{
231+
}

libraries/EEPROM/src/EEPROM.h

+13-6
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ class EEPROMClass : public FlashIAPBlockDevice {
2727
void config(eeprom_config_t cfg){
2828
_cfg = cfg;
2929
}
30+
mbed::bd_size_t length(void){
31+
return(_cfg.sram_bytes);
32+
}
3033
void configSramUsage(mbed::bd_size_t bytes){
3134
_cfg.sram_bytes = bytes;
3235
}
36+
void setLength(mbed::bd_size_t bytes){ //Old function to maintain compatibility
37+
configSramUsage(bytes);
38+
}
3339

3440
void read(int idx, uint8_t* data, uint32_t size){
3541
FlashIAPBlockDevice::read(data, idx, size);
@@ -40,16 +46,17 @@ class EEPROMClass : public FlashIAPBlockDevice {
4046
return val;
4147
}
4248
void write(int idx, uint8_t* data, uint32_t size){
43-
mbed::bd_size_t scratch_size = (_cfg.sram_bytes+3)/4;
44-
uint32_t scratch[scratch_size];
49+
mbed::bd_size_t scratch_size = _cfg.sram_bytes;
50+
uint8_t scratch[scratch_size];
51+
4552
FlashIAPBlockDevice::read((uint8_t*)scratch, 0, _cfg.sram_bytes); // keep all of flash in sram in case we need to erase
46-
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
53+
54+
if(memcmp((void*)(((uint8_t*)scratch) + idx), data, size)){ // compare desired data (data) to existing information in flash (scratch)
55+
memcpy(scratch + idx, data, size);
4756
erase();
48-
memcpy(scratch, data, size);
49-
int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, 4*scratch_size);
57+
int result = FlashIAPBlockDevice::program((uint8_t*)scratch, 0, scratch_size);
5058
return;
5159
}
52-
printf("contents already match\n");
5360
}
5461
void write(int idx, uint8_t val){
5562
write(idx, &val, 1);

0 commit comments

Comments
 (0)