Electronic – Program AVR EEPROM directly from C source

attinyavrceeprom

When you include the following code in an AVR C source, you can apparently directly program the fuses, without the need for an extra command or .hex file:

#include <avr/io.h>

FUSES = {
        .low =          LFUSE_DEFAULT ,
        .high =         HFUSE_DEFAULT ,
        .extended =     EFUSE_DEFAULT ,
};

Is there a similar trick to program values in EEPROM?

I have checked /usr/lib/avr/include/avr/fuse.h where I can find some comments about a macro, but I can't find a similar comment in /usr/lib/avr/include/avr/eeprom.h and interpreting the preprocessor stuff is a bit out of my league.

It would be really handy if I could include default EEPROM values in the C source code. Anyone know how to accomplish that?

edit1:

This FUSES trick is only executed at ISP-time, not at RUN-time. So there are no fuses being programmed in the resulting assembly code in the controller. Instead the programmer automatically cycles through an extra FUSES programming cycle.

edit2:

I use the avr-gcc and avrdude toolchain on Linux.

Best Answer

With avr-gcc the EEMEM macro can be used on the definition of a variable, see the libc docs and an example here:

#include <avr/eeprom.h>
char myEepromString[] EEMEM = "Hello World!";

declares the array of characters to reside in a section named ".eeprom" which after compilation tells the programmer that this data is to be programmed to the EEPROM. Depending on your programmer software, you may need to explicitly give the name of the ".eep"-file created during the build process to the programmer, or it may implicitly find it by itself.