MySensors Library & Examples  2.3.2-62-ge298769
eepromTest_Wire1.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 //Mik 03Jan2017 (configured Library to use the Wire1 as I2C channel)
8 
9 #include <extEEPROM.h> //https://github.com/PaoloP74/extEEPROM
10 
11 //One 24LC256 EEPROMs on the bus
12 const uint32_t totalKBytes = 32; //for read and write test functions
13 extEEPROM eep(kbits_256, 1, 64, 0x57); //device size, number of devices, page size
14 
15 const uint8_t btnStart = 6; //start button
16 
17 void setup(void)
18 {
19  uint8_t eepStatus;
20  pinMode(btnStart, INPUT_PULLUP);
21  Serial.begin(115200);
22  while (!SerialUSB) {}
23 
24  bool channelInsert = false;
25  Serial.println(F("Select the number of Wire channel use the eeprom"));
26  Serial.println(F("0 = Wire"));
27  Serial.println(F("1 = Wire1"));
28  Serial.println(F("...."));
29  Serial.println(F("x = WIRE_INTERFACES_COUNT"));
30 
31  do {
32  if (Serial.available()) {
33  char I2Cchannel = Serial.read();
34 
35  // only number that are less than WIRE_INTERFACES_COUNT are allowed
36  if ((I2Cchannel > '0') && (I2Cchannel < ('0' + WIRE_INTERFACES_COUNT))) {
37  channelInsert = true;
38  }
39 
40  switch ((I2Cchannel - '0')) {
41 
42  case 0:
43  Serial.println(F("Using the default Wire interface"));
44  eepStatus = eep.begin(eep.twiClock400kHz); //go fast!
45  break;
46 
47  case 1:
48  Serial.println(F("Using the Wire1 interface"));
49  eepStatus = eep.begin(eep.twiClock400kHz, &Wire1); //go fast!
50  break;
51 
52  /*
53  Uncomment till the number of WIRE_INTERFACES_COUNT of your Arduino board
54  case 2:
55  Serial.println(F("Using the Wire2 interface"));
56  eepStatus = eep.begin(eep.twiClock400kHz, &Wire2); //go fast!
57  break;
58 
59  case 3:
60  Serial.println(F("Using the Wire3 interface"));
61  eepStatus = eep.begin(eep.twiClock400kHz, &Wire3); //go fast!
62  break;
63 
64  case 4:
65  Serial.println(F("Using the Wire4 interface"));
66  eepStatus = eep.begin(eep.twiClock400kHz, &Wire4); //go fast!
67  break;
68 
69  case 5:
70  Serial.println(F("Using the Wire5 interface"));
71  eepStatus = eep.begin(eep.twiClock400kHz, &Wire5); //go fast!
72  break;*/
73 
74  default:
75  Serial.println(F("A wrong channel has been inserted (Arduino manage max 5)"));
76  break;
77  }
78  }
79  } while (!channelInsert);
80 
81  if (eepStatus) {
82  Serial.print(F("extEEPROM.begin() failed, status = "));
83  Serial.println(eepStatus);
84  while (1);
85  }
86 
87  Serial.println(F("Started !!"));
88 
89  uint8_t chunkSize =
90  64; //this can be changed, but must be a multiple of 4 since we're writing 32-bit integers
91  // eeErase(chunkSize, 0, totalKBytes * 1024 - 1);
92  eeWrite(chunkSize);
93  eeRead(chunkSize);
94 
95  dump(0, 32); //the first 32 bytes
96  dump(32256, 64); //the last 64 bytes
97  //dump(32512, 64); //across the device boundary
98  //dump(65520, 16); //the last 16 bytes
99 }
100 
101 void loop(void)
102 {
103 }
104 
105 //write test data (32-bit integers) to eeprom, "chunk" bytes at a time
106 void eeWrite(uint8_t chunk)
107 {
108  chunk &= 0xFC; //force chunk to be a multiple of 4
109  uint8_t data[chunk];
110  uint32_t val = 0;
111  Serial.println(F("Writing..."));
112  uint32_t msStart = millis();
113 
114  for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
115  if ( (addr & 0xFFF) == 0 ) {
116  Serial.println(addr);
117  }
118  for (uint8_t c = 0; c < chunk; c += 4) {
119  data[c + 0] = val >> 24;
120  data[c + 1] = val >> 16;
121  data[c + 2] = val >> 8;
122  data[c + 3] = val;
123  ++val;
124  }
125  eep.write(addr, data, chunk);
126  }
127  uint32_t msLapse = millis() - msStart;
128  Serial.print(F("Write lapse: "));
129  Serial.print(msLapse);
130  Serial.println(F(" ms"));
131 }
132 
133 //read test data (32-bit integers) from eeprom, "chunk" bytes at a time
134 void eeRead(uint8_t chunk)
135 {
136  chunk &= 0xFC; //force chunk to be a multiple of 4
137  uint8_t data[chunk];
138  uint32_t val = 0, testVal;
139  Serial.println(F("Reading..."));
140  uint32_t msStart = millis();
141 
142  for (uint32_t addr = 0; addr < totalKBytes * 1024; addr += chunk) {
143  if ( (addr & 0xFFF) == 0 ) {
144  Serial.println(addr);
145  }
146  eep.read(addr, data, chunk);
147  for (uint8_t c = 0; c < chunk; c += 4) {
148  testVal = ((uint32_t)data[c + 0] << 24) + ((uint32_t)data[c + 1] << 16) + ((
149  uint32_t)data[c + 2] << 8) + (uint32_t)data[c + 3];
150  if (testVal != val) {
151  Serial.print(F("Error @ addr "));
152  Serial.print(addr + c);
153  Serial.print(F(" Expected "));
154  Serial.print(val);
155  Serial.print(F(" Read "));
156  Serial.print(testVal);
157  Serial.print(F(" 0x"));
158  Serial.println(testVal, HEX);
159  }
160  ++val;
161  }
162  }
163  uint32_t msLapse = millis() - msStart;
164  Serial.print(F("Last value: "));
165  Serial.print(val);
166  Serial.print(F(" Read lapse: "));
167  Serial.print(msLapse);
168  Serial.println(F(" ms"));
169 }
170 
171 //write 0xFF to eeprom, "chunk" bytes at a time
172 void eeErase(uint8_t chunk, uint32_t startAddr, uint32_t endAddr)
173 {
174  chunk &= 0xFC; //force chunk to be a multiple of 4
175  uint8_t data[chunk];
176  Serial.println(F("Erasing..."));
177  for (int i = 0; i < chunk; i++) {
178  data[i] = 0xFF;
179  }
180  uint32_t msStart = millis();
181 
182  for (uint32_t a = startAddr; a <= endAddr; a += chunk) {
183  if ( (a & 0xFFF) == 0 ) {
184  Serial.println(a);
185  }
186  eep.write(a, data, chunk);
187  }
188  uint32_t msLapse = millis() - msStart;
189  Serial.print(F("Erase lapse: "));
190  Serial.print(msLapse);
191  Serial.print(F(" ms"));
192 }
193 
194 //dump eeprom contents, 16 bytes at a time.
195 //always dumps a multiple of 16 bytes.
196 void dump(uint32_t startAddr, uint32_t nBytes)
197 {
198  Serial.print(F("EEPROM DUMP 0x"));
199  Serial.print(startAddr, HEX);
200  Serial.print(F(" 0x"));
201  Serial.print(nBytes, HEX);
202  Serial.print(F(" "));
203  Serial.print(startAddr);
204  Serial.print(F(" "));
205  Serial.println(nBytes);
206  uint32_t nRows = (nBytes + 15) >> 4;
207 
208  uint8_t d[16];
209  for (uint32_t r = 0; r < nRows; r++) {
210  uint32_t a = startAddr + 16 * r;
211  eep.read(a, d, 16);
212  Serial.print(F("0x"));
213  if ( a < 16 * 16 * 16 ) {
214  Serial.print(F("0"));
215  }
216  if ( a < 16 * 16 ) {
217  Serial.print(F("0"));
218  }
219  if ( a < 16 ) {
220  Serial.print(F("0"));
221  }
222  Serial.print(a, HEX);
223  Serial.print(F(" "));
224  for ( int c = 0; c < 16; c++ ) {
225  if ( d[c] < 16 ) {
226  Serial.print(F("0"));
227  Serial.print(d[c], HEX);
228  Serial.print( c == 7 ? " " : " ");
229  }
230  }
231  Serial.println(F(""));
232  }
233 }
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:654
extEEPROM::begin
byte begin(twiClockFreq_t twiFreq=twiClock100kHz, TwoWire *_comm=&Wire)
begin()
loop
void loop(void)
Main loop.
Definition: eepromTest_Wire1.ino:101
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_Wire1.ino:17
extEEPROM::twiClock400kHz
@ twiClock400kHz
twiClock400kHz
Definition: extEEPROM.h:99