OK, now that (I think) I fixed my I2C issues, I'm seeing odd behavior read/writing data to the NVRAM (Adesto RM24EP32). If I write values of zero to the NVRAM a subsequent read returns zeros. But, if I write non-zero numbers to the NVRAM, it returns either 0, 4294967295.////// or -4294967295.////// I'm sure I'm missing something simple.
Any help is appreciated!
And here's the result:
Any help is appreciated!
Code:
#include <i2c_t3.h>
//#include <Wire.h>
#define DEBUGNVRAM
const int dblVal = 64;
const int floatVal = 32;
//const int intVal = 32;
//const int shortVal = 16;
//const int byteVal = 8;
const int NVRAM_I2C_ADDR = 0x53; // NVRAM address on the I2C bus
const int NVRAM_ADDR = 0; // NVRAM register start address
const int TRIPA_ADDR = NVRAM_ADDR; // Tripmeter A - float
const int TRIPB_ADDR = TRIPA_ADDR + floatVal; // Tripmeter B - float
const int LAT_ADDR = TRIPB_ADDR + floatVal; // Latitude - double
const int LON_ADDR = LAT_ADDR + dblVal; // Longitude - double
float f_TripA, f_TripB;
double longitude, latitude;
void setup() {
Serial.begin(115200);
delay(4000);
#ifdef DEBUGNVRAM
Serial.println(F("Start I2C"));
#endif
// Start the I2C
Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_EXT, I2C_RATE_400);
// Wire.begin();
Serial.println(F("\nEnd of setup()\n"));
}
void loop() {
writeNVRAM(0.0,0.0,0.0,0.0);
readNVRAM();
writeNVRAM(999.9,8888.8,45.123456,-121.654321);
readNVRAM();
writeNVRAM(1111.1,22.2,-15.111111,85.222222);
readNVRAM();
Serial.println(F("\n\n"));
delay(5000);
}
void readNVRAM() {
#ifdef DEBUGNVRAM
Serial.println(F("Reading NVRAM values"));
#endif
f_TripA = NVRAM_ReadFloat(TRIPA_ADDR); // Read the stored Tripmeter A value so we can display it
f_TripB = NVRAM_ReadFloat(TRIPB_ADDR); // Read the stored Tripmeter B value so we can display it
latitude = NVRAM_ReadDouble(LAT_ADDR); // Read the stored Latitude value so we can display it
longitude = NVRAM_ReadDouble(LON_ADDR); // Read the stored Longitude value so we can display it
Serial.print(F("TripA: "));
Serial.println(f_TripA,2);
Serial.print(F("TripB: "));
Serial.println(f_TripB,2);
Serial.print(F("Lat: "));
Serial.println(latitude,6);
Serial.print(F("Lon: "));
Serial.println(longitude,6);
Serial.println(F("----------------------"));
}
void writeNVRAM(float tempTripA,float tempTripB,double tempLat,double tempLon) {
#ifdef DEBUGNVRAM
Serial.println(F("Writing new NVRAM values"));
#endif
Serial.print(F("tempTripA: "));
Serial.println(tempTripA,2);
Serial.print(F("tempTripB: "));
Serial.println(tempTripB,2);
Serial.print(F("tempLat: "));
Serial.println(tempLat,6);
Serial.print(F("tempLon: "));
Serial.println(tempLon,6);
NVRAM_WriteFloat(TRIPA_ADDR, tempTripA);
NVRAM_WriteFloat(TRIPB_ADDR, tempTripB);
NVRAM_WriteDouble(LAT_ADDR, tempLat);
NVRAM_WriteDouble(LON_ADDR, tempLon);
Serial.println(F("----------------------"));
}
// Reads a 4-byte float value from the NVRAM RAM registers
float NVRAM_ReadFloat(int valAddr) {
float value;
byte *byteArray = (byte *) &value;
// Set the register pointer
Wire.beginTransmission(NVRAM_I2C_ADDR);
Wire.write(valAddr);
Wire.endTransmission();
// Read 4 bytes
Wire.requestFrom(NVRAM_I2C_ADDR, 4);
byteArray[3] = Wire.read();
byteArray[2] = Wire.read();
byteArray[1] = Wire.read();
byteArray[0] = Wire.read();
return value;
}
// Writes a 4-byte float value to the NVRAM RAM registers
void NVRAM_WriteFloat(int valAddr, float value) {
Wire.beginTransmission(NVRAM_I2C_ADDR);
Wire.write(valAddr);
// Write 4 bytes
byte *byteArray;
byteArray = (byte *) &value;
Wire.write(byteArray[3]);
Wire.write(byteArray[2]);
Wire.write(byteArray[1]);
Wire.write(byteArray[0]);
Wire.endTransmission();
}
// Reads a 8-byte double value from the NVRAM RAM registers
float NVRAM_ReadDouble(int valAddr) {
double value;
byte *byteArray = (byte *) &value;
// Set the register pointer
Wire.beginTransmission(NVRAM_I2C_ADDR);
Wire.write(valAddr);
Wire.endTransmission();
// Read 8 bytes
Wire.requestFrom(NVRAM_I2C_ADDR, 8);
byteArray[7] = Wire.read();
byteArray[6] = Wire.read();
byteArray[5] = Wire.read();
byteArray[4] = Wire.read();
byteArray[3] = Wire.read();
byteArray[2] = Wire.read();
byteArray[1] = Wire.read();
byteArray[0] = Wire.read();
return value;
}
// Writes a 8-byte double value to the NVRAM RAM registers
void NVRAM_WriteDouble(int valAddr, double value) {
Wire.beginTransmission(NVRAM_I2C_ADDR);
Wire.write(valAddr);
// Write 8 bytes
byte *byteArray;
byteArray = (byte *) &value;
Wire.write(byteArray[7]);
Wire.write(byteArray[6]);
Wire.write(byteArray[5]);
Wire.write(byteArray[4]);
Wire.write(byteArray[3]);
Wire.write(byteArray[2]);
Wire.write(byteArray[1]);
Wire.write(byteArray[0]);
Wire.endTransmission();
}
And here's the result:
Code:
Start I2C
End of setup()
Writing new NVRAM values
tempTripA: 0.00
tempTripB: 0.00
tempLat: 0.000000
tempLon: 0.000000
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: 0.000000
----------------------
Writing new NVRAM values
tempTripA: 999.90
tempTripB: 8888.80
tempLat: 45.123456
tempLon: -121.654321
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: -4294967295.//////
----------------------
Writing new NVRAM values
tempTripA: 1111.10
tempTripB: 22.20
tempLat: -15.111111
tempLon: 85.222222
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: -4294967295.//////
Lon: 4294967295.//////
----------------------
Writing new NVRAM values
tempTripA: 0.00
tempTripB: 0.00
tempLat: 0.000000
tempLon: 0.000000
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: 0.000000
----------------------
Writing new NVRAM values
tempTripA: 999.90
tempTripB: 8888.80
tempLat: 45.123456
tempLon: -121.654321
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: -4294967295.//////
----------------------
Writing new NVRAM values
tempTripA: 1111.10
tempTripB: 22.20
tempLat: -15.111111
tempLon: 85.222222
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: -4294967295.//////
Lon: 4294967295.//////
----------------------
Writing new NVRAM values
tempTripA: 0.00
tempTripB: 0.00
tempLat: 0.000000
tempLon: 0.000000
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: 0.000000
----------------------
Writing new NVRAM values
tempTripA: 999.90
tempTripB: 8888.80
tempLat: 45.123456
tempLon: -121.654321
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: 0.000000
Lon: -4294967295.//////
----------------------
Writing new NVRAM values
tempTripA: 1111.10
tempTripB: 22.20
tempLat: -15.111111
tempLon: 85.222222
----------------------
Reading NVRAM values
TripA: 0.00
TripB: 0.00
Lat: -4294967295.//////
Lon: 4294967295.//////
----------------------
And repeat...