Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serial port discovery #1498

Merged
merged 57 commits into from
Sep 22, 2023
Merged

Add serial port discovery #1498

merged 57 commits into from
Sep 22, 2023

Conversation

MCUdude
Copy link
Collaborator

@MCUdude MCUdude commented Aug 31, 2023

This PR adds serial port discovery functionality using libserialport. Please test and review!

$ avrdude -curclock -patmega169 -P ch340
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9405 (probably m169)

avrdude done.  Thank you.

It is also possible to specify a serial number like this: -P ft232r:A50285BI, using USB VID/PID like so: -P usb:0x0403:0x6002, or using USB VID/PID/SN like so: -P usb:0x0403:0x6002:A50285BI

For some reason, I'm getting a linker error when building using make. Perhaps @dl8dtl knows what causes this?

make
/Library/Developer/CommandLineTools/usr/bin/make  all-recursive
Making all in .
  CC       avrdude-main.o
  CC       libavrdude_a-serialadapter.o
  AR       libavrdude.a
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-confwin.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ppi.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ppiwin.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-serbb_win32.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ser_win32.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-confwin.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ppi.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ppiwin.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-serbb_win32.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: libavrdude.a(libavrdude_a-ser_win32.o) has no symbols
  CCLD     avrdude
ld: warning: directory not found for option '-FCoreFoundation'
Undefined symbols for architecture x86_64:
  "_sp_free_port_list", referenced from:
      _find_serialport_adapter in libavrdude.a(libavrdude_a-serialadapter.o)
      _find_serialport_vid_pid in libavrdude.a(libavrdude_a-serialadapter.o)
  "_sp_get_port_name", referenced from:
      _find_serialport_adapter in libavrdude.a(libavrdude_a-serialadapter.o)
      _find_serialport_vid_pid in libavrdude.a(libavrdude_a-serialadapter.o)
  "_sp_get_port_usb_serial", referenced from:
      _find_serialport_adapter in libavrdude.a(libavrdude_a-serialadapter.o)
      _find_serialport_vid_pid in libavrdude.a(libavrdude_a-serialadapter.o)
  "_sp_get_port_usb_vid_pid", referenced from:
      _find_serialport_adapter in libavrdude.a(libavrdude_a-serialadapter.o)
      _find_serialport_vid_pid in libavrdude.a(libavrdude_a-serialadapter.o)
  "_sp_list_ports", referenced from:
      _find_serialport_adapter in libavrdude.a(libavrdude_a-serialadapter.o)
      _find_serialport_vid_pid in libavrdude.a(libavrdude_a-serialadapter.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [avrdude] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

@MCUdude MCUdude linked an issue Aug 31, 2023 that may be closed by this pull request
@MCUdude MCUdude added the enhancement New feature or request label Aug 31, 2023
@mcuee
Copy link
Collaborator

mcuee commented Aug 31, 2023

@MCUdude
I think you have to add libserail port detection to configure.ac.
https://github.com/avrdudes/avrdude/blob/main/src/configure.ac

@mcuee
Copy link
Collaborator

mcuee commented Aug 31, 2023

There are a few warnings when building using MinGW64.
https://github.com/avrdudes/avrdude/actions/runs/6043108677/job/16399525881?pr=1498

Same for my local build.

[65/71] Building C object src/CMakeFiles/avrdude.dir/main.c.obj
C:/work/avr/avrdude_test/avrdude_hans/src/main.c: In function 'main':
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1184:11: warning: the comparison will always evaluate as 'true' for the address of 'port_tok' will never be NULL [-Waddress]
 1184 |       if (port_tok[1] && port_tok[1][0])
      |           ^~~~~~~~
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1168:8: note: 'port_tok' declared here
 1168 |   char port_tok[3][256];
      |        ^~~~~~~~
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1191:9: warning: the comparison will always evaluate as 'true' for the address of 'port_tok' will never be NULL [-Waddress]
 1191 |     if (port_tok[1] && port_tok[1][0])
      |         ^~~~~~~~
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1168:8: note: 'port_tok' declared here
 1168 |   char port_tok[3][256];
      |        ^~~~~~~~
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1201:13: warning: the comparison will always evaluate as 'true' for the address of 'port_tok' will never be NULL [-Waddress]
 1201 |         if (port_tok[2] && port_tok[2][0])
      |             ^~~~~~~~
C:/work/avr/avrdude_test/avrdude_hans/src/main.c:1168:8: note: 'port_tok' declared here
 1168 |   char port_tok[3][256];
      |        ^~~~~~~~
[71/71] Linking C executable src\avrdude.exe

Build succeeded.

@mcuee
Copy link
Collaborator

mcuee commented Aug 31, 2023

@MCUdude

github action script needs to be added as well to install libserialport for each OS. If not, right now the builds are without libserialport.
https://github.com/avrdudes/avrdude/blob/main/.github/workflows/build.yml

For example, I need to use pacman -S mingw-w64-x86_64-libserialport under MSYS2 MinGW64.

I am not so sure how to do that for MSVC build though. Probably @mariusgreuel can help.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 1, 2023

I think you have to add libserial port detection to configure.ac.

True, but I don't know how/where... @dl8dtl?

There are a few warnings when building using MinGW64.

Thanks for the heads-up! I've pushed a fix for this.

github action script needs to be added as well to install libserialport for each OS. If not, right now the builds are without libserialport.

The question is if we want the CI to build Avrdude with or without libserialport support. Avrdude should work perfectly fine without as well, so it might be a good test to not have libserialport installed? I don't know. @mariusgreuel probably has something to say about this.

But apart from this, does the PR work for you @mcuee?

@mcuee
Copy link
Collaborator

mcuee commented Sep 1, 2023

But apart from this, does the PR work for you @mcuee?

I will carry out the tests over the weekend.

@mcuee
Copy link
Collaborator

mcuee commented Sep 1, 2023

The question is if we want the CI to build Avrdude with or without libserialport support. Avrdude should work perfectly fine without as well, so it might be a good test to not have libserialport installed? I don't know. @mariusgreuel probably has something to say about this.

I think we can at least build avrdude with libserialport support with Linux, macOS and MinGW where it is easier to add support.

BTW, since we only provide official Windows binary using MSVC build where we have to wait for @mariusgreuel or others to help support, the above change will not change status quo.

@stefanrueger
Copy link
Collaborator

Started reviewing. There are serious problems with port assignment. Here a patch 0001-Fix-port-assignment:

  • Ensure port is always set by cfg_strdup(), so it can be free'd
  • Fix possible buffer overrun when tokenising port
  • Free and reassign port string rather than copying over it as it has unknown allocated length
  • Warn if sp_get_port_name() returns NULL and return -1
  • Remove superfluous continue

src/main.c Outdated Show resolved Hide resolved
src/main.c Outdated
pmsg_error("serial adapter %s with serial number %s not found\n", seradapter, port_tok[1]);
else
pmsg_error("serial adapter %s not found\n", seradapter);
exit(1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we exit here? Say, there is a serialadapter ch340 in avrdude.conf. -P ch340 checks if there is a serial adapter ch340 connected and does not find one. OK, rather then exiting, it could still allow the open() routine to open the file ch340; after all it could be a symbolic link to an existing port for all we know

src/main.c Outdated Show resolved Hide resolved
src/main.c Outdated Show resolved Hide resolved
@stefanrueger
Copy link
Collaborator

@MCUdude This is a great feature, but there are pitfalls: Say I have a beloved 3d-printer that is plugged into the PC. Unbeknownst to me it uses an AVR board with a ch340 serial adapter. I now plug in one of your boards (also with a ch340 adapter) and want to use this feature uploading blink.hex to -P ch340. If I understand your PR correctly it would silently be uploaded to one of the two plugged in ch340 boards. As blink.hex outputs a signal on PD5 that is connected to a booby-trap on the 3d-printer the latter explodes with 50% probability.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 1, 2023

Thank you for the feedback! I've applied the patch you provided and the other suggestions as well. The code is very much "experimental", and is a result of me fighting segfaults and fooling around an entire evening. The functionality is there, but it absolutely needs a critical review and some work.

If I understand your PR correctly it would silently be uploaded to one of the two plugged in ch340 boards

Yes, that's correct. Ideally, it should loop through the port list to make sure that the user-specified USB to serial adapter is unique, and stop the upload process if there is a chance that the incorrect serial port may be chosen.

If you have suggestions on how something like this could be implemented, feel free to submit a patch or explain what you'd do. I'm not available this evening (it's my wife's birthday today), but I will be on Saturday and Sunday evening.

@stefanrueger
Copy link
Collaborator

One idea may be dreaming up a general complex use case. For example, a user has 7 boards plugged into the host: two original FT232R with a proper serial number, two cheap clones with identical serial numbers, a direct USART connector, a CH340 serial adapter (which don't have serial numbers) and a new connector CH911 that isn't known to avrdude.conf

  • ft232r:A600K207683 -> /dev/ttyUSB1
  • ft232r:A600K207684 -> /dev/ttyUSB7
  • ft232r:cheapo -> /dev/ttyUSB9
  • ft232r:cheapo -> /dev/ttyUSB3
  • /dev/ttyAMA0
  • ch340 -> /dev/ttyUSB4
  • ch911 -> /dev/ttyUSB8

Here a few suggestions that the code could consider

  • Only deploy the serial port discovery for -c programmers where the connection_type is serial
  • Check whether a given -P serialadapter string uniquely identifies a port, only then overwrite the port string
  • If the serialadapter code identifies multiple possible ports, show the user all possible unique alternatives (and do not overwrite the port string); for example, if the user requests -P ft232r then the output could be sth like
     avrdude warning: -P ft232r is not unique; consider one of the below
      -P /dev/ttyUSB1 or -P ft232r:A600K207683
      -P /dev/ttyUSB3 (via ft232r serial adapter)
      -P /dev/ttyUSB7 or -P ft232r:A600K207684
      -P /dev/ttyUSB9 (via ft232r serial adapter)
    
    Note that -P ft232r:cheapo isn't shown as it wouldn't result in a unique port string.
  • If -P string is not found as serial adapter, let pgm->open() try (could be a valid serial port or a symbolic link to one)
  • As always, the most important question is How would users know of AVRDUDE's new serial port discovery super power? They might not even know what a serial adapter is, nor that they could have serial number, nor that they can be specified instead of the serial port, nor what the syntax is for doing so. Sooooo, in above scenario when pgm->open() does fail and the programmer's connection_type is serial and there wasn't already a list of multiple possible ports given (see above) it would be fabulous to list possible connected devices:
    $ avrdude -P /dev/ttyUSB2
    avrdude error: unable to open programmer urclock on port /dev/ttyUSB2
    Possible candidate serial ports are:
      -P /dev/ttyUSB1 or -P ft232r:A600K207683
      -P /dev/ttyUSB3 (via ft232r serial adapter)
      -P /dev/ttyUSB4 or -P ch340
      -P /dev/ttyUSB7 or -P ft232r:A600K207684
      -P /dev/ttyUSB8 or -P usb:C0CA:C01A (serial adapter unknown to avrdude.conf)
      -P /dev/ttyUSB9 (via ft232r serial adapter)
    Note that above ports may not necessarily be connected to a target board or an AVR programmer.
    Also note there may be other direct serial ports not listed above.
    
    Of course, if serial port discovery yields only one candidate then the grammar should be singular along the lines of:
    $ avrdude -P /dev/ttyUSB2
    avrdude error: unable to open programmer urclock on port /dev/ttyUSB2
    A possible candidate serial port is -P /dev/ttyUSB4 (same as -P ch340) but
    note it may not be connected to a target board or an AVR programmer and also
    note there may be other direct serial ports.
    
  • Implement -P usb:VID:PID[:serial] for serial adapters not in avrdude.conf :)
  • Trailing serial numbers should be sufficient if unique on the host (as is the case for USB connections): -P ft232r:83
  • Seeing as we aspire to publishing libavrdude may I suggest slightly clearer function names:
    • find_serialport_adapter() -> setport_from_serialadapter()
    • find_serialport_vid_pid() -> setport_from_vid_pid()

I don't know how to turn Jörg's other suggestion into code as it only affects direct USB connections (connection_type = usb) which would remain unchanged but his suggestion of generic usb serial adapter specification just like for direct usb connections is already in above list, as is the trailing serial number.

@mcuee
Copy link
Collaborator

mcuee commented Sep 2, 2023

This PR seems to work well under Windows.

$ ./port_finder/port_finder.exe ch340
Found port:
id:     ch340
desc:   USB-SERIAL CH340 (COM41)
port:   COM41
serno:

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c wiring -p m2560 -P COM41
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e9801 (probably m2560)

avrdude_pr1498v1 done.  Thank you.

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c wiring -p m2560 -P ch340
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e9801 (probably m2560)

avrdude_pr1498v1 done.  Thank you.

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c wiring -p m2560 -P 0x1a86:0x7523
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e9801 (probably m2560)

avrdude_pr1498v1 done.  Thank you.

@mcuee
Copy link
Collaborator

mcuee commented Sep 2, 2023

Another example with Arduino Uno clone with ATmega16U2 USB CDC-ACM serial port.

@MCUdude I am not so sure how to specify programmer ID in this case.

$ ./port_finder/port_finder.exe
Missing programmer ID, e.g ch340

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c urclock -p m328p -P COM40
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e950f (probably m328p)

avrdude_pr1498v1 done.  Thank you.

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c urclock -p m328p -P 0x2341:0x0043
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e950f (probably m328p)

avrdude_pr1498v1 done.  Thank you.

$ ./avrdude_pr1498v1 -C ./avrdude_pr1498v1.conf -c urclock -p m328p -P 0x2341:0x0043:11352601009369196700
avrdude_pr1498v1: AVR device initialized and ready to accept instructions
avrdude_pr1498v1: device signature = 0x1e950f (probably m328p)

avrdude_pr1498v1 done.  Thank you.

From libserialport example:

MINGW64 /c/work/avr/avrdude_test/serial/libserialport/examples
$ ./port_info.exe COM40
Looking for port COM40.
Port name: COM40
Description: Arduino Uno (COM40)
Type: USB
Manufacturer: Arduino (www.arduino.cc)
Product: Arduino Uno
Serial: 11352601009369196700
VID: 2341 PID: 0043
Bus: 1 Address: 15
Freeing port.

and print the available options is the serial port isn't unique
@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 2, 2023

Thanks for the valuable input @stefanrueger! Now Avrdude requires the port to be unique, and it will print the alternative options if not. The code works but needs another set of eyes to make sure it's as efficient as possible. There are a bunch of for loops in there; maybe a clever mind can simply the code a bit.

One thing I didn't figure out is how to properly deal with the serports struct. The idea is to use libserialport to loop through all serial ports and store the useful information in an array of serports structs. However, the sernum and port strings are fixed sizes, and the maximum number of serial ports is 32.

It would be great if you could suggest how this could be dealt with dynamically.

@stefanrueger
Copy link
Collaborator

Cool... progress!

the sernum and port strings are fixed sizes

Can do but then it's a no-no to strcpy() strings of unknown length into the fixed space. Why not declare them char * and assign via malloc'd space? char *port = cfg_strdup(__func__, xxx); Can be free'd when deconstructing the storage.

maximum number of serial ports is 32

The code could first count how many there are and then allocate the space?

int n;
for (n = 0; port_list[n]; n++)
  continue;

struct serports *sp = cfg_malloc(__func__, n*sizeof*sp);

for(int i = 0; i < n; i++) {
  // ... set prt
  sp[i].sernum = cfg_strdup(__func__, sp_get_port_usb_serial(prt));
  sp[i].port = cfg_strdup(__func__, sp_get_port_name(prt));
}

When you don't need the array deconstruct via

for(int i = 0; i < n; i++) {
  free(sp[i].sernum);
  free(sp[i].port);
}
free(sp);

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 17, 2023

However, when I specify the ambiguous -P ch340 I would expect only the two ch340 ports to be listed, but three are listed, one of which superfluous (and wrong):

Fixed.

However, when I specify the ambiguous -P ch340 I would expect only the two ch340 ports to be listed, but three are listed, one of which superfluous (and wrong):

I need to borrow a big USB hub from work to iron this out. I'm sure it can be done. Thanks for pointing this out.

I thought it might be best to decompose the code into a structure that is closer to the problem at hand. Here a
patch 0001-Refactor-serialadapter.c.zip. No guarantees; this needs thoroughly reviewing and thoroughly testing!

I had a look at the patch, and I have to admit I have mixed feelings. On one side, I'm sure the remaining issues from my core are gone, but on the other side, I don't think there are that many issues left in the first place. On the other side, the code is very different from my approach, and even though it sure is more clever, I find it more complicated and unfamiliar at first glance (I'm sure this the latter will change).

Don't get me wrong, I'm very happy about all the feedback I've gotten while working on the for the last two or three weeks, but it feels a little wistful to merge a patch at the very end that replaces most of the underlying functionality I've spent all this time figuring out myself.

I'll spend some time reading through the patch to figure out exactly what's going on, while still having #1500 in the back of my head.

else
msg_warning("-P %s (via %s serial adapter)\n", sp[i].port, *portp);
if (sp[i].match) {
if (sp[i].unique && sp[i].sernum[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can sp[i].match and sp[i].unique determine whether ser is ambiguous wrt to the plugged-in devices?

These flags cannot possibly know this as they have never seen ser at all; when they were computed in get_libserialport_data() that function only had the plugged-in devices to look at. match and unique are not useful for the problem of matching a user-specified adapter. This code cannot possibly work. It's the wrong structure.

And it does not work: I have just plugged in four devices, two ft232r with different serial number and two ch340 without serial numbers. Here the classic test avrdude -P dunno output, which is correct on this occasion:

avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
-P /dev/ttyUSB3, -P ft232r:MCU8
-P /dev/ttyUSB1 (via ch340 serial adapter)
-P /dev/ttyUSB2, -P ft232r:A600K203
-P /dev/ttyUSB0 (via ch340 serial adapter)
Note that above ports may not necessarily be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

Now I test

$ avrdude -P ch340
avrdude warning: -P ch340 is not unique; consider one of the below
-P /dev/ttyUSB3 or -P ch340:MCU8
-P /dev/ttyUSB1 (via ch340 serial adapter)
-P /dev/ttyUSB2 or -P ch340:A600K203
-P /dev/ttyUSB0 (via ch340 serial adapter)
avrdude OS error: cannot open port ch340: No such file or directory
avrdude error: unable to open programmer urclock on port ch340

Half of the output is wrong on more than one level. And the same happens with

$ avrdude -P ft232r
avrdude warning: -P ft232r is not unique; consider one of the below
-P /dev/ttyUSB3 or -P ft232r:MCU8
-P /dev/ttyUSB1 (via ft232r serial adapter)
-P /dev/ttyUSB2 or -P ft232r:A600K203
-P /dev/ttyUSB0 (via ft232r serial adapter)
avrdude OS error: cannot open port ft232r: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r

The reason for this is that the match, unique and known flags are not needed and their use yields the wrong outputs. The current code needs some fundamental change.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 18, 2023

I've connected 12 USB to serial devices to test your patch @stefanrueger. The first issue I've encountered is that it seems like it doesn't print "native" ports; ports that are built-in and don't have a VID/PID. I think these should be printed if the user has specified an unknown port.

Apart from this, I've not encountered any issues.

Ideally, there should have been a lot more "duplicate" Arduino boards, but I was so dumb and connected a 12V port to the USB hub, which ended up frying four boards. So I've ordered 3x ATmega16U2 chips and an ATmega32U4 to fix the broken boards. Luckily, I only fried one target microcontroller, and this was only an ATmega328P in a socket, so that's not a big loss.

I've connected the following devices:

  • 3x CH340 boards without serial numbers
  • 3x FT232R boards with unique serial numbers
  • 1x Arduino UNO Wifi rev2 (usb:03eb:2145:11B050488B1C13A05D65)
  • 1x Arduino Nano Every
  • 1x Arduino UNO (usb:2341:0043)
  • 1x Curiosity Nano ATmega4809 (usb:03eb:2175)
  • 1x MicroUPDI programmer (mEDBG/UNO Wifi Rev2 with custom SN)
  • ATEN UC232A RS232 adapter (usb:0557:2008)
Output with all adapters connected
$ cat ~/.avrduderc 
default_programmer = "usbasp";
default_serial     = "/dev/cu.usbserial-1410";
#allow_subshells    = yes;

serialadapter parent "ch340"
    id                     = "ch340-250k";
    baudrate               = 250000;
;

serialadapter parent "ch340"
    id                     = "ch340-500k";
    baudrate               = 500000;
;

serialadapter parent "ft232r"
    id                     = "3d-printer";
    usbsn                  = "A60408VE";
    baudrate               = 230400;
;

serialadapter parent "ft232r"
    id                     = "xmega-dev-board";
    baudrate               = 57600;
;

serialadapter
    id                     = "nano-every";
    desc                   = "Arduino Nano Every";
    usbvid                 = 0x2341;
    usbpid                 = 0x0058;
;

$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.usbmodem1424302 or -P usb:03eb:2145:MICROUPDIPROGRAMMERX
  -P /dev/cu.usbmodem142301 or -P nano-every
  -P /dev/cu.usbmodem1421401 or -P usb:2341:0043
  -P /dev/cu.usbserial-142420 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142410 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142440 (via ch340 serial adapter)
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbmodem142202 or -P usb:03eb:2145:11B050488B1C13A05D65
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
  -P /dev/cu.usbmodem141202 or -P usb:03eb:2175
  -P /dev/cu.UC-232AC or -P usb:0557:2008
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ch340
avrdude warning: -P ch340 is ambiguous; consider
  -P /dev/cu.usbserial-142420 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142410 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142440 (via ch340 serial adapter)
avrdude OS error: cannot open port ch340: No such file or directory
avrdude error: unable to open programmer urclock on port ch340

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r
avrdude warning: -P ft232r is ambiguous; consider
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
avrdude OS error: cannot open port ft232r: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P nano-every
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P ft232r:AH00M3HQ
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
avrdude warning: attempt 4 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P xmega-dev-board:A60408VE
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C
In the next step I disconnected all adapters except for the FT232R ones:
$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r
avrdude warning: -P ft232r is ambiguous; consider
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
avrdude OS error: cannot open port ft232r: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r:A
avrdude warning: -P ft232r:A is ambiguous; consider
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
avrdude OS error: cannot open port ft232r:A: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r:A

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r:A6
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P ft232r:...I
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P ft232r:...q
avrdude warning: -P ft232r:...q is not connected; consider
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
avrdude OS error: cannot open port ft232r:...q: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r:...q

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P 3d-printer
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P 3d-printer:A5
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C
Arduino UNO Wifi Rev2, Arduino UNO, Curiosity Nano and UC-232A connected
$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.usbmodem1421102 or -P usb:03eb:2145
  -P /dev/cu.usbmodem1421202 or -P usb:03eb:2175
  -P /dev/cu.usbmodem1421301 or -P usb:2341:0043
  -P /dev/cu.UC-232AC or -P usb:0557:2008
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P usb:03eb:2145
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P usb:0557:2008
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C
Single CH340 adapter connected
$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
A possible candidate serial port is:
  -P /dev/cu.usbserial-142110, -P ch340, -P ch340-250k or -P ch340-500k
Note that above port might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ch340-500k
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
avrdude warning: attempt 4 of 10: not in sync
avrdude warning: attempt 5 of 10: not in sync
avrdude warning: attempt 6 of 10: not in sync
avrdude warning: attempt 7 of 10: not in sync
avrdude warning: attempt 8 of 10: not in sync
avrdude warning: attempt 9 of 10: not in sync
avrdude warning: attempt 10 of 10: not in sync
avrdude error: unable to open programmer urclock on port /dev/cu.usbserial-142110
A possible candidate serial port is:
  -P /dev/cu.usbserial-142110, -P ch340, -P ch340-250k or -P ch340-500k
Note that above port might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Sep 18, 2023

@MCUdude Brilliant stress tests! ... and commiseration re the fry-up :-(

The tests have uncovered another problem: the order of the ports looks somewhat unknown/unsystematic/weird. How about sorting according to the port string in a way that /dev/ttyUSB7 comes before /dev/ttyUSB10 (ie, separate initial string from trailing number, then sort by initial string first and then numerically by trailing number)?

I have rustled up a patch to sort the list: 0001-Sort-list-of-plugged-in-SERPORTs-according-to-port.zip

With #1500 in mind, where you need a list of SERPORTS in the new list that were not in the old list, I have added in above patch a function SERPORT **sa_spa_not_spb() that does exactly that.

doesn't print "native" ports; ports that are built-in and don't have a VID/PID

Yes, I wondered about this, too. I have started to check the libserialport library to see how one could get these but have not made progress. This is why the current version prints Also note there may be other direct serial ports not listed above. See below.

@stefanrueger
Copy link
Collaborator

stefanrueger commented Sep 18, 2023

native ports

Aha, yes. My bad, the following corrects this

diff --git a/src/serialadapter.c b/src/serialadapter.c
index 5b649539..e17b7a22 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -122,7 +122,9 @@ static SERPORT *get_libserialport_data(int *np) {
     struct sp_port *p = port_list[i];
     char *q;
     // Fill sp struct with port information
-    if(sp_get_port_usb_vid_pid(p, &sp[j].vid, &sp[j].pid) == SP_OK && (q = sp_get_port_name(p))) {
+    if((q = sp_get_port_name(p))) {
+      if(sp_get_port_usb_vid_pid(p, &sp[j].vid, &sp[j].pid) != SP_OK)
+        sp[j].vid = sp[j].pid = 0;
       sp[j].port = cfg_strdup(__func__, q);
       sp[j].sernum = cfg_strdup(__func__, (q = sp_get_port_usb_serial(p))? q: "");
       j++;

If the native ports are printed, indeed, after reverting above maybe we can drop the output Also note there may be other direct serial ports not listed above?

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

@stefanrueger we're almost there. The VID/PID shouldn't be printed for the /dev/cu.Bluetooth-Incoming-Port port

$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.Bluetooth-Incoming-Port or -P usb:0000:0000
  -P /dev/cu.UC-232AC or -P usb:0557:2008
  -P /dev/cu.usbmodem14102 or -P usb:03eb:2175
  -P /dev/cu.usbmodem1421402 or -P usb:03eb:2145
  -P /dev/cu.usbmodem1424201 or -P nano-every
  -P /dev/cu.usbmodem1424301 or -P usb:2341:0043
  -P /dev/cu.usbserial-14220 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142130 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142440 (via ch340 serial adapter)
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
  -P /dev/cu.usbserial-DN021PGG, -P ft231x, -P ft234x or -P ft230x
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

BTW I found an FT231X based USB to serial adapter, and it too works like a charm!

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

Wouldn't this be sufficient? After all, the PID can theoretically be zero, but the VID can't be.

diff --git a/src/serialadapter.c b/src/serialadapter.c
index e17b7a22..3ee356a0 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -251,7 +251,8 @@ static void sa_print_specs(const SERPORT *sp, int n, int i) {
 
   msg_warning("  -P %s", sp[i].port);
   for(char **Ps = Pspecs; *Ps; Ps++) {
-    msg_warning("%s %s", str_starts(*Ps, "(via ")? "": Ps[1]? ", -P": " or -P", *Ps);
+    if(sp[i].vid != 0)
+      msg_warning("%s %s", str_starts(*Ps, "(via ")? "": Ps[1]? ", -P": " or -P", *Ps);
     free(*Ps);
   }
   msg_warning("\n");

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

If the native ports are printed, indeed, after reverting above maybe we can drop the output Also note there may be other direct serial ports not listed above

Also note there may be other direct serial ports not listed above.

I think it's smart to have the text there. Libserialport may not be able to find all ports for some reason. One known issue in MacOS is that liberserialport thinks that CH210x USB to serial adapters are native, and fail to list them. It only occurs on MacOS. See https://sigrok.org/bugzilla/show_bug.cgi?id=1698 for details. I hope it can be fixed soon. I was thinking about asking if there was any progress, but I can't create a bugzilla user; it looks like you'll have to be invited.

Anyways, this shouldn't be a showstopper for Avrdude, but it's good to know at least.

EDIT: regarding MacOS and the CP210x, it's not all that bad. Libserialport can still find the port, but it can't match the VID/PID, so it's treated like the internal Bluetooth port on my mac:

$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.Bluetooth-Incoming-Port
  -P /dev/cu.SLAB_USBtoUART
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

@stefanrueger
Copy link
Collaborator

-P usb:0000:0000

How big a problem is this? The current code base will allow this port to be addressed through -P usb:0:0, so it might well be shown as an option. Note it is no longer offered when it wouldn't work (such as when two ports are there with 0:0 vid:pid) as you can see above. If we don't print alternative -P specs for ports with 0 vid then users can no longer usefully give the internal serial port a short nickname in their .avrduderc (that would assign 0 vid and pid to it)

@stefanrueger
Copy link
Collaborator

@MCUdude I suspect above sorted outputs also use the most recent patch. Could you push the version you have tried for your latest tests? BTW, I realise that this PR is a playground to test out ideas. Feel free to go back to the drawing board and design a new PR (perhaps also with #1500 in mind). But in the end it is this kind of functionality that we currently have that I find very useful.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

How big a problem is this?

It's not a big problem, but it technically isn't correct that a native serial port can be addressed via VID and PID numbers, since it isn't a USB device at all. It's neat that a native port can be mapped, but if the user replaces its RS232 PCI card with something else, the native port (usb:0000:0000) may change.

Could you push the version you have tried for your latest tests?

I don't have any local changes to this PR other than the diff I suggested in a previous post.

Feel free to go back to the drawing board and design a new PR.

Well, I'm happy with the result of this PR, and I'm keen to start working on something else. If you want to implement #1500, even by submitting a patch to this PR, I'm all for it. I have plenty of hardware I can use to test the "1200bps touch" functionality.

@stefanrueger
Copy link
Collaborator

[no] local changes to this PR

Are you sure? The output of your most recent tests look like they are sorted in the way that my patch to sort the list: 0001-Sort-list-of-plugged-in-SERPORTs-according-to-port.zip patch would do it, but if I clone this PR the sorting isn't part of that (but I feel it should). Also the current PR has not reverted yet to the previous way the SERPORT list would have been created. That too should be done before merging.

As to -P usb:0:0 I am OK either way (yes, it isn't a USB device but usb:0:0 doesn't look like a USB device b/c all is zero). If you don't want -P usb:0:0 be printed then I would ever so slightly prefer

diff --git a/src/serialadapter.c b/src/serialadapter.c
index e17b7a22..ef9396cc 100644
--- a/src/serialadapter.c
+++ b/src/serialadapter.c
@@ -232,7 +232,7 @@ static char **sa_list_specs(const SERPORT *sp, int n, int i) {
     }
   }
 
-  if(Pi == 0) {                 // No unique serial adapter, so maybe vid:pid[:sn] works?
+  if(Pi == 0 && sp[i].vid) {    // No unique serial adapter, so maybe vid:pid[:sn] works?
     if(sa_unique_by_ids(sp[i].vid, sp[i].pid, "", sp, n, i))
       Plist[Pi++] = str_sprintf("usb:%04x:%04x", sp[i].vid, sp[i].pid);
     else if(*sn && sa_unique_by_ids(sp[i].vid, sp[i].pid, sn, sp, n, i))

happy with the result of this PR

OK, how about you, @MCUdude,

And once everyone is happy I merge the resulting PR.

#1500

Defo not up my road. I don't know what should be done, and cannot test as I don't have the boards. I would (as with everything that I am asked to merge) look very carefully at any PR before merging.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

Are you sure?

Oh, sorry! I committed the changes, but I forgot to push them. Should be OK now.

Push the sorting patch into this PR
Push #1498 (comment)
Solve the P usb:0:0 issue as you see fit
If you think str_endnumber() is generally useful, maybe move it to strutil.c and advertise in libavrdude.h
Test the final PR (@mcuee as well?)

I've done every step in the list, and I'll do a final stress test before I go to bed.

#1500
Defo not up my road. I don't know what should be done, and cannot test as I don't have the boards. I would (as with everything that I am asked to merge) look very carefully at any PR before merging.

No problem! I can start working on a proof of concept, submit a PR, and then we can take it from there.

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 19, 2023

Here are some test results. Note that I've patched libserialport in order to work properly with the CP210x chip family. See sigrokproject/libserialport#14 for details.

$ cat ~/.avrduderc 
default_programmer = "usbasp";
default_serial     = "/dev/cu.usbserial-1410";
#allow_subshells    = yes;

serialadapter parent "ch340"
    id                     = "ch340-250k";
    baudrate               = 250000;
;

serialadapter parent "ch340"
    id                     = "ch340-500k";
    baudrate               = 500000;
;

serialadapter parent "ft232r"
    id                     = "3d-printer";
    usbsn                  = "A60408VE";
    baudrate               = 230400;
;

serialadapter parent "ft232r"
    id                     = "xmega-dev-board";
    baudrate               = 57600;
;

serialadapter
    id                     = "nano-every";
    desc                   = "Arduino Nano Every";
    usbvid                 = 0x2341;
    usbpid                 = 0x0058;
;

$ ./avrdude -curclock -patmega328p -P dunno
avrdude OS error: cannot open port dunno: No such file or directory
avrdude error: unable to open programmer urclock on port dunno
Possible candidate serial ports are:
  -P /dev/cu.Bluetooth-Incoming-Port
  -P /dev/cu.SLAB_USBtoUART or -P cp210x
  -P /dev/cu.UC-232AC or -P usb:0557:2008
  -P /dev/cu.usbmodem142201 or -P nano-every
  -P /dev/cu.usbmodem142301 or -P usb:2341:0043
  -P /dev/cu.usbmodem1424202 or -P usb:03eb:2145
  -P /dev/cu.usbmodem1424402 or -P usb:03eb:2175
  -P /dev/cu.usbserial-142140 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142430 (via ch340 serial adapter)
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P nano-every
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P ch340
avrdude warning: -P ch340 is ambiguous; consider
  -P /dev/cu.usbserial-142140 (via ch340 serial adapter)
  -P /dev/cu.usbserial-142430 (via ch340 serial adapter)
avrdude OS error: cannot open port ch340: No such file or directory
avrdude error: unable to open programmer urclock on port ch340

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r
avrdude warning: -P ft232r is ambiguous; consider
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
avrdude OS error: cannot open port ft232r: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P 3d-printer
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

$ ./avrdude -curclock -patmega328p -P ft232r:A
avrdude warning: -P ft232r:A is ambiguous; consider
  -P /dev/cu.usbserial-A50285BI, -P ft232r:A50285BI, -P 3d-printer:A50285BI or -P xmega-dev-board:A50285BI
  -P /dev/cu.usbserial-A60408VE, -P ft232r:A60408VE, -P 3d-printer or -P xmega-dev-board:A60408VE
  -P /dev/cu.usbserial-AH00M3HQ, -P ft232r:AH00M3HQ, -P 3d-printer:AH00M3HQ or -P xmega-dev-board:AH00M3HQ
avrdude OS error: cannot open port ft232r:A: No such file or directory
avrdude error: unable to open programmer urclock on port ft232r:A

avrdude done.  Thank you.

$ ./avrdude -curclock -patmega328p -P ft232r:...I
avrdude warning: attempt 1 of 10: not in sync
avrdude warning: attempt 2 of 10: not in sync
avrdude warning: attempt 3 of 10: not in sync
^C

@stefanrueger
Copy link
Collaborator

It occurred to me that it might be nice to have an option -P ?s to list all curently plugged-in serial devices known to the libserialport library, and another option -P ?sa to list of all serial adapters known to AVRDUDE, i.e., defined in avrdude.conf.

$ avrdude -P ?s
Possible candidate serial ports are:
  -P /dev/ttyUSB0 or -P ft232r:A600K203
  -P /dev/ttyUSB1 or -P ft232r:MCU8
  -P /dev/ttyUSB3, -P ch340 or -P ch340-115k
Note that above ports might not be connected to a target board or an AVR programmer.
Also note there may be other direct serial ports not listed above.

$ avrdude -P ?sa
Valid serial adapters are:
  ch340      = [usbvid 0x1a86, usbpid 0x7523]
  ch340-115k = [usbvid 0x1a86, usbpid 0x7523]
  ch341a     = [usbvid 0x1a86, usbpid 0x5512]
  ch9102     = [usbvid 0x1a86, usbpid 0x55d4]
  cp210x     = [usbvid 0x10c4, usbpid 0xea60 0xea70 0xea71]
  ft2232h    = [usbvid 0x0403, usbpid 0x6010]
  ft231x     = [usbvid 0x0403, usbpid 0x6015]
  ft234x     = [usbvid 0x0403, usbpid 0x6015]
  ft230x     = [usbvid 0x0403, usbpid 0x6015]
  ft232h     = [usbvid 0x0403, usbpid 0x6014]
  ft232r     = [usbvid 0x0403, usbpid 0x6001]
  ft4232h    = [usbvid 0x0403, usbpid 0x6011]
  pl2303     = [usbvid 0x067b, usbpid 0x2303]

For simplicity, I have pushed this commit onto the current PR. I noted that ft231x, ft234x and ft232h share the same PID. Is this correct?

@MCUdude
Copy link
Collaborator Author

MCUdude commented Sep 20, 2023

It occurred to me that it might be nice to have an option -P ?s to list all curently plugged-in serial devices known to the libserialport library, and another option -P ?sa to list of all serial adapters known to AVRDUDE, i.e., defined in avrdude.conf.

Great idea! I'll give it a try later this evening.

I noted that ft231x, ft234x and ft232h share the same PID. Is this correct?

ft231x, ft234x, and ft230x have the same PID. The ft232h has a different PID.

@stefanrueger stefanrueger merged commit 0be6568 into avrdudes:main Sep 22, 2023
10 checks passed
@mcuee mcuee added this to the AVRDUDE 7.3 milestone Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support COM port discovery via USB VID/PID
3 participants