mirror of
https://github.com/esphome/esphome.git
synced 2026-09-16 01:28:39 +00:00
[usb_uart] Add per-device-type maximum baud rate cap (#17259)
This commit is contained in:
@@ -46,42 +46,59 @@ DEFAULT_BAUD_RATE = 9600
|
||||
|
||||
|
||||
class Type:
|
||||
def __init__(self, name, vid, pid, cls, max_channels=1, baud_rate_required=True):
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
vid,
|
||||
pid,
|
||||
cls,
|
||||
max_channels=1,
|
||||
baud_rate_required=True,
|
||||
max_baud=1_000_000,
|
||||
):
|
||||
self.name = name
|
||||
cls = cls or name
|
||||
self.vid = vid
|
||||
self.pid = pid
|
||||
self.cls = usb_uart_ns.class_(f"USBUartType{cls}", USBUartComponent)
|
||||
self.max_channels = max_channels
|
||||
self._max_channels = max_channels
|
||||
self.baud_rate_required = baud_rate_required
|
||||
self.max_baud = max_baud
|
||||
|
||||
@property
|
||||
def max_channels(self) -> int:
|
||||
return (
|
||||
3
|
||||
if (
|
||||
CORE.is_esp32
|
||||
and get_esp32_variant() != VARIANT_ESP32P4
|
||||
and self._max_channels > 3
|
||||
)
|
||||
else self._max_channels
|
||||
)
|
||||
|
||||
|
||||
uart_types = (
|
||||
Type("CDC_ACM", 0, 0, "CdcAcm", 1, baud_rate_required=False),
|
||||
Type("CH34X", 0x1A86, 0x55D5, "CH34X", 4),
|
||||
Type("CH340", 0x1A86, 0x7523, "CH34X", 1),
|
||||
Type("CP210X", 0x10C4, 0xEA60, "CP210X", 3),
|
||||
Type("CH34X", 0x1A86, 0x55D5, "CH34X", 4, max_baud=2_000_000),
|
||||
Type("CH340", 0x1A86, 0x7523, "CH34X", 1, max_baud=2_000_000),
|
||||
Type("CP210X", 0x10C4, 0xEA60, "CP210X", 3, max_baud=2_000_000),
|
||||
Type("ESP_JTAG", 0x303A, 0x1001, "CdcAcm", 1, baud_rate_required=False),
|
||||
Type("FT232", 0x0403, 0x6001, "FT23XX", 1),
|
||||
Type("FT2232", 0x0403, 0x6010, "FT23XX", 2),
|
||||
Type("FT4232", 0x0403, 0x6011, "FT23XX", 4),
|
||||
Type("PL2303", 0x067B, 0x2303, "PL2303", 1),
|
||||
Type("PL2303GB", 0x067B, 0x23B3, "PL2303", 1),
|
||||
Type("PL2303GC", 0x067B, 0x23A3, "PL2303", 1),
|
||||
Type("PL2303GE", 0x067B, 0x23E3, "PL2303", 1),
|
||||
Type("PL2303GL", 0x067B, 0x23D3, "PL2303", 1),
|
||||
Type("PL2303GS", 0x067B, 0x23F3, "PL2303", 1),
|
||||
Type("PL2303GT", 0x067B, 0x23C3, "PL2303", 1),
|
||||
Type("FT232", 0x0403, 0x6001, "FT23XX", 1, max_baud=3_000_000),
|
||||
Type("FT2232", 0x0403, 0x6010, "FT23XX", 2, max_baud=12_000_000),
|
||||
Type("FT4232", 0x0403, 0x6011, "FT23XX", 4, max_baud=12_000_000),
|
||||
Type("PL2303", 0x067B, 0x2303, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GB", 0x067B, 0x23B3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GC", 0x067B, 0x23A3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GE", 0x067B, 0x23E3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GL", 0x067B, 0x23D3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GS", 0x067B, 0x23F3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("PL2303GT", 0x067B, 0x23C3, "PL2303", 1, max_baud=6_000_000),
|
||||
Type("STM32_VCP", 0x0483, 0x5740, "CdcAcm", 1, baud_rate_required=False),
|
||||
)
|
||||
|
||||
|
||||
def channel_schema(channels, baud_rate_required):
|
||||
# For now S3 is restricted to 3 channels since each needs 2 endpoints, plus the control endpoint, and
|
||||
# there are only a total of 8 endpoints available.
|
||||
# This will need updating when the 8 channel devices that multiplex over an endpoint are added.
|
||||
if CORE.is_esp32 and get_esp32_variant() != VARIANT_ESP32P4 and channels > 3:
|
||||
channels = 3
|
||||
def channel_schema(type_: "Type") -> cv.Schema:
|
||||
return cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_CHANNELS): cv.All(
|
||||
@@ -94,11 +111,11 @@ def channel_schema(channels, baud_rate_required):
|
||||
),
|
||||
(
|
||||
cv.Required(CONF_BAUD_RATE)
|
||||
if baud_rate_required
|
||||
if type_.baud_rate_required
|
||||
else cv.Optional(
|
||||
CONF_BAUD_RATE, default=DEFAULT_BAUD_RATE
|
||||
)
|
||||
): cv.int_range(min=300, max=1000000),
|
||||
): cv.int_range(min=300, max=type_.max_baud),
|
||||
cv.Optional(CONF_STOP_BITS, default="1"): cv.enum(
|
||||
UART_STOP_BITS_OPTIONS, upper=True
|
||||
),
|
||||
@@ -117,7 +134,10 @@ def channel_schema(channels, baud_rate_required):
|
||||
}
|
||||
)
|
||||
),
|
||||
cv.Length(max=channels),
|
||||
cv.Length(
|
||||
max=type_.max_channels,
|
||||
msg=f"Device type {type_.name} supports a maximum of {type_.max_channels} channels",
|
||||
),
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -127,7 +147,7 @@ CONFIG_SCHEMA = cv.ensure_list(
|
||||
cv.typed_schema(
|
||||
{
|
||||
it.name: usb_device_schema(it.cls, it.vid, it.pid).extend(
|
||||
channel_schema(it.max_channels, it.baud_rate_required)
|
||||
channel_schema(it)
|
||||
)
|
||||
for it in uart_types
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user