How to detect right COM-port for uploading code to Arduino Leonardo (atmega32u4) outside the Arduino IDE?

Context

I'm cofounder of this new, free IDE for microcontrollers:
https://embeetle.com

We're currently making lots of efforts to support Arduino boards and microcontrollers. The Arduino Leonardo is next.

Problem

When uploading firmware to the Arduino Leonardo, I noticed something strange. My Leonardo board was detected by the Arduino IDE on port COM9. Also the Windows Device Manager detects the board there:

image

However, when flashing the firmware, I see the following output in the Arduino IDE:

Forcing reset using 1200bps open/close on port COM9
PORTS {COM9, } / {} => {}
PORTS {} / {} => {}
PORTS {} / {} => {}
PORTS {} / {COM10, } => {COM10, }
Found upload port: COM10
avrdude -CC:\Users\Gebruiker\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf
        -v
        -patmega32u4
        -cavr109
        -PCOM10
        -b57600
        -D
        -Uflash:w:C:\Users\GEBRUI~1\AppData\Local\Temp\arduino_build_518049/Blink.ino.hex:i 

It looks like the Leonardo board first displays itself as COM10 (after a reset), and then switches to COM9 after a few seconds. In my understanding, the Arduino IDE forces a reset and then polls the COM-ports to detect the Leonardo board at COM10. As soon as this detection happened, the Arduino IDE proceeds with the actual firmware upload.

My question is: how can we do this in our IDE? In case we'd use Python for this, what Python commands could be used?

PS: Parts of our IDE are coded in Python, other parts in C++

That does not look like a Leonardo. A real Leonardo identifies itself after a power cycle as an Arduino Leonardo and after a reset as a Arduino Leonardo (bootloader). Reset is either achieved by pressing and releasing the reset button or by opening and closing the port at 1200 baud. Once the boot loader times out or a sketch is uploaded, control goes back to the sketch and the Leonardo identifies itself as that.

After you force the reset, you have to retrieve a listing of the com ports and next try to determine which one is the Leonardo bootloader. I have no idea how it's done but the IDE source code is open source so you can check that.

Hi @sterretje ,
Thank you for this information.

The Leonardo board must be genuine, because I bought it directly from the Arduino store. However, as soon as I had it in my hands, I played with the bootloader. Today I burned the default bootloader with an Atmel ICE - so I thought it should now be in its initial state (the way it left the Arduino factory). I checked, and the burned bootloader seems to be this one:
bootloaders/caterina/Caterina-Leonardo.hex

What could be the reason that it identifies itself merely as USB Serial Device (COM9), not as an Arduino Leonardo?

So that's why the port is opened at 1200 baud. I didn't know it's a trick to reset the chip. Thanks for this info.

What are the timing constraints? I mean, how many milliseconds do I have to wait after the reset before I can start looking for the new COM-port and launch avrdude?

I don't know.

You have, if memory serves me well, about 8 seconds to start the actual download. You can check in Windows device manager how long it takes to go from COM10 back to COM9.

1 Like

Thank you very much @sterretje :slight_smile:
By the way, please give Embeetle IDE a try. I'd be happy to get your feedback on our project.

Kind regards,
Kristof

The Leonardo serial port is software generated unlike devices with the 328 processors that might be relevant

1 Like

Thank you @hammy for this information :slight_smile: