Hard fault with EEPROM.get()

I’m reasonably comfortable with C++ and Arduino, but I’m writing my first code for the Photon, using firmware 0.6.0. Almost the first thing my sketch does is load configuration data from EEPROM; but that causes a hard fault (SOS with one blink). Here’s a minimal version of the sketch that demonstrates the problem.

#include <spark_wiring_eeprom.h>

struct EepromImage {
    uint8_t cookie;
    uint8_t addressCount;
    IPAddress ipAddresses[5];
};

void setup() {
    EepromImage image;
    EEPROM.get (0, image);
}

void loop() {

}

Was it something obvious?

Thanks,
Dan Wiebe

maybe it doesn’t like what it is seeing in EEPROM.

And FYI you don’t need this:

#include <spark_wiring_eeprom.h>

anymore, it is part of the basic libraries now.

Try initializing an EepromImage first usig a put() and then comment that out…

struct EepromImage {
    uint8_t cookie;
    uint8_t addressCount;
    IPAddress ipAddresses[5];
};

void setup() {
    EepromImage image;
    EEPROM.put(0, image);  // run me once, then comment me out
    //EEPROM.get (0, image);  // comment me out and then resurrect me once an EepromImage object has been initialized here.
}

void loop() {

}

worked for me…

1 Like

Wow. Seemed to work. Thanks…but I’d like to be able to write self-bootstrapping code that could tell whether the device it ended up on had been configured or not.

Anyway, you got me past the immediate bump. Thanks.

hmmmm… how would a valid struct become extracted from EEPROM if you didn’t initialize that EEPROM location set with the right data in the first place?

Since IPAddress is an object that wants constructing before populating with values, I’d rather go a few steps back and only store the raw octets as uint8_t [5][4] (or HAL_IPAddress[5]).
After that you can construct the IPAddress objects from that array.
Storing objects in EEPROM will always be a problem if you don’t know exactly how the objects are constructed and initialized.
For such actions you’d need to have some serialization/deserialization logic embedded in the class, which I don’t see in IPAddress.

With EEPROM (and other possibly uninitialized structs) having a checksum in addition to your magic number/cookie to tell if the data is valid might be a good idea too.

2 Likes

Don’t really care about validity. I just want EEPROM.get() to shovel bits from EEPROM into the area occupied by the EepromImage structure. Then I’ll check the cookie field to see if it has the right value. If it doesn’t, that means there’s trash in the structure and I need to run the bootstrap code to store a reasonable default in there. If it does, then it’s already been bootstrapped and I go about trying to interpret the rest of the structure.

you could just try an EEPROM flag… look for an “initialization event has happened” flag at say location zero.

This was the real problem. Thank you kindly. I ended up making that EepromImage.ipAddresses field just an array of uint8_t, and then casting it back and forth on load and save.

1 Like