3

When trying to flash my genuine arduino micro with avrdude from cmd on a Manjaro system I get the infamous error

avrdude: stk500_getsync() attempt x of 10 ...

Im adding this post to the many other that ask about this error because all of them are trying to use the IDE to flash the arduino, whereas I can use it no problem on both Linux and Windows, but when using avrdude from cmd on Linux I get this error. This confirms that the board is fine and there are no shorts on the reset pin or any other possible physical cause of this issue, something appears to be wrong in the avrdude configuration.

This is my Makefile:

default: build

build:
    avr-gcc -mmcu=atmega32u4 main.c -o main

flash:  
    avr-objcopy -O ihex -R .eeprom main main.hex
    avrdude -F -V -c arduino -p m32u4 -P /dev/ttyACM0 -b 115200 -U flash:w:main.hex

Can you suggest anyway to fix this?

EDIT: Problem Solved!

The error was in the -c parameter, for the atmega 32u4 it's supposed to be avr109 and not arduino.

StefanoN
  • 131
  • 4
  • 1
    In the IDE you can set the preference for verbose output on upload, and then watch the log. It might show all commands that the IDE uses. – the busybee May 05 '22 at 06:01

1 Answers1

4

There are two common ways of resetting an Arduino into the bootloader:

  1. For UART based boards (Uno, Mega2560 etc) just opening the port resets the board
  2. For USB based boards (Micro, etc with the 32u4 chip) you have to manually tell it to reset

Yours is option 2 - unless you tell it to reset the board it won't do anything. The IDE normally handles that for you.

But it's simple: just cause the CDC/ACM port to be opened at 1200 baud and closed again. The simplest method is to use stty to do it for you:

stty -F /dev/ttyACM0 speed 1200

You should find that it then resets the board. Adding a short delay to allow the USB to reset would be good too. So your whole programming routine could look something like:

flash:  
    avr-objcopy -O ihex -R .eeprom main main.hex
    stty -F /dev/ttyACM0 speed 1200
    sleep 1
    avrdude -F -V -c arduino -p m32u4 -P /dev/ttyACM0 -b 115200 -U flash:w:main.hex

In Makefiles I find it useful to set the port as an overridable variable:

PORT?=/dev/ttyACM0

Then use ${PORT} everywhere instead of /dev/ttyACM0 - then you can use make PORT=/dev/ttyACM1 if you need to without having to edit the Makefile. Great if you have multiple boards.

Majenko
  • 103,876
  • 5
  • 75
  • 133
  • I was indeed thinking about the reset, thanks for the explanation! When I run the flash command the I get the following and then it stucks without going any further: ```avr-objcopy -O ihex -R .eeprom main main.hex \n stty -F /dev/ttyACM0 speed 1200 \n 9600 \n``` What is this about? – StefanoN May 04 '22 at 13:04
  • This [Arduino Makefile](https://github.com/sudar/Arduino-Makefile) comes with [a small script](https://github.com/sudar/Arduino-Makefile/blob/1.6.0/bin/ard-reset-arduino) that does exactly this on a Micro, and has different logic for other board types. – Edgar Bonet May 04 '22 at 13:12
  • @EdgarBonet I'm trying to learn low level programming, thats why I'm doing it with a MakeFile, I don't want to use a python script. – StefanoN May 04 '22 at 13:26