Is there a way to initialize a part of the EEPROM at the time a sketch is uploaded to the Arduboy? Ideally I would like to do this via the Arduino IDE, which I use to upload a sketch to the Arduboy.
It can be done via avrdude (the program the Arduino IDE defers to for uploading) with the right command line arguments.
The avrdude executable should be found in the folder located at:
<Arduino IDE installation location>/hardware/tools/avr/bin/
(It would be
avrdude.exe on Windows and presumably just
avrdude on Mac & Linux.)
The avrdude command line argument reference is here:
From a brief look, I think you would need to use the
-U option and specify
eeprom as the
If I have read this correctly, an argument of
-U eeprom:w:eeprom.data:r would copy the raw contents of the file
eeprom.data to the device’s EEPROM.
I believe there may also be a way to store variables in EEPROM using compiler-specific attributes to place the variables in the
.eeprom section of the output
.elf file (which is generated prior to the final
.hex file) but I have never seen this attempted and don’t know the specifics.
I’m only aware of this possibility because I’ve read the documentation for
<avr/eeprom.h> which documents that there is a macro
EEMEM defined as
I know nothing of the specifics or potential caveats, hence I’d advise against this approach in favour of the more modular and better-defined approach using avrdude.
Thanks. Is the EEPROM, outside the first 16 bytes, set to a particular value from the factory? All the addresses I am reading seem to hold 255 as their value.
Note that EEPROM has a limited number of write cycles. Unless you have a good reason to change it, it’s better to only write the areas you plan to use.
It’s also better to do a read of the location you plan to write and only write it if the value would change. The Arduino EEPROM.update() and EEPROM.put() functions will do this. I believe writing using avrdude is “blind” and always writes all specified locations.
EEPROM is “erased” from the factory, so all values will be 0xFF (255 decimal). The Arduboy and Arduboy2 libraries assume the first 16 bytes reserved for system use will be all 0xFF when shipped to the customer.
Thanks for pointing out the limited write cycles. I am using the EEPROM only for application configuration options. The 100k write cycles should be sufficient for this need.