MySensors Library & Examples  2.3.2-62-ge298769
eepromReadWrite.ino
1 /*
2  Copyright (c) 2014 Arduino. All right reserved.
3 
4  This library is free software; you can redistribute it and / or
5  modify it under the terms of the GNU Lesser General Public
6  License as published by the Free Software Foundation; either
7  version 2.1 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  See the GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110 - 1301 USA
17 */
18 
19 #include "extEEPROM.h"
20 
21 extEEPROM myEEPROM(kbits_256, 1, 64, 0x57);
22 
23 void setup(void)
24 {
25  SerialUSB.begin(115200);
26  while (!SerialUSB) {
27  ;
28  }
29 
30  byte i2cStat = myEEPROM.begin(myEEPROM.twiClock100kHz);
31  if ( i2cStat != 0 ) {
32  SerialUSB.println(F("I2C Problem"));
33  }
34 
35  SerialUSB.println(
36  F("EEPROM Memory commands: read:(a)(l)(r) , write:(a)(d)(w), next read data (n)"));
37  SerialUSB.println(F("- Commands TO PRESS:"));
38  SerialUSB.println(F("\t a : memory address to read / write"));
39  SerialUSB.println(F("\t d : data to write"));
40  SerialUSB.println(F("\t l : data to write"));
41  SerialUSB.println(F("\t r : read command"));
42  SerialUSB.println(F("\t w : write command"));
43 }
44 
45 unsigned long address = 0;
46 const unsigned int maxDataSize = 1024; //0x8000; // 32 k bytes (32768 = 0x8000) = 256 kbits
47 
48 byte data[maxDataSize] = {'p', 'i', 'p', 'p', 'o'};
49 unsigned int dataSize = 5;
50 
51 void eprom_read_write(bool write)
52 {
53  byte i2cStat = 0;
54  if (write) {
55  i2cStat = myEEPROM.write(address, data, dataSize);
56  } else {
57  memset(data, 0, maxDataSize);
58  i2cStat = myEEPROM.read(address, data, dataSize);
59  }
60  if ( i2cStat != 0 ) {
61  //there was a problem
62  SerialUSB.print(F("I2C Problem: "));
63  if ( i2cStat == EEPROM_ADDR_ERR) {
64  SerialUSB.println(F("Wrong address"));
65  } else {
66  SerialUSB.print(F("I2C error: "));
67  SerialUSB.print(i2cStat);
68  SerialUSB.println(F(""));
69  }
70  }
71 }
72 
73 
74 void parse(char inChar)
75 {
76  const char addr_len = 5;
77  char addr_char[addr_len] = "";
78  const char data_len = 3;
79  char data_char[data_len] = "";
80  char size_char[data_len] = "";
81  char inc = 0, i = 0, j = 0;
82 
83  switch (inChar) {
84  case 'a':
85  SerialUSB.print(F("Insert Address as 4 Hex chars (without '0x'): "));
86 
87  while (i < 4) {
88  while (SerialUSB.available() <= 0)
89  ;
90  inc = SerialUSB.read();
91 
92  if (inc == 'q') {
93  return;
94  }
95 
96  addr_char[i] = inc;
97  ++i;
98  }
99  address = (unsigned long)strtol(addr_char, NULL, 16);
100  SerialUSB.println(address);
101  break;
102 
103  case 'd':
104  SerialUSB.print(F("Insert Hex data sequence (without '0x'), return to enter: "));
105  memset(data, 0, maxDataSize);
106  while (true) {
107  while (SerialUSB.available() <= 0)
108  ;
109  inc = SerialUSB.read();
110  if (inc == 'q') {
111  return;
112  }
113  if (inc == '\r' || inc == '\n') {
114  break;
115  }
116 
117  if (inc >= 'a' && inc <= 'f') {
118  data[j] += inc - 'a' + 10;
119  } else if (inc >= 'A' && inc <= 'F') {
120  data[j] += inc - 'A' + 10;
121  } else if (inc >= '0' && inc <= '9') {
122  data[j] += inc - '0';
123  } else {
124  return;
125  }
126 
127  if (i % 2) {
128  j++;
129  } else {
130  data[j] = data[j] << 4;
131  }
132  i++;
133  }
134  dataSize = j;
135  SerialUSB.println(dataSize);
136  SerialUSB.println(F(""));
137  break;
138  case 'l':
139  SerialUSB.print(F("Insert data len as 2 Hex chars (without '0x'): "));
140  while (i < 2) {
141  while (SerialUSB.available() <= 0)
142  ;
143  inc = SerialUSB.read();
144  if (inc == 'q') {
145  return;
146  }
147 
148  size_char[i] = inc;
149  ++i;
150  if (inc == '\n') {
151  return;
152  }
153  }
154 
155  dataSize = (unsigned int)strtol(size_char, NULL, 16);
156  SerialUSB.println(dataSize);
157  break;
158 
159 
160  case 'n':
161  address += dataSize;
162  /* FALLTHROUGH */
163  case 'r':
164  SerialUSB.print(F("reading address: "));
165  SerialUSB.println(address, HEX);
166 
167  eprom_read_write(false);
168  for (i = 0; i < dataSize ; ++i) {
169  SerialUSB.print(data[i], HEX);
170  SerialUSB.print(F(" "));
171  }
172  SerialUSB.println();
173 
174  break;
175 
176  case 'w':
177  SerialUSB.print(F("writing at address: "));
178  SerialUSB.print(address, HEX);
179  SerialUSB.print(F(", len: "));
180  SerialUSB.println(address, dataSize);
181  for (i = 0; i < dataSize ; ++i) {
182  SerialUSB.print(data[i], HEX);
183  SerialUSB.print(F(" "));
184  }
185  eprom_read_write(true);
186  SerialUSB.println();
187 
188  break;
189  case 'T':
190  SerialUSB.println(F("Memory test: writing and verifying the whole memory"));
191  break;
192 
193  default:
194  break;
195  }
196 }
197 
198 
199 void loop(void)
200 {
201  if (SerialUSB.available() > 0) {
202  char inChar = SerialUSB.read();
203  SerialUSB.print(inChar);
204  parse(inChar);
205  }
206 
207  delay(10);
208 }
209 
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: eepromReadWrite.ino:199
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: eepromReadWrite.ino:23
extEEPROM::twiClock100kHz
@ twiClock100kHz
twiClock100kHz
Definition: extEEPROM.h:98