Allow configuring serial port as USB location ID

This is a workaround for the inconsistent device naming pattern
implemented in the device driver for Silicon Labs CP2102 USB to UART
Bridge Controller.  When there are multiple devices installed on one
computer, the second and subsequent devices are given a numeric suffix
that increments every time one of the devices is plugged in.  Thus,
unplugging and reinserting a cable causes the device name to change.

The workaround is to use the device USB location identifier as an
alternative consistent handle for the device, assuming that the cable
is plugged into the same jack each time.  This workaround does not
interfere with using the normal device name as the handle for cases
where only one device is used or when the inconsistent naming is not a
problem.
This commit is contained in:
Stephen Casner 2018-02-18 14:35:24 -08:00
parent 8d8d62da9e
commit 3d5f7b3efd
2 changed files with 64 additions and 3 deletions

View file

@ -1,6 +1,11 @@
# Component support for esptool.py. Doesn't do much by itself,
# components have their own flash targets that can use these variables.
ESPPORT ?= $(CONFIG_ESPTOOLPY_PORT)
PYTHON ?= $(call dequote,$(CONFIG_PYTHON))
ifdef CONFIG_ESPTOOLPY_PORT
ESPPORT ?= $(shell $(PYTHON) $(IDF_PATH)/tools/findcp2102.py $(CONFIG_ESPTOOLPY_PORT))
endif
ESPBAUD ?= $(CONFIG_ESPTOOLPY_BAUD)
ESPFLASHMODE ?= $(CONFIG_ESPTOOLPY_FLASHMODE)
ESPFLASHFREQ ?= $(CONFIG_ESPTOOLPY_FLASHFREQ)
@ -8,8 +13,6 @@ ESPFLASHSIZE ?= $(CONFIG_ESPTOOLPY_FLASHSIZE)
CONFIG_ESPTOOLPY_COMPRESSED ?=
PYTHON ?= $(call dequote,$(CONFIG_PYTHON))
# two commands that can be used from other components
# to invoke esptool.py (with or without serial port args)
#

58
tools/findcp2102.py Executable file
View file

@ -0,0 +1,58 @@
#!/usr/bin/env python
#
# This program is a workaround for the inconsistent device naming
# pattern implemented in the device driver for Silicon Labs CP2102 USB
# to UART Bridge Controller. When there are multiple devices
# installed on one computer, the second and subsequent devices are
# given a numeric suffix that increments every time one of the devices
# is plugged in. Thus, unplugging and reinserting a cable causes the
# device name to change.
#
# The workaround is to use the device USB location identifier as a
# consistent handle for the device, assuming that the cable is plugged
# into the same jack each time.
#
# This program has two uses:
#
# 1) When invoked with no arguments it searches for all CP2102 devices
# in the computer's USB hierarchy and prints a list of the bus
# location identifier and device name for each. The device names will
# be numbered in the order they were plugged in, so this list allows
# associating a location identifier with each physical device.
#
# 2) When invoked with one argument that is the bus location of a
# CP2102 device it prints the associated device name. This allows the
# location identifier to be used as a constant identifier in a system
# configuration file and then this program can be called in a script or
# makefile to translate to the dynamic device name. If the argument
# is not found as a location identifier, the argument will be output
# unchanged; this allows the script or makefile to work with either a
# location identifier or a device name as the configured handle.
#
# This program is derived from code provided without copyright by user
# spachner21 in the Silicon Labs Interface Forum. It is provided here
# as public domain.
import sys
import serial.tools.list_ports
ports = serial.tools.list_ports.grep("USB VID:PID=10c4:ea60")
#Make new list from generated 'generator' objects returned by grep above
pl = []
for p in ports:
pl.append(p)
#Sort list by object attribute 'location'
pl.sort(key=lambda x: x.location, reverse=False)
if len(sys.argv) > 1:
for p in pl:
if str(p.location) == sys.argv[1]:
print p.device
break
else:
print sys.argv[1]
else:
for p in pl:
print p.location + " " + p.device