Reading float values from EEPROM reboots Teensy 4.1

jpatrick62

Well-known member
Hello = have an issue I have not been able to track down, hopefully someone can help. I have a Teensy 4.1 which is doing pressure testing and comparing the tested pressure to some set levels - 3 levels in fact. The Teensy then reports the findings via USB to a Windows program. So all went well until I started writing/reading the 3 float values from EEPROM - doing so appears to hang the Watchdog timer and it reboots. The functions to write/read floats to EEPROM are as follows:

Code:
template <class T> int MemWriteValue(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        EEPROM.write(ee++, *p++);
    return i;
}

template <class T> void MemReadTypeValue(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    EEPROM.get(ee, value);
}

template <class T> int MemReadValue(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
        *p++ = EEPROM.read(ee++);
    return i;
}

The EEPROM memory addresses are:
//Low values
int LOW_ADDRESS_0 = 0;
//fast values
int FAST_ADDRESS_0 = 4;
//accurate values
int ACCURATE_ADDRESS_0 = 8;

So each address has 4 bytes for each float. The program uses 3 default initialized float variables that I want to load the EEPROM float values I saved to:

float START_PRESSURE_LEVEL = 1.0;
float FAST_PRESSURE_LEVEL = 2.0;
float ACCURATE_PRESSURE_LEVEL = 3.0;

Now I read the pressure values from EEPROM at the start of the program, and occasionally the user may modify one of the values. Writing to EEPROM does not seem to cause the issue that reading the float value(s) does.
I know the issue is related to the EEPROM memory reads as each time the following function is called (is called once at the beginning of the program to load the stored float values from EEPROM into variables int eh program) the Teensy 4.1 reboots. If I comment out the function, the program runs fine. Here is the function that seems to cause the issue - as you can seen I've tried several of the above EEPROM read functions.

Code:
bool GetPressureSettingsFromMemory()
{
    MemReadTypeValue(LOW_ADDRESS_0, START_PRESSURE_LEVEL);
    _DebugPrint("Start Pressure from memory: ");
    _DebugPrintln(START_PRESSURE_LEVEL);
    MemReadTypeValue(FAST_ADDRESS_0, FAST_PRESSURE_LEVEL);
    _DebugPrint("Fast Pressure from memory: ");
    _DebugPrintln(FAST_PRESSURE_LEVEL);
    MemReadTypeValue(ACCURATE_ADDRESS_0, ACCURATE_PRESSURE_LEVEL);
    _DebugPrint("Accurate Pressure from memory: ");
    _DebugPrintln(ACCURATE_PRESSURE_LEVEL);
    /*
    MemReadValue(LOW_ADDRESS_0, START_PRESSURE_LEVEL);
    _DebugPrint("Start Pressure from memory: ");
    _DebugPrintln(START_PRESSURE_LEVEL);
    MemReadValue(FAST_ADDRESS_0, FAST_PRESSURE_LEVEL);
    _DebugPrint("Fast Pressure from memory: ");
    _DebugPrintln(FAST_PRESSURE_LEVEL);
    MemReadValue(ACCURATE_ADDRESS_0, ACCURATE_PRESSURE_LEVEL);
    _DebugPrint("Accurate Pressure from memory: ");
    _DebugPrintln(ACCURATE_PRESSURE_LEVEL);
    */
}


Oddly, the EEPROM read function debug prints out the correct float values stored in EEPROM before it hangs and then reboots.
 
So couldn't you just use
Code:
EEPROM.get(LOW_ADDRESS_0, START_PRESSURE_LEVEL);

EEPROM.put(LOW_ADDRESS_0, START_PRESSURE_LEVEL);
instead of your template class?
 
Oddly, the EEPROM read function debug prints out the correct float values stored in EEPROM before it hangs and then reboots.
This would indicate that the posted code is not the problem.

How do you know the watchdog timer is responsible for the reboot? What is the rest of your code doing?
 
Can you give us a small but complete program anyone can copy into Arduino IDE and run on Teensy 4.1 to reproduce this problem?

If you really have found a previously unknown bug in the EEPROM library, of course I want to investigate and fix. But I can't even begin an investigation until a complete program is shown to reproduce the problem.
 
Back
Top