1

Why do I have to burn the code twice to actually burn it on to the Arduino board? I have to execute this command twice - only then is the chip getting programmed.

I'm using this command to burn the program into the Arduino Uno board using a Rasp Pi 3, via SSH from Windows 10.

avr-gcc -mmcu=atmega328 filename.c | avr-objcopy -O ihex -j .text -j .data a.out a.hex | avrdude -C avrdude.conf -v -p atmega328p -c arduino -P /dev/ttyACM* -b 115200 -D -U flash:w:a.hex:i
Greenonline
  • 2,844
  • 7
  • 31
  • 46
user137442
  • 19
  • 1

1 Answers1

6

On a Unix shell, separating commands with the pipe character (|) means “run these commands in parallel, feeding the standard output of each one to the standard input of next one”.

In this context, it makes no sense to use pipes, as both avr-objcopy and avrdude read their data from files, not from stdin. Furthermore, running these in parallel means that avrdude will not have access to a.hex when it starts.

The solution is to run the commands sequentially instead of in parallel. Type and run one command at a time.

Alternatively, if you really want to have everything in a single line, separate the commands with a semicolon (;, meaning “run the next command when the previous one is done”) or, better yet, double ampersand (&& = “run the next command only if the previous one exits successfully”).

Edgar Bonet
  • 39,449
  • 4
  • 36
  • 72
  • correct me, so in the first burn it is the previous a.hex file and while on the second burn it gets the new one. right? – user137442 Sep 10 '21 at 16:57
  • @user137442: Probably something like that, although it's hard to know exactly. Avrdude may be burning the hex file from the previous run of the pipe, or from the one before if the previous run could not build an hex file, or it may fail because it finds an incomplete hex file which is in the process of being written... By starting the three commands in parallel, you are at the mercy of unpredictable, timing-dependent behavior. – Edgar Bonet Sep 10 '21 at 19:11
  • thank you so much Bonet. I tried && and it is working fine in the first burn only. In your answer you said somthing like "In this context, it makes no ......... a.hex when it starts" Can you explain this? – user137442 Sep 11 '21 at 08:37
  • @user137442: Many Unix utilities (`sort`, `split`, `sed`, `head`...) default to processing the data that comes into their standard input and write the results to standard output. These are meant to be combined with pipes. The commands you are using do not work this way so, when using `avr-gcc`, `avr-objcopy` and `avrdude`, it makes no sense to use pipes. – Edgar Bonet Sep 11 '21 at 10:18
  • Thank you. I haven't understood all but i can see where it is going wrong. Thank you so much – user137442 Sep 13 '21 at 11:20