How to WRITE to EEPROM - reading works

Hey Guys!

I am trying to write an eeprom.hex file to the eeprom of an arduino mini, but for some reason it fails. WHY?

When I read from the eeprom using the following shell script:

OUT_FNAME=EEPROM_h.hex
SERIAL_PORT=/dev/tty.usbserial-AB0JTYB9
AVR_CHIP=atmega328p
PROGRAMMER=arduino

avrdude -c $PROGRAMMER -P $SERIAL_PORT -p $AVR_CHIP -U eeprom:r:$OUT_FNAME:h

it works fine and I get the file EEPROM_h.hex.

However, when I try to write the exact same file (unaltered) back to the EEPROM with the script:

OUT_FNAME=EEPROM_h.hex
SERIAL_PORT=/dev/tty.usbserial-AB0JTYB9
AVR_CHIP=atmega328p
PROGRAMMER=arduino

avrdude -c $PROGRAMMER -P $SERIAL_PORT -p $AVR_CHIP -U eeprom:w:$OUT_FNAME:h

it fails.

The output I get is:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: reading input file "EEPROM_h.hex"
avrdude: fileio: invalid operation=0
avrdude: read from file 'EEPROM_h.hex' failed

avrdude: safemode: Fuses OK (H:00, E:00, L:00)

avrdude done.  Thank you.

What am I doing wrong?

FYI: The final goal is to prep a hex file using a cpp program, which then contains some fixed parameters. But if writing back the original file already fails, I suspect that I did not get the actual writing command right.

However, I was looking at the avrdude user-manual to prep my script.

Any advice is very welcome.

The arduino bootloader does not support eeprom (at least, not optiboot as used on Uno.)
(It doesn’t look like you’re getting that far, actually. Don’t know why.)

Thanks for the reply.

So if I understand you right, then you imply that I cannot directly write to the EEPROM via Serial.
(Though I thought I saw also some examples doing that. But can’t find it anymore.)

Anyhow. So take your hint, I also tried writing to the EEPROM using a USBtiny ISP programmer:

OUT_FNAME=EEPROM_h.hex
AVR_CHIP=atmega328p
PROGRAMMER=usbtiny

avrdude -v -c $PROGRAMMER -p $AVR_CHIP -U eeprom:w:$OUT_FNAME:h

And I even tried using the avrdude from the Arduino IDE with its avrdude.conf file using the -C flag.

But also via ISP I get the same error (used the -v verbose flag here):

hostname:shell_scripts$ ./AVRdude_Write_EEPROM_ISP.sh 
============ My AVR-EEPROM Reader ============

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "avrdude.conf"
         User configuration file is "<local path>/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : usbtiny
avrdude: usbdev_open(): Found USBtinyISP, bus:device: 020:006
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : USBtiny
         Description     : USBtiny simple USB programmer, https://learn.adafruit.com/usbtinyisp
avrdude: programmer operation not supported

avrdude: Using SCK period of 10 usec
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DE
avrdude: safemode: efuse reads as FD
avrdude: reading input file "EEPROM_h.hex"
avrdude: fileio: invalid operation=0
avrdude: read from file 'EEPROM_h.hex' failed

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DE
avrdude: safemode: efuse reads as FD
avrdude: safemode: Fuses OK (E:FD, H:DE, L:FF)

avrdude done.  Thank you.

Is ‘funny’, coz it complains for the .conf file, but it cant find the USBtiny with the .conf file. (Perhaps the avrdude from the Arduino IDE is slightly different?)

However, the questions are problably, what the following Errors mean exactly:

avrdude: programmer operation not supported
and
avrdude: fileio: invalid operation=0

I am pretty sure there is a way to write directly to the EEPROM using avrdude, but I am still not sure what the problem is.
My Arduino Mini Hardware?
The avrdude command?
The fileformat of the .hex file (there is the :i and :h option when reading/writing a hex file).

Again… any advice and/or a working example command for reading and writing back the EEPROM to the AVR chip would be very much appreciated.

UPDATE:

Not there yet, but one step further.

Aparently the :h format is only valid on output. I have missed that info, as it is mentioned in the description for the :d format only (see [1])

So I tried using the Intel Hex format using :i as well as the raw format using :r and there it reads, and it seems to write, BUT I get a verification error:

hostname:Shell_Scripts$ ./AVRdude_Write_EEPROM.sh 
============ My AVR-EEPROM Reader ============

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: reading input file "EEPROM_raw.hex"
avrdude: writing eeprom (1024 bytes):

Writing | ################################################## | 100% 3.55s

avrdude: 1024 bytes of eeprom written
avrdude: verifying eeprom memory against EEPROM_raw.hex:
avrdude: load data eeprom data from input file EEPROM_raw.hex:
avrdude: input file EEPROM_raw.hex contains 1024 bytes
avrdude: reading on-chip eeprom data:

Reading | ################################################## | 100% 1.49s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x004c
         0x1e != 0x2a
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK (H:00, E:00, L:00)

avrdude done.  Thank you.

Not sure yet if it did actually write correctly, because the program I wrote to create the hex file with the actual infos I want, creates a standard hex file (using the :h format), which can apparently not be used to write.
I guess I need to rewrite my hex generator to create a hex file using the Intel Hex format. (Which has a less simple structure unfortunately.)

Anyway, I just wanted to leave that update here, in case someone else is also struggling.
Note: The d, h, o and b format can ONLY be used for reading from the EEPROM!
See [1]

[1] AVRDUDE: 2.1 Option Descriptions

Here's a sketch that you can load into your Arduino that will allow access to THAT Arduino's EEPROM using standard programming tools like AVRDude.

It's based on ArduinoISP, but with all the SPI code removed and replaced with "self-access." Obviously it can't program flash or fuses. I think it's only useful for manipulating EEPROM (and maybe "poking around.")

Tested on an Uno.