I have an application that uses EEPROM to store a lot of configuration data. I also, eventually, want to run the program without a boot loader, so it starts up immediately on reset.
For development I currently use the bootloader to program the FLASH using the serial I/F, and run a separate program to intialize the EEPROM. My questions are:
Is there a way to easily program the EEPROM over the serial port, from some kind of compiled image?
When I use a USBTiny to program the FLASH (without the bootloader), the EEPROM seems to get erased. So, pre-initializing the FLASH with the separate program does not work, as the EEPROM gets erased when the FLASH is re-programmed.
Are there other options for programming FLASH and EEPROM I should know about?
I know AVRDude can write a file directly to EEPROM. I don't know for sure if that works with Arduino in any way because I've never tried it.
There is a flag you can give AVRDude so that doesn't happen.
I just googled a little bit to try to find what I'm talking about. Sorry I don't remember specifics. But this is where I'd start if I was looking tonight.
The -e option over-rides the chip-erase before programming, but says the chip-erase is required, except for ATxMega chips, and I'm using a 328.
-e Causes a chip erase to be executed. This will reset the contents of the flash ROM and EEPROM to the value ‘0xff’, and clear all lock bits. Except for ATxmega devices which can use page erase, it is basically a prerequisite command before the flash ROM can be reprogrammed again. The only exception would be if the new contents would exclusively cause bits to be programmed from the value ‘1’ to ‘0’. Note that in order to reprogram EERPOM cells, no explicit prior chip erase is required since the MCU provides an auto-erase cycle in that case before programming the cell.
avrdude can, indeed program the EEPROM from a file, using the -U option:
-U memtype:op:filename[:format]
setting memtype to "eeprom". So, I can, in theory, create a HEX file for the EEPROM, then use avrdude from the command line to load it into the chip.
The AVR micros have an EESAVE fuse that can be programmed to preserve the eeprom contents. See the avrlibc docs for eeprom.h, you can define eeprom variables that can be uploaded to the eeprom in the micro. The IDE build-system will extract the .eeprom section into an *.eep file you can upload. However the bootloaders do not support eeprom uploads, you need to do it via ICSP.
I'd probably also consider using a technique similar to that used some over the air uploads. A tiny part of the sketch is dedicated to watching for update attempts. Say, something in setup(). If an input pin is low when setup() runs, read the serial port and and write the data received into the EEPROM. For a typical AVR where the RAM is larger the EEPROM, you may even be able to read the whole data into RAM first, check it (somehow) and write it to the EEPROM.
Avrdude and also optiboot support EEPROM uploading via serial. However, the optiboot is optimized to size to be less than 512B for 328P, so this feature is excluded.
I'm using this for ATmega1284P. The size of the bootloader is under 1kB. The smallest boot region for ATmega1284P is 1kB so why not.