I want to compile and transfer an Arduino program by myself on a Leonardo board.
Everything works great with the Arduino official IDE. I have enabled verbose mode for compiling and bytecode transfer.
I can see each command line.
I want to understand each line.
Everything is good except last step: transfer with AVRDUDE. If I type exactly the same command, I get an error:
.avrdude: butterfly_recv(): programmer is not responding
This error is not present if I upload code with the Arduino IDE.
I can see a difference - the Arduino IDE displays this line before the AVRDUDE call:
Forcing reset using 1200 bps open/close on port /dev/cu.usbmodem1431
How can I make this reset by command line?
Press and hold the reset button on the Leonardo or Micro, then hit the upload button in the Arduino software. Only release the reset button after you see the message "Uploading..." appear in the software's status bar. When you do so, the bootloader will start, creating a new virtual (CDC) serial port on the computer.
The RESET button is a white or blue push button located on top of your Arduino board. Pressing it has the same effect as disconnecting and reconnecting the power supply: The board will wait briefly for a new sketch to uploaded, then it will start executing any instructions in the sketch from the beginning.
There are two ways to reset Arduino using Arduino code: By Arduino code only (called software reset): by this way, Arduino code wil be run from the beginning. But Hardware peripherals are not reset. For example, states of IO pin is not set set to default.
All you have to do is press the momentary push button mounted to the top of the board, and your Arduino will reset.
I had the same issue on macOS, and I came up with the following Bash script:
# Find the Arduino port
ARDUINO_UPLOAD_PORT="$(find /dev/cu.usbmodem* | head -n 1)"
# Reset the Arduino
stty -f "${ARDUINO_UPLOAD_PORT}" 1200
# Wait for it...
while :; do
sleep 0.5
[ -c "${ARDUINO_UPLOAD_PORT}" ] && break
done
# ...upload!
avrdude "${OPTIONS[@]}"
The while
loop is the trick! It's going to proceed as soon as the Arduino port is back online.
This is part of a Makefile I wrote for the project Sesame.
On Windows, in a command prompt, the same solution, slightly different batch file:
It determines the bootloader COM port as well. Note that just only the Leonardo to be flashed should be connected!
@echo off
echo Upgrade procedure starting.
if %1.==. goto error
set hexfile=%1
set comportA=NONE
set comportB=NONE
if not exist %hexfile% goto error
for /f "usebackq" %%B in (`wmic path Win32_SerialPort Where "Caption LIKE '%%Leonardo%%'" Get DeviceID ^| FIND "COM"`) do set comportA=%%B
if %comportA%==NONE goto nodevice
echo COM port for Arduino device is detected as %comportA%.
echo Reset Arduino into bootloader
mode %comportA%: baud=12 > nul
timeout 2 > nul
for /f "usebackq" %%B in (`wmic path Win32_SerialPort Where "Caption LIKE '%%Leonardo%%'" Get DeviceID ^| FIND "COM"`) do set comportB=%%B
if %comportB%==NONE goto nobldevice
echo COM port for Arduino bootloader device is detected as %comportB%.
echo.
echo Starting AVR Downloader/UploaDEr.....
avrdude -pm32u4 -cavr109 -D -P%comportB% -b57600 -Uflash:w:%hexfile%
goto upgradedone
:nodevice
echo No matching module found, you should connect the module you want to upgrade.
goto end
:nobldevice
echo Reset into bootloader failed, please try again...
goto end
:error
Echo Missing parameter or file, you should provide the full filename of an existing .hex file you want to use.
goto end
:upgradedone
echo.
echo Upgrade done!
:end
I had the same problem. I've tried opening and closing the ACM0 port with a Python script at 1200 baud, as someone already mentioned. It didn't work for me. Then I have received half-advice to try toggling RTS/DTS and that will make autoreset. So in the end I found the solution (at least for me) on Linux Mint 18.2 (Sonya):
#! /usr/bin/python
import sys
import serial
com = serial.Serial(sys.argv[1], 1200)
com.dtr=False
com.close()
python ./reset.py "/dev/ttyACM0"
dmesg
shows me:
[21850.047120] cdc_acm 1-1:1.0: ttyACM0: USB ACM device
[22093.700327] usb 1-1: USB disconnect, device number 53
[22094.034133] usb 1-1: new full-speed USB device number 54 using xhci_hcd
[22094.175377] usb 1-1: New USB device found, idVendor=2341, idProduct=0036
[22094.175381] usb 1-1: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[22094.175384] usb 1-1: Product: Arduino Leonardo
[22094.175387] usb 1-1: Manufacturer: Arduino LLC
[22094.175964] cdc_acm 1-1:1.0: ttyACM0: USB ACM device
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With