Go Down

Topic: IDE upload to EEPROM (Read 519 times) previous topic - next topic

aweatherguy

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.

1) The standard Uno bootloader (optiboot) doesn't support EEPROM programming.

2) 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:

1) I got it to work and thought others might find it useful.

2) 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.

1) Disable LED flashing during boot by adding "LED_START_FLASHES=0" to the make command line.

2) 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:

Code: [Select]

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):

Code: [Select]

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.

Code: [Select]

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.

Budvar10

Thank you for sharing. I like that somebody start to fight with bootloader's EEPROM programming feature on UNO.
I am using mostly ATmega1284P in my own Arduino and I have the bootloader with this feature but there is 1k minimum boot region so I didn't care about the bootloader size so much.
However, I think that the LED blinking is a very useful indication to be omitted, it will be better to find the space somewhere else.
Anyway, K+ for your investigation.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

krupski

Thank you for sharing. I like that somebody start to fight with bootloader's EEPROM programming feature on UNO.
I am using mostly ATmega1284P in my own Arduino and I have the bootloader with this feature but there is 1k minimum boot region so I didn't care about the bootloader size so much.
However, I think that the LED blinking is a very useful indication to be omitted, it will be better to find the space somewhere else.
Anyway, K+ for your investigation.
The blinking LED on pin 13 is the worst thing that the bootloader could do. It prevents being able to use the pin 13 for anything that needs an output. Connect a motor driver to pin 13? The motor will start and stop without being commanded to do so because of the stupid "blink" code.

I re-wrote Optiboot to support both flash and eeprom programming, and it's 468 bytes in size (i.e. fits in the 512 byte space).

New versions of the IDE seem to come out weekly, yet support for features that EVERYONE on here talks about, needs and wants seem to be ignored.

I've moved away from the IDE and program my Arduino boards with GCC and a Makefile. I can do anything I want without being restricted by the Arduino IDE.

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

aweatherguy

Well, as evidenced by krupski's post, LEDs are a very touchy (almost religious) topic. Some folks have to have 'em. Others hate them with a passion. To each his own I figure. As far as I'm concerned, both opinions above are equally valid :)

Just a quick update on EEPROM auto-upload. I've also got a custom board with ATmega32U4 which looks for the most part like a LilyPad USB. The upload process for that is quite different, but the bootloader (Caterina) supports EEPROM. I managed to get that working by adding a local platform.txt file which contaings a copy of the avrdude section from the standard platform.txt file. I simply renamed the section from "avrdude" to "eepdude" (or whatever) and added the extra -U argument to the upload.pattern and program.pattern entries. Works like a charm.

Budvar10

The blinking LED on pin 13 is the worst thing that the bootloader could do. It prevents being able to use the pin 13 for anything that needs an output. Connect a motor driver to pin 13? The motor will start and stop without being commanded to do so because of the stupid "blink" code.
Yes, I agree with your opinion. I didn't look to the problem from this view. I'd never used pin 13 for such purpose so never had a problem. Anyway, it is not problem for me also for you to customize the bootloader for specific use and for geneneral purpose I would stay with "stupid blink" code.

I re-wrote Optiboot to support both flash and eeprom programming, and it's 468 bytes in size (i.e. fits in the 512 byte space).

New versions of the IDE seem to come out weekly, yet support for features that EVERYONE on here talks about, needs and wants seem to be ignored.

I've moved away from the IDE and program my Arduino boards with GCC and a Makefile. I can do anything I want without being restricted by the Arduino IDE.
I was after your effort with the bootloader - e.g. as you omitted saving bytes to buffer and pushed it directly to the programming memory. K+ :)

I am still using IDE (my laziness?) but of course, I'm using also avrdude directly like for EEPROM programming as I wrote above. It is just rarely in compare with flash programming, so I am not long for the option in IDE as much.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

krupski

Well, as evidenced by krupski's post, LEDs are a very touchy (almost religious) topic. Some folks have to have 'em. Others hate them with a passion. To each his own I figure. As far as I'm concerned, both opinions above are equally valid :)
I LOVE LEDs. Pretty colors, all blinking and shining....  :)

But what I don't like is an Arduino IO pin made useless by the bootloader making the "Pin 13" LED blink.

We've all seen posts by n00bs asking why their motor starts and stops at random only to find they have it connected to pin 13.

'Nuf said?
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

krupski

#6
Oct 09, 2017, 04:16 am Last Edit: Oct 09, 2017, 04:18 am by krupski
I am still using IDE (my laziness?) but of course, I'm using also avrdude directly like for EEPROM programming as I wrote above. It is just rarely in compare with flash programming, so I am not long for the option in IDE as much.
I use the IDE a lot myself believe it or not.  I use version "1.0.6" which is a heavily hacked 1.0.5.

It's got features I need (unlike the stock IDE)

(click pic for larger)

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

krupski

#7
Oct 09, 2017, 04:23 am Last Edit: Oct 09, 2017, 04:25 am by krupski
I am still using IDE (my laziness?) but of course, I'm using also avrdude directly like for EEPROM programming as I wrote above. It is just rarely in compare with flash programming, so I am not long for the option in IDE as much.
Another pic: The serial monitor. Note the control buttons on the bottom: Left to right:

Close window, Autoscroll ON/OFF, Linewrap ON/OFF, DTR (reboot Arduino), CLS (clear display), CR/LF/CRLF setecl, Serial font size and Serial baud rate (all operate in real time and saved in Preferences (no need to restart the IDE).

(click pic for larger)

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Budvar10

@krupski
Mr. is gourmet. :) I am still using IDE 1.0.6 as is, but with updated toolchain of course.
Could you offer your modified IDE for testing if it is easy usable somehow for Lnx or Win (u have Mac as I see)?
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

krupski

@krupski
Mr. is gourmet. :) I am still using IDE 1.0.6 as is, but with updated toolchain of course.
Could you offer your modified IDE for testing if it is easy usable somehow for Lnx or Win (u have Mac as I see)?
I'm running Linux, not a Mac. I just use a "Mac-like" theme.

I also run a separate toolchain (AVR-GCC 6.3.0) compiled from source.

For my hacked IDE, I can give you my source (you would need to compile it with ant).

How would be a good way to send you the source? Email?


Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Coding Badly

How would be a good way to send you the source? Email?
I believe Personal Messages can have attachments.


Budvar10

#11
Oct 11, 2017, 10:14 am Last Edit: Oct 11, 2017, 10:16 am by Budvar10
I'm running Linux, not a Mac. I just use a "Mac-like" theme.
:)

You have a PM. Thanks.

@aweatherguy
Sorry for OT.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

Go Up