4

I have been working on an arduino program, which has gotten rather large. I am close to hitting the 32k limit on Flash for my ATmega328p. I would like lower my program size by saving some of my many string literals into EEPROM.

All of the example programs on the EEPROM page suggest loading them in at runtime, in which case they would take up flash memory anyways! What I am wondering, is if there is a way to write to EEPROM from my PC, THEN load the arduino program which will access those values. Do I need some external hardware to do this?

Thanks for your help!

-sam

2

The simplest way would be to have two sketches.

One that will store all the strings into EEPROM.

After that you can upload the second actual sketch that uses the values stored in EEPROM.

You only need to upload the first sketch if any of the strings change.

There is a way to upload a hex directly to the EEPROM using avrdude, but you'd have to use the commandline, and also have some way of generating this hex file containing all these strings.

2

I think here is a confusion about the three types of memory. EEPROM, FLASH and SRAM are completely separate. EEPROM is like your file storage. FLASH contains you program and static values. SRAM is the memory the program is working with.

Static values are loaded from FLASH into SRAM upon start unless you declare your static content not to load automatically by using the according Macro PROGMEM. In that case it does not eat up your SRAM until you manually read it from FLASH into SRAM. #include <avr/pgmspace.h> is used for this (pgmspace.h).

EEPROM is not loaded automatically and therefore must be read explicitly. #include <avr/eeprom.h> contains all the functionality (eeprom.h).

I'm not sure about the Arduino IDE but with avrdude you can program the EEPROM separately without having the values been predefined in your program. The third example in the AVRdude manual shows how to flash the program, eeprom and fuses at once.

  • thanks I'll check out the avrdude example! that may be what I am looking for – riverrun Aug 11 '16 at 12:59
2

You don't need two sketches:

#include <avr/eeprom.h>

char e2str[] EEMEM = "Hello, world!\n";

void setup()
{
    Serial.begin(9600);
    uint8_t *ptr = reinterpret_cast<uint8_t *>(e2str);
    while (true)
    {
        char c = eeprom_read_byte(ptr++);
        if (c == '\0') break;
        Serial.write(c);
    }
}

void loop() {}

Compile (Sketch>Verify/Compile) the source above and you should find file named <sketch-name>.ino.eep in project's build directory.

For Arduino Nano, you can just run avrdude with -U eeprom:w:<path-to-eep-file>:i option before Sketch>Upload.

For Arduino Uno, however, you must first program it's EEPROM using programmer, since Optiboot doesn't support writing to EEPROM. Or you can burn it with Arduino Nano bootloader once (Tools>Board>Arduino Nano, Tools>Processor>ATmega328, Tools>Burn Bootloader) and then use the procedure for Arduino Nano. (Arduino Uno and Nano bootloaders use different baud rates).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.