Hello, when I try to migrate a project from Teensy 3.2 to Teensy 4.0, the EEPROM is somehow not keeping correctly the stored values.
I use the simple functions EEPROM.update() and EEPROM.read() to store 20 uint16_t values of configuration that can be changed via Modbus ...
On Teensy 3.2 the code worked without any problems. But on Teensy 4.0 everything works properly only until I turn the power off and on again . Teensy is powered externally via 5VDC on the VIN pin... I double check the stored values by printing the written values, then printing the values directly read from EEPROM and all values are correct as long as Teensy is running without power interruption.
When the Teensy 4.0 is powered on, some values in the EEPROM are overwritten, but most remain correct...
I also tried to software reset the Teensy via writing to SCB_AIRCR = 0x05FA0004; and the EEPROM then holds all values correctly ... But only until I turn power off.
I also tried to change the EEPROM address from 0 - 40 to 100-140 , to check if there's any problem with the flash region, but it acts the same...
However, if I do not write anything to the EEPROM, the values in the EEPROM will not change even after the power is turned off.
Is there something I forgot to do before or after writing to EEPROM to make sure the values are stored correctly in Teensy 4.0? Or is it better to use EEPROM.put() and EEPROM.get() ?
Here is an example part of the code with functions I'm calling from loop() to store the uint16_t variables I'm getting from Modbus, but code have some interrupt routines too...
I use the simple functions EEPROM.update() and EEPROM.read() to store 20 uint16_t values of configuration that can be changed via Modbus ...
On Teensy 3.2 the code worked without any problems. But on Teensy 4.0 everything works properly only until I turn the power off and on again . Teensy is powered externally via 5VDC on the VIN pin... I double check the stored values by printing the written values, then printing the values directly read from EEPROM and all values are correct as long as Teensy is running without power interruption.
When the Teensy 4.0 is powered on, some values in the EEPROM are overwritten, but most remain correct...
I also tried to software reset the Teensy via writing to SCB_AIRCR = 0x05FA0004; and the EEPROM then holds all values correctly ... But only until I turn power off.
I also tried to change the EEPROM address from 0 - 40 to 100-140 , to check if there's any problem with the flash region, but it acts the same...
However, if I do not write anything to the EEPROM, the values in the EEPROM will not change even after the power is turned off.
Is there something I forgot to do before or after writing to EEPROM to make sure the values are stored correctly in Teensy 4.0? Or is it better to use EEPROM.put() and EEPROM.get() ?
Here is an example part of the code with functions I'm calling from loop() to store the uint16_t variables I'm getting from Modbus, but code have some interrupt routines too...
Code:
// EEPROM Addresses for signature code and version of firmware
#define EE_ADDR_OFFSET 100 // EEPROM address offset for debug
#define EE_ADDR_MODEL_TYPE EE_ADDR_OFFSET+0 // WORD
#define EE_ADDR_MODEL_SERIAL_NUMBER EE_ADDR_OFFSET+2 // WORD
#define EE_ADDR_FW_VERSION EE_ADDR_OFFSET+4 // WORD
// EEPROM Addresses for config
#define EE_ADDR_modbus_ID EE_ADDR_OFFSET+6 // WORD
#define EE_ADDR_modbus_Speed EE_ADDR_OFFSET+8 // WORD
#define EE_ADDR_modbus_Format EE_ADDR_OFFSET+10 // WORD
// etc...
volatile uint16_t modbusID = 0;
// EEPROM
// read a unsigned int (two bytes) value from eeprom
uint16_t eeprom_readInt(uint16_t address)
{
return EEPROM.read(address) | (EEPROM.read(address + 1) << 8);
#if defined(__IMXRT1062__) // Teensy 4.0
asm("DSB");
#endif
}
// Write a unsigned int (two bytes) value to eeprom
void eeprom_writeInt(uint16_t address, uint16_t value)
{
__disable_irq();
EEPROM.write(address, value & 0xFF); // LSB
EEPROM.write(address + 1, value >> 8); // MSB
#if defined(SERIAL_DEBUG)
Serial.printf("EEwr a: %u w: %u r: %u\n", address, value, eeprom_readInt(address));
// print all used EEPROM words
for (int p=EE_ADDR_OFFSET+0; p <EE_ADDR_OFFSET+42; p=p+2)
{
Serial.printf("%2u:%5u|", p, EEPROM.read(p) | EEPROM.read(p + 1) << 8);
}
Serial.println();
#endif
__enable_irq();
#if defined(__IMXRT1062__) // Teensy 4.0
asm("DSB");
#endif
}
void eeprom_updateInt(uint16_t address, uint16_t value)
{
__disable_irq();
EEPROM.update(address, value & 0xFF); // LSB
EEPROM.update(address + 1, value >> 8);
#if defined(SERIAL_DEBUG)
Serial.printf("EEupd a: %u w: %u r: %u\n", address, value, eeprom_readInt(address));
// print all used EEPROM words
for (int p=EE_ADDR_OFFSET+0; p <EE_ADDR_OFFSET+42; p=p+2)
{
Serial.printf("%2u:%5u|", p, EEPROM.read(p) | EEPROM.read(p + 1) << 8);
}
Serial.println();
#endif
__enable_irq();
#if defined(__IMXRT1062__) // Teensy 4.0
asm("DSB");
#endif
}
void loop()
{
// check changes made via ModBus - if values are valid, save them in EEPROM
if ((holdingRegs[MODBUS_ID] != modbusID) && (holdingRegs[MODBUS_ID] > 0) && (holdingRegs[MODBUS_ID] < 248))
{
modbusID = holdingRegs[MODBUS_ID];
eeprom_updateInt(EE_ADDR_modbus_ID, modbusID);
}
}