1#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
2 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
4#include "usb/usb_host.h"
12static constexpr uint8_t SET_LINE_REQUEST_TYPE = 0x21;
13static constexpr uint8_t SET_LINE_REQUEST = 0x20;
15static constexpr uint8_t SET_CONTROL_REQUEST_TYPE = 0x21;
16static constexpr uint8_t SET_CONTROL_REQUEST = 0x22;
17static constexpr uint8_t CONTROL_DTR = 0x01;
18static constexpr uint8_t CONTROL_RTS = 0x02;
20static constexpr uint8_t VENDOR_WRITE_REQUEST_TYPE = 0x40;
21static constexpr uint8_t VENDOR_WRITE_REQUEST = 0x01;
23static constexpr uint8_t VENDOR_READ_REQUEST_TYPE = 0xc0;
24static constexpr uint8_t VENDOR_READ_REQUEST = 0x01;
28static constexpr uint8_t VENDOR_WRITE_NREQUEST = 0x80;
29static constexpr uint8_t VENDOR_READ_NREQUEST = 0x81;
30static constexpr uint16_t VENDOR_READ_REG_FLAG = 0x80;
32static constexpr uint8_t FLOWCTRL_REG = 0x00;
33static constexpr uint8_t FLOWCTRL_MASK = 0xf0;
34static constexpr uint8_t FLOWCTRL_NONE = 0x00;
35static constexpr uint8_t FLOWCTRL_RTS_CTS_LEGACY = 0x40;
36static constexpr uint8_t FLOWCTRL_RTS_CTS = 0x60;
37static constexpr uint8_t FLOWCTRL_XON_XOFF = 0xc0;
39static constexpr uint8_t HXN_FLOWCTRL_REG = 0x0a;
40static constexpr uint8_t HXN_FLOWCTRL_MASK = 0x1c;
41static constexpr uint8_t HXN_FLOWCTRL_NONE = 0x1c;
42static constexpr uint8_t HXN_FLOWCTRL_RTS_CTS = 0x18;
43static constexpr uint8_t HXN_FLOWCTRL_XON_XOFF = 0x0c;
46static const uint32_t SUPPORTED_BAUD_RATES[] = {
47 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 19200,
48 28800, 38400, 57600, 115200, 230400, 460800, 614400, 921600, 1228800, 2457600, 3000000, 6000000,
64 return "G/HXN (newer)";
73 switch (flow_control) {
75 return HXN_FLOWCTRL_RTS_CTS;
77 return HXN_FLOWCTRL_XON_XOFF;
79 return HXN_FLOWCTRL_NONE;
82 switch (flow_control) {
95 size_t n =
sizeof(SUPPORTED_BAUD_RATES) /
sizeof(SUPPORTED_BAUD_RATES[0]);
96 for (
size_t i = 0; i < n; i++) {
97 if (SUPPORTED_BAUD_RATES[i] > baud) {
99 return SUPPORTED_BAUD_RATES[0];
100 uint32_t lower = SUPPORTED_BAUD_RATES[i - 1];
101 uint32_t upper = SUPPORTED_BAUD_RATES[i];
102 return (upper - baud) > (baud - lower) ? lower : upper;
105 return SUPPORTED_BAUD_RATES[n - 1];
109static void encode_baud_divisor(uint8_t buf[4],
uint32_t baud) {
114 uint8_t exponent = 0;
115 while (mantissa >= 512) {
126 buf[1] = (exponent << 1) | (mantissa >> 8);
127 buf[0] = mantissa & 0xFF;
131static void encode_baud_divisor_alt(uint8_t buf[4],
uint32_t baud) {
136 uint8_t exponent = 0;
137 while (mantissa >= 2048) {
147 buf[2] = exponent & 0x01;
148 buf[1] = ((exponent & ~0x01) << 4) | (mantissa >> 8);
149 buf[0] = mantissa & 0xFF;
153 const usb_config_desc_t *config_desc;
154 const usb_device_desc_t *device_desc;
155 std::vector<CdcEps> cdc_devs{};
157 if (usb_host_get_device_descriptor(dev_hdl, &device_desc) != ESP_OK) {
158 ESP_LOGE(TAG,
"PL2303: get_device_descriptor failed");
161 if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) != ESP_OK) {
162 ESP_LOGE(TAG,
"PL2303: get_active_config_descriptor failed");
167 uint16_t bcd_device = device_desc->bcdDevice;
168 uint16_t bcd_usb = device_desc->bcdUSB;
169 uint8_t bmax_packet = device_desc->bMaxPacketSize0;
170 uint8_t bdev_class = device_desc->bDeviceClass;
172 if (bdev_class == 0x02 || bmax_packet != 0x40) {
182 if (bcd_device == 0x0300) {
184 }
else if (bcd_device == 0x0500) {
193 ESP_LOGI(TAG,
"PL2303 chip type: %s (bcdUSB=0x%04X bcdDevice=0x%04X bMaxPkt=%u)", pl2303_type_name(this->
chip_type_),
194 bcd_usb, bcd_device, bmax_packet);
198 for (uint8_t i = 0; i < config_desc->bNumInterfaces; i++) {
199 int ep_offset = conf_offset;
200 const auto *intf = usb_parse_interface_descriptor(config_desc, i, 0, &conf_offset);
203 if (intf->bNumEndpoints < 2)
206 const usb_ep_desc_t *in_ep =
nullptr;
207 const usb_ep_desc_t *out_ep =
nullptr;
208 const usb_ep_desc_t *notify_ep =
nullptr;
210 for (uint8_t e = 0; e < intf->bNumEndpoints; e++) {
211 ep_offset = conf_offset;
212 const auto *ep = usb_parse_endpoint_descriptor_by_index(intf, e, config_desc->wTotalLength, &ep_offset);
215 if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK) {
216 if (ep->bEndpointAddress & usb_host::USB_DIR_IN) {
221 }
else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_INT) {
226 if (in_ep && out_ep) {
227 CdcEps eps{notify_ep, in_ep, out_ep, intf->bInterfaceNumber, intf->bInterfaceNumber};
229 eps.interrupt_interface_class = intf->bInterfaceClass;
230 cdc_devs.push_back(eps);
235 if (cdc_devs.empty()) {
236 ESP_LOGE(TAG,
"PL2303: failed to find bulk IN+OUT endpoints");
246struct Pl2303InitStep {
253static const Pl2303InitStep PL2303_INIT[] = {
254 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0,
true},
255 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0,
false},
256 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0,
true},
257 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0,
true},
258 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0,
true},
259 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1,
false},
260 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0,
true},
261 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0,
true},
262 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1,
false},
263 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0,
false},
264 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0,
false},
266static constexpr uint8_t PL2303_INIT_COUNT =
sizeof(PL2303_INIT) /
sizeof(PL2303_INIT[0]);
269 const uint8_t *response) {
274 uint8_t init_count = (!reload && !is_hxn) ? PL2303_INIT_COUNT : 0;
275 if (step < init_count) {
276 const auto &e = PL2303_INIT[step];
277 uint16_t index = (step == PL2303_INIT_COUNT - 1) ? (is_legacy ? 0x24 : 0x44) : e.index;
279 e.read ? std::vector<uint8_t>{0} : std::vector<uint8_t>{});
289 uint8_t line_coding[7] = {};
293 uint32_t nearest = nearest_supported_baud(baud);
297 encode_baud_divisor_alt(line_coding, baud);
299 encode_baud_divisor(line_coding, baud);
304 line_coding[5] =
static_cast<uint8_t
>(channel->
line_parity_());
309 ESP_LOGD(TAG,
"PL2303: SET_LINE_REQUEST baud=%" PRIu32
" stop=%u parity=%u data=%u", baud, line_coding[4],
310 line_coding[5], line_coding[6]);
312 std::vector<uint8_t> lc_vec(line_coding, line_coding + 7);
313 this->
config_transfer_(SET_LINE_REQUEST_TYPE, SET_LINE_REQUEST, 0, iface, lc_vec);
319 this->
config_transfer_(VENDOR_READ_REQUEST_TYPE, is_hxn ? VENDOR_READ_NREQUEST : VENDOR_READ_REQUEST,
320 is_hxn ? HXN_FLOWCTRL_REG : (FLOWCTRL_REG | VENDOR_READ_REG_FLAG), 0,
325 const uint8_t reg = is_hxn ? HXN_FLOWCTRL_REG : FLOWCTRL_REG;
326 const uint8_t mask = is_hxn ? HXN_FLOWCTRL_MASK : FLOWCTRL_MASK;
327 const uint8_t new_byte =
329 ESP_LOGD(TAG,
"PL2303: flow control register 0x%02X: 0x%02X -> 0x%02X", reg, response[0], new_byte);
330 this->
config_transfer_(VENDOR_WRITE_REQUEST_TYPE, is_hxn ? VENDOR_WRITE_NREQUEST : VENDOR_WRITE_REQUEST, reg,
346 const uint16_t
value = (channel->
dtr_ ? CONTROL_DTR : 0) | (channel->
rts_ ? CONTROL_RTS : 0);
347 ESP_LOGD(TAG,
"PL2303: SET_CONTROL_REQUEST DTR=%s RTS=%s", ONOFF(channel->
dtr_), ONOFF(channel->
rts_));
uint8_t get_data_bits() const
uint32_t get_baud_rate() const
UARTFlowControl flow_control_
LineParity line_parity_() const
LineStopBits line_stop_bits_() const
void config_transfer_(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const std::vector< uint8_t > &data={}, uint8_t *dest=nullptr)
std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl) override
bool config_step(USBUartChannelBase *channel, uint8_t step, bool reload, bool ok, const uint8_t *response) override
Pl2303ChipType chip_type_
void modem_control_transfer(USBUartChannelBase *channel) override
@ UART_FLOW_CONTROL_RTS_CTS
@ UART_FLOW_CONTROL_XON_XOFF
constexpr uint8_t * put_le32(uint8_t *dst, uint32_t value)
uint8_t bulk_interface_number
uint8_t bulk_interface_class