ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
pl2303.cpp
Go to the documentation of this file.
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)
3#include "usb_uart.h"
4#include "usb/usb_host.h"
5#include "esphome/core/log.h"
6#include <cinttypes>
7
8namespace esphome::usb_uart {
9
10// Control request types
11static constexpr uint8_t SET_LINE_REQUEST_TYPE = 0x21;
12static constexpr uint8_t SET_LINE_REQUEST = 0x20;
13
14static constexpr uint8_t SET_CONTROL_REQUEST_TYPE = 0x21;
15static constexpr uint8_t SET_CONTROL_REQUEST = 0x22;
16static constexpr uint8_t CONTROL_DTR = 0x01;
17static constexpr uint8_t CONTROL_RTS = 0x02;
18
19static constexpr uint8_t VENDOR_WRITE_REQUEST_TYPE = 0x40;
20static constexpr uint8_t VENDOR_WRITE_REQUEST = 0x01;
21
22static constexpr uint8_t VENDOR_READ_REQUEST_TYPE = 0xc0;
23static constexpr uint8_t VENDOR_READ_REQUEST = 0x01;
24
25// Supported standard baud rates for direct encoding (TYPE_H, TYPE_HX, TYPE_HXD, TYPE_HXN)
26static const uint32_t SUPPORTED_BAUD_RATES[] = {
27 75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 14400, 19200,
28 28800, 38400, 57600, 115200, 230400, 460800, 614400, 921600, 1228800, 2457600, 3000000, 6000000,
29};
30
31static const char *pl2303_type_name(Pl2303ChipType type) {
32 switch (type) {
33 case PL2303_TYPE_H:
34 return "H (legacy)";
35 case PL2303_TYPE_HX:
36 return "HX";
37 case PL2303_TYPE_TA:
38 return "TA";
39 case PL2303_TYPE_TB:
40 return "TB";
41 case PL2303_TYPE_HXD:
42 return "HXD";
43 case PL2303_TYPE_HXN:
44 return "G/HXN (newer)";
45 default:
46 return "unknown";
47 }
48}
49
50// Find nearest supported baud rate for direct encoding
51static uint32_t nearest_supported_baud(uint32_t baud) {
52 size_t n = sizeof(SUPPORTED_BAUD_RATES) / sizeof(SUPPORTED_BAUD_RATES[0]);
53 for (size_t i = 0; i < n; i++) {
54 if (SUPPORTED_BAUD_RATES[i] > baud) {
55 if (i == 0)
56 return SUPPORTED_BAUD_RATES[0];
57 uint32_t lower = SUPPORTED_BAUD_RATES[i - 1];
58 uint32_t upper = SUPPORTED_BAUD_RATES[i];
59 return (upper - baud) > (baud - lower) ? lower : upper;
60 }
61 }
62 return SUPPORTED_BAUD_RATES[n - 1];
63}
64
65// Direct encoding: little-endian 32-bit baud rate value
66static void encode_baud_direct(uint8_t buf[4], uint32_t baud) {
67 buf[0] = baud & 0xFF;
68 buf[1] = (baud >> 8) & 0xFF;
69 buf[2] = (baud >> 16) & 0xFF;
70 buf[3] = (baud >> 24) & 0xFF;
71}
72
73// Divisor encoding for TYPE_HX, TYPE_HXD: baudrate = 12M*32 / (mantissa * 4^exponent)
74static void encode_baud_divisor(uint8_t buf[4], uint32_t baud) {
75 static constexpr uint32_t BASELINE = 12000000 * 32;
76 uint32_t mantissa = BASELINE / baud;
77 if (mantissa == 0)
78 mantissa = 1;
79 uint8_t exponent = 0;
80 while (mantissa >= 512) {
81 if (exponent < 7) {
82 mantissa >>= 2;
83 exponent++;
84 } else {
85 mantissa = 511;
86 break;
87 }
88 }
89 buf[3] = 0x80;
90 buf[2] = 0;
91 buf[1] = (exponent << 1) | (mantissa >> 8);
92 buf[0] = mantissa & 0xFF;
93}
94
95// Alt divisor encoding for TYPE_TA, TYPE_TB: baudrate = 12M*32 / (mantissa * 2^exponent)
96static void encode_baud_divisor_alt(uint8_t buf[4], uint32_t baud) {
97 static constexpr uint32_t BASELINE = 12000000 * 32;
98 uint32_t mantissa = BASELINE / baud;
99 if (mantissa == 0)
100 mantissa = 1;
101 uint8_t exponent = 0;
102 while (mantissa >= 2048) {
103 if (exponent < 15) {
104 mantissa >>= 1;
105 exponent++;
106 } else {
107 mantissa = 2047;
108 break;
109 }
110 }
111 buf[3] = 0x80;
112 buf[2] = exponent & 0x01;
113 buf[1] = ((exponent & ~0x01) << 4) | (mantissa >> 8);
114 buf[0] = mantissa & 0xFF;
115}
116
117std::vector<CdcEps> USBUartTypePL2303::parse_descriptors(usb_device_handle_t dev_hdl) {
118 const usb_config_desc_t *config_desc;
119 const usb_device_desc_t *device_desc;
120 std::vector<CdcEps> cdc_devs{};
121
122 if (usb_host_get_device_descriptor(dev_hdl, &device_desc) != ESP_OK) {
123 ESP_LOGE(TAG, "PL2303: get_device_descriptor failed");
124 return {};
125 }
126 if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) != ESP_OK) {
127 ESP_LOGE(TAG, "PL2303: get_active_config_descriptor failed");
128 return {};
129 }
130
131 // Detect chip type from USB descriptor fields (mirrors pl2303_detect_type in Linux driver)
132 uint16_t bcd_device = device_desc->bcdDevice;
133 uint16_t bcd_usb = device_desc->bcdUSB;
134 uint8_t bmax_packet = device_desc->bMaxPacketSize0;
135 uint8_t bdev_class = device_desc->bDeviceClass;
136
137 if (bdev_class == 0x02 || bmax_packet != 0x40) {
139 } else {
140 switch (bcd_usb) {
141 case 0x0101:
142 case 0x0110:
143 this->chip_type_ = (bcd_device == 0x0400) ? PL2303_TYPE_HXD : PL2303_TYPE_HX;
144 break;
145 default:
146 // TA and TB are distinguishable by bcdDevice without any USB probe.
147 if (bcd_device == 0x0300) {
149 } else if (bcd_device == 0x0500) {
151 } else {
153 }
154 break;
155 }
156 }
157
158 ESP_LOGI(TAG, "PL2303 chip type: %s (bcdUSB=0x%04X bcdDevice=0x%04X bMaxPkt=%u)", pl2303_type_name(this->chip_type_),
159 bcd_usb, bcd_device, bmax_packet);
160
161 // PL2303 is single-port: find first interface with 2 bulk endpoints
162 int conf_offset = 0;
163 for (uint8_t i = 0; i < config_desc->bNumInterfaces; i++) {
164 int ep_offset = conf_offset;
165 const auto *intf = usb_parse_interface_descriptor(config_desc, i, 0, &conf_offset);
166 if (!intf)
167 break;
168 if (intf->bNumEndpoints < 2)
169 continue;
170
171 const usb_ep_desc_t *in_ep = nullptr;
172 const usb_ep_desc_t *out_ep = nullptr;
173 const usb_ep_desc_t *notify_ep = nullptr;
174
175 for (uint8_t e = 0; e < intf->bNumEndpoints; e++) {
176 ep_offset = conf_offset;
177 const auto *ep = usb_parse_endpoint_descriptor_by_index(intf, e, config_desc->wTotalLength, &ep_offset);
178 if (!ep)
179 break;
180 if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_BULK) {
181 if (ep->bEndpointAddress & usb_host::USB_DIR_IN) {
182 in_ep = ep;
183 } else {
184 out_ep = ep;
185 }
186 } else if (ep->bmAttributes == USB_BM_ATTRIBUTES_XFER_INT) {
187 notify_ep = ep;
188 }
189 }
190
191 if (in_ep && out_ep) {
192 cdc_devs.push_back(CdcEps{notify_ep, in_ep, out_ep, intf->bInterfaceNumber, intf->bInterfaceNumber});
193 break; // PL2303 is single-port
194 }
195 }
196
197 if (cdc_devs.empty())
198 ESP_LOGE(TAG, "PL2303: failed to find bulk IN+OUT endpoints");
199
200 return cdc_devs;
201}
202
203// Vendor init sequence for non-HXN chips (mirrors pl2303_startup in the Linux driver):
204// read 0x8484, write 0x0404=0, read 0x8484, read 0x8383, read 0x8484, write 0x0404=1,
205// read 0x8484, read 0x8383, write 0=1, write 1=0, write 2=0x24 (legacy) or 0x44 (HX+).
206// The final entry's wIndex is patched at runtime depending on the chip type.
207struct Pl2303InitStep {
208 uint8_t type;
209 uint8_t request;
210 uint16_t value;
211 uint16_t index;
212 bool read; // reads need a 1-byte buffer to set wLength=1 so the IN data stage runs
213};
214static const Pl2303InitStep PL2303_INIT[] = {
215 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0, true},
216 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0, false},
217 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0, true},
218 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0, true},
219 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0, true},
220 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1, false},
221 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0, true},
222 {VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0, true},
223 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1, false},
224 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0, false},
225 {VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0, false},
226};
227static constexpr uint8_t PL2303_INIT_COUNT = sizeof(PL2303_INIT) / sizeof(PL2303_INIT[0]);
228
229bool USBUartTypePL2303::config_step(USBUartChannel *channel, uint8_t step, bool reload, bool ok,
230 const uint8_t *response) {
231 bool is_legacy = (this->chip_type_ == PL2303_TYPE_H);
232 bool is_hxn = (this->chip_type_ == PL2303_TYPE_HXN);
233
234 // Vendor init burst runs only on full init for non-HXN chips.
235 uint8_t init_count = (!reload && !is_hxn) ? PL2303_INIT_COUNT : 0;
236 if (step < init_count) {
237 const auto &e = PL2303_INIT[step];
238 uint16_t index = (step == PL2303_INIT_COUNT - 1) ? (is_legacy ? 0x24 : 0x44) : e.index;
239 this->config_transfer_(e.type, e.request, e.value, index,
240 e.read ? std::vector<uint8_t>{0} : std::vector<uint8_t>{});
241 return true;
242 }
243 step -= init_count;
244
245 uint16_t iface = channel->cdc_dev_.bulk_interface_number;
246 switch (step) {
247 case 0: {
248 // Build 7-byte line coding structure:
249 // [0-3] baud rate (LE32), [4] stop bits, [5] parity, [6] data bits
250 uint8_t line_coding[7] = {};
251 uint32_t baud = channel->get_baud_rate();
252
253 // Choose baud encoding based on chip type
254 uint32_t nearest = nearest_supported_baud(baud);
255 if (baud == nearest || this->chip_type_ == PL2303_TYPE_HXN) {
256 encode_baud_direct(line_coding, baud);
257 } else if (this->chip_type_ == PL2303_TYPE_TA || this->chip_type_ == PL2303_TYPE_TB) {
258 encode_baud_divisor_alt(line_coding, baud);
259 } else {
260 encode_baud_divisor(line_coding, baud);
261 }
262
263 // Stop bits: 0=1, 1=1.5, 2=2
264 switch (channel->get_stop_bits()) {
265 case 2:
266 line_coding[4] = 2;
267 break;
268 default:
269 line_coding[4] = 0;
270 break;
271 }
272
273 // Parity: 0=none, 1=odd, 2=even, 3=mark, 4=space
274 switch (channel->parity_) {
276 line_coding[5] = 1;
277 break;
279 line_coding[5] = 2;
280 break;
282 line_coding[5] = 3;
283 break;
285 line_coding[5] = 4;
286 break;
287 default:
288 line_coding[5] = 0;
289 break;
290 }
291
292 // Data bits
293 line_coding[6] = channel->get_data_bits();
294
295 ESP_LOGD(TAG, "PL2303: SET_LINE_REQUEST baud=%u stop=%u parity=%u data=%u", baud, line_coding[4], line_coding[5],
296 line_coding[6]);
297
298 std::vector<uint8_t> lc_vec(line_coding, line_coding + 7);
299 this->config_transfer_(SET_LINE_REQUEST_TYPE, SET_LINE_REQUEST, 0, iface, lc_vec);
300 return true;
301 }
302 case 1:
303 // Assert DTR + RTS (init only)
304 if (reload)
305 return false;
306 this->config_transfer_(SET_CONTROL_REQUEST_TYPE, SET_CONTROL_REQUEST, CONTROL_DTR | CONTROL_RTS, iface);
307 return true;
308 default:
309 return false;
310 }
311}
312
313} // namespace esphome::usb_uart
314#endif // USE_ESP32_VARIANT_ESP32P4 || USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 ||
315 // USE_ESP32_VARIANT_ESP32S31 || USE_ESP32_VARIANT_ESP32H4
void config_transfer_(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const std::vector< uint8_t > &data={})
Definition usb_uart.cpp:565
std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl) override
Definition pl2303.cpp:117
bool config_step(USBUartChannel *channel, uint8_t step, bool reload, bool ok, const uint8_t *response) override
Definition pl2303.cpp:229
uint16_t type
static void uint32_t