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
3
  • 1
    Please post any messages you get from the burning process (amend your question to do this). Commented Sep 9, 2021 at 7:34
  • why do you use | between the commands? Commented Sep 9, 2021 at 15:06
  • to execute those three separate command in one single time. @Juraj Commented Sep 10, 2021 at 16:51

1 Answer 1

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”).

5
  • 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? Commented Sep 10, 2021 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. Commented Sep 10, 2021 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? Commented Sep 11, 2021 at 8: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. Commented Sep 11, 2021 at 10:18
  • Thank you. I haven't understood all but i can see where it is going wrong. Thank you so much Commented Sep 13, 2021 at 11:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.