I developed the following functions to copy the contents of a serial EEPROM to the SD card and then back to the EEPEOM as a means to backup EEPROM data or copy an EEPROM.
void readFromEeprom()
{
byte readVal = 0;
myFile = SD.open("EEPROM.txt", FILE_WRITE);
if(myFile)
{
SerialUSB.println(F("successfully opened EEPROM.txt"));
}
else
{
SerialUSB.println(F("error opening EEPROM.txt"));
return;
}
for(unsigned long i=0; i<MEM_SIZE; i++)
{
// Read from EEPROM at the current address
readVal = readAddress(i);
// For debugging/info only
if((i % KB_VAL == 0) && (i > 1))
{
unsigned long total = (unsigned long) (i / KB_VAL);
SerialUSB.print(F("Wrote "));
SerialUSB.print(total);
SerialUSB.println(F(" Kb"));
}
// Write the value to the SD Card
myFile.write(readVal);
}
myFile.close();
}
and
void readFromSD(){
uint16_t i = 0;
uint8_t byteData;
File dataFile = SD.open("EEPROM.txt");
if(dataFile) {
SerialUSB.println(F("successfully opened EEPROM.txt")); }
else
{
SerialUSB.println(F("error opening EEPROM.txt"));
return ;
}
while (i<MEM_SIZE && dataFile.available()){
byteData=dataFile.read();
writeEEPROM(EEPROM_I2C_ADDRESS,i,byteData);
i++;
// For debugging/info only
if((i % KB_VAL == 0) && (i > 1))
{
unsigned long total = (unsigned long) (i / KB_VAL);
SerialUSB.print(F("Wrote "));
SerialUSB.print(total);
SerialUSB.println(F(" Kb"));
}
}
dataFile.close();
}
However when the data copied appear to be corrupt although they appear at the correct position. See example below
Main BR ENTRANCE ----> Main eRrEoT AnCrne
Main BR entry spots ----> Main bRdeotmyssooss
Other than being horribly inefficient to do it byte by byte, I don't seen any issues other than you are not checking the return value of Wire.endtransmission() which will indicate success/failure.
You can also replace your readAddress() function with a different form of requestFrom()
Note: error checking omitted. I would suggest checking the return values of endTransmission() and reqestFrom() just to verify things are working as intended.
What model Arduino are you using? I tried to compile your sketch for an UNO and "SerialUSB" is not defined.
Compiling for Leonardo I get an error because you are passing a uint16_t to Wire.beginTransmission() and the compiler can't figure out if you mean 'uint8_t' or 'int'.
/Users/john/Documents/Arduino/sketch_may19a/sketch_may19a.ino: In function 'void writeEEPROM(uint16_t, uint16_t, uint8_t)':
sketch_may19a:206:39: error: call of overloaded 'beginTransmission(uint16_t&)' is ambiguous
Wire.beginTransmission(deviceaddress);
^
In file included from /Users/john/Documents/Arduino/sketch_may19a/sketch_may19a.ino:1:0:
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.3/libraries/Wire/src/Wire.h:61:10: note: candidate: void TwoWire::beginTransmission(uint8_t)
void beginTransmission(uint8_t);
^~~~~~~~~~~~~~~~~
/Users/john/Library/Arduino15/packages/arduino/hardware/avr/1.8.3/libraries/Wire/src/Wire.h:62:10: note: candidate: void TwoWire::beginTransmission(int)
void beginTransmission(int);
^~~~~~~~~~~~~~~~~
exit status 1
call of overloaded 'beginTransmission(uint16_t&)' is ambiguous
No, is was supposed to be the version that passes all 5 parameters, but I left off the last one - sorry. You need to use this one to pass a 16 bit memory address
address is of type 'uint16_t' but needs to be cast to uint32_t. Your parameters needs to match up with what the function is expecting. That is how C++ matches function calls to multiple function definitions (aka signatures)