MySensors Library & Examples  2.3.2-62-ge298769
eepromTest.ino
1 //Test extEEPROM library.
2 //Writes the EEPROM full of 32-bit integers and reads them back to verify.
3 //Wire a button from digital pin 6 to ground, this is used as a start button
4 //so the sketch doesn't do unnecessary EEPROM writes every time it's reset.
5 //Jack Christensen 09Jul2014
6 //Paolo Paolucci 17Mar2016 (fix 28Jun2017)
7 
8 #include <extEEPROM.h> //https://github.com/PaoloP74/extEEPROM
9 
10 //One 24LC256 EEPROMs on the bus
11 const uint32_t totalKBytes = 32; //for read and write test functions
12 extEEPROM eep(kbits_256, 1, 64); //device size, number of devices, page size
13 
14 const uint8_t btnStart = 6; //start button
15 
16 void setup(void)
17 {
18  pinMode(btnStart, INPUT_PULLUP);
19  Serial.begin(115200);
20  uint8_t eepStatus = eep.begin(eep.twiClock400kHz); //go fast!
21  if (eepStatus) {
22  Serial.print(F("extEEPROM.begin() failed, status = "));
23  Serial.println(eepStatus);
24  while (1);
25  }
26 
27  Serial.println(F("Press button to start..."));
28  while (digitalRead(btnStart) == HIGH) {
29  delay(10); //wait for button push
30  }
31 
32  uint8_t chunkSize =
33  64; //this can be changed, but must be a multiple of 4 since we're writing 32-bit integers
34  // eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
35  eeWrite(chunkSize);
36  eeRead(chunkSize);
37 
38  dump(0, 32); //the first 32 bytes
39  dump(32256, 64); //the last 64 bytes
40  //dump(32512, 64); //across the device boundary
41  //dump(65520, 16); //the last 16 bytes
42 }
43 
44 void loop(void)
45 {
46 }
47 
48 //write test data (32-bit integers) to eeprom, "chunk" bytes at a time
49 void eeWrite(uint8_t chunk)
50 {
51  chunk &= 0xFC; //force chunk to be a multiple of 4
52  uint8_t data[chunk];
53  uint32_t val = 0;
54  Serial.println(F("Writing..."));
55  uint32_t msStart = millis();
56 
57  for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
58  if ( (addr & 0xFFF) == 0 ) {
59  Serial.println(addr);
60  }
61  for (uint8_t c = 0; c < chunk; c += 4) {
62  data[c + 0] = val >> 24;
63  data[c + 1] = val >> 16;
64  data[c + 2] = val >> 8;
65  data[c + 3] = val;
66  ++val;
67  }
68  eep.write(addr, data, chunk);
69  }
70  uint32_t msLapse = millis() - msStart;
71  Serial.print(F("Write lapse: "));
72  Serial.print(msLapse);
73  Serial.println(F(" ms"));
74 }
75 
76 //read test data (32-bit integers) from eeprom, "chunk" bytes at a time
77 void eeRead(uint8_t chunk)
78 {
79  chunk &= 0xFC; //force chunk to be a multiple of 4
80  uint8_t data[chunk];
81  uint32_t val = 0, testVal;
82  Serial.println(F("Reading..."));
83  uint32_t msStart = millis();
84 
85  for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
86  if ( (addr & 0xFFF) == 0 ) {
87  Serial.println(addr);
88  }
89  eep.read(addr, data, chunk);
90  for (uint8_t c = 0; c < chunk; c += 4) {
91  testVal = ((uint32_t)data[c + 0] << 24) + ((uint32_t)data[c + 1] << 16) + ((
92  uint32_t)data[c + 2] << 8) + (uint32_t)data[c + 3];
93  if (testVal != val) {
94  Serial.print(F("Error @ addr "));
95  Serial.print(addr + c);
96  Serial.print(F(" Expected "));
97  Serial.print(val);
98  Serial.print(F(" Read "));
99  Serial.print(testVal);
100  Serial.print(F(" 0x"));
101  Serial.println(testVal, HEX);
102  }
103  ++val;
104  }
105  }
106  uint32_t msLapse = millis() - msStart;
107  Serial.print(F("Last value: "));
108  Serial.print(val);
109  Serial.print(F(" Read lapse: "));
110  Serial.print(msLapse);
111  Serial.println(F(" ms"));
112 }
113 
114 //write 0xFF to eeprom, "chunk" bytes at a time
115 void eeErase(uint8_t chunk, uint32_t startAddr, uint32_t endAddr)
116 {
117  chunk &= 0xFC; //force chunk to be a multiple of 4
118  uint8_t data[chunk];
119  Serial.println(F("Erasing..."));
120  for (int i = 0; i < chunk; i++) {
121  data[i] = 0xFF;
122  }
123  uint32_t msStart = millis();
124 
125  for (uint32_t a = startAddr; a <= endAddr; a += chunk) {
126  if ( (a & 0xFFF) == 0 ) {
127  Serial.println(a);
128  }
129  eep.write(a, data, chunk);
130  }
131  uint32_t msLapse = millis() - msStart;
132  Serial.print(F("Erase lapse: "));
133  Serial.print(msLapse);
134  Serial.print(F(" ms"));
135 }
136 
137 //dump eeprom contents, 16 bytes at a time.
138 //always dumps a multiple of 16 bytes.
139 void dump(uint32_t startAddr, uint32_t nBytes)
140 {
141  Serial.print(F("EEPROM DUMP 0x"));
142  Serial.print(startAddr, HEX);
143  Serial.print(F(" 0x"));
144  Serial.print(nBytes, HEX);
145  Serial.print(F(" "));
146  Serial.print(startAddr);
147  Serial.print(F(" "));
148  Serial.println(nBytes);
149  uint32_t nRows = (nBytes + 15) >> 4;
150 
151  uint8_t d[16];
152  for (uint32_t r = 0; r < nRows; r++) {
153  uint32_t a = startAddr + 16 * r;
154  eep.read(a, d, 16);
155  Serial.print(F("0x"));
156  if ( a < 16 * 16 * 16 ) {
157  Serial.print(F("0"));
158  }
159  if ( a < 16 * 16 ) {
160  Serial.print(F("0"));
161  }
162  if ( a < 16 ) {
163  Serial.print(F("0"));
164  }
165  Serial.print(a, HEX);
166  Serial.print(F(" "));
167  for ( int c = 0; c < 16; c++ ) {
168  if ( d[c] < 16 ) {
169  Serial.print(F("0"));
170  Serial.print(d[c], HEX);
171  Serial.print( c == 7 ? " " : " ");
172  }
173  }
174  Serial.println(F(""));
175  }
176 }
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:654
HIGH
#define HIGH
Definition: bcm2835.h:572
extEEPROM::begin
byte begin(twiClockFreq_t twiFreq=twiClock100kHz, TwoWire *_comm=&Wire)
begin()
loop
void loop(void)
Main loop.
Definition: eepromTest.ino:44
extEEPROM
Definition: extEEPROM.h:87
extEEPROM::read
byte read(unsigned long addr, byte *values, unsigned int nBytes)
read()
extEEPROM::write
byte write(unsigned long addr, byte *values, unsigned int nBytes)
write()
setup
void setup(void)
Called after node initialises but before main loop.
Definition: eepromTest.ino:16
extEEPROM::twiClock400kHz
@ twiClock400kHz
twiClock400kHz
Definition: extEEPROM.h:99