I've been using EEPROM to store tables for sketches to save space in flash. It's kind of a pain however, because the only way to get this data into EEPROM right now is with a separate manual step either using an ISP or a special sketch.
Building a sketch with EEMEM data creates a ".eep" file containing the EEPROM data but there are two issues preventing the upload of this file with stock Arduino IDE and optiboot loader. I was working with the Uno but this probably applies to other boards as well.
-
The standard Uno bootloader (optiboot) doesn't support EEPROM programming.
-
The IDE doesn't issue the proper command line options to avrdude to program EEPROM from the ".eep" file.
Here's a solution which automatically programs both flash and EEPROM for sketches containing EEMEM data. It requires a modified optiboot loader and a tweak to the "programmers.txt" file. The modified optiboot is exactly 512 bytes in size so you don't have to give up any flash space.
I'm posting this for two reasons:
-
I got it to work and thought others might find it useful.
-
Someone may have suggestions or better ways to do this.
The hack is in two parts.
Part I -- Optiboot modification
Maybe this belongs on an optiboot discussion forum, but since it's required for the overall change to work, I'm including it here. The challenge is adding EEPROM support to optiboot without exceeding a 512-byte size limit.
Defining the SUPPORT_EEPROM macro during complilation of optiboot turns on EEPROM programming support. I added a new target in the makefile to accomplish this. If this is the only change, then optiboot swells to 562 bytes and will no longer fit in the desired 512-byte space. Two changes will bring it back within this limit.
-
Disable LED flashing during boot by adding "LED_START_FLASHES=0" to the make command line.
-
Comment out the two places where watchdog timeout is set to 16 msec. This gives up the Adaboot no-wait behavior but is necessary to get the size down to 512 bytes.
A new target added to the optiboot makefile can be used to enable EEPROM support. Other than a new name, the only change is setting the DEFS macro. I'm sure there are other (perhaps better) ways to do this. Here's the target I used to enable EEPROM support on the Uno:
atmega328_ee: TARGET = atmega328_ee
atmega328_ee: MCU_TARGET = atmega328p
atmega328_ee: CFLAGS += $(COMMON_OPTIONS)
atmega328_ee: AVR_FREQ ?= 16000000L
atmega328_ee: LDSECTIONSÂ = -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe
atmega328_ee: $(PROGRAM)_atmega328.hex
atmega328_ee: $(PROGRAM)_atmega328.lst
atmega328_ee: DEFS=-DSUPPORT_EEPROM
This next part is not mandatory but may be of interest to Windows people. Linux folks can skip to Part II.
If you're building this with Cygwin on Windows, the build of "baudcheck.tmp.sh" generates CR-LF line endings and bash barfs on this. Modifying the makefile rule for baudcheck as follows is one fix (as an aside, the change would be compatible on Linux builds):
baudcheck: FORCE
- @$(CC) --version
- @$(CC) $(CFLAGS) -E baudcheck.c -o baudcheck.tmp
- sed 's/\r//' baudcheck.tmp >baudcheck.tmp.sh
- rm -f baudcheck.tmp
- @$(SH) baudcheck.tmp.sh
These changes are what's required to get a version of optiboot that supports EEPROM programming and still fits (exactly) within a 512-byte boot space.
Part II -- Arduino IDE Changes
There is only one change here -- adding a new section in the programmers.txt file. This adds options on the avrdude command line to program both Flash and EEPROM during the upload. It's kind of frustrating that files such as programmers.txt don't have any formal documentation (not that I could find, anyway), but I figured this out from some other examples.
witheeprom.name=Arduino (with EEPROM)
witheeprom.communication=serial
witheeprom.protocol=arduino
witheeprom.program.protocol=arduino
witheeprom.program.tool=avrdude
witheeprom.program.extra_params=-P{serial.port} -D -U eeprom:w:{build.path}/{build.project_name}.eep:i
Of course, you must select this programmer in the "Tools..Programmer..." GUI menu and use the "File...Upload Using Programmer" menu pick. Sketches without EEMEM data may not generate a ".eep" file and using this programmer in that case may cause an upload failure.
Summary
Although you have to give up LED flashing during boot and live with a longer boot delay (1 second versus 16 msec), this hack will allow you to define EEMEM in a sketch and have it automatically programmed into the MCU during upload by the Arduino IDE.