1#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || defined(USE_ESP32_VARIANT_ESP32P4) || \
2 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
4#include "usb/usb_host.h"
14using namespace bytebuffer;
29static int ftdi_to_clkbits_am(
int baudrate,
uint32_t *encoded_divisor) {
30 static const char FRAC_CODE[8] = {0, 3, 2, 4, 1, 5, 6, 7};
31 static const char AM_ADJUST_UP[8] = {0, 0, 0, 1, 0, 3, 2, 1};
32 static const char AM_ADJUST_DN[8] = {0, 0, 0, 1, 0, 1, 2, 3};
33 int divisor, best_divisor, best_baud, best_baud_diff;
35 divisor = 24000000 / baudrate;
37 divisor -= AM_ADJUST_DN[divisor & 7];
42 for (i = 0; i < 2; i++) {
43 int try_divisor = divisor + i;
47 if (try_divisor <= 8) {
49 }
else if (divisor < 16) {
52 try_divisor += AM_ADJUST_UP[try_divisor & 7];
53 if (try_divisor > 0x1FFF8) {
55 try_divisor = 0x1FFF8;
58 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
59 if (baud_estimate < baudrate) {
60 baud_diff = baudrate - baud_estimate;
62 baud_diff = baud_estimate - baudrate;
64 if (i == 0 || baud_diff < best_baud_diff) {
65 best_divisor = try_divisor;
66 best_baud = baud_estimate;
67 best_baud_diff = baud_diff;
73 *encoded_divisor = (best_divisor >> 3) | (FRAC_CODE[best_divisor & 7] << 14);
74 if (*encoded_divisor == 1) {
76 }
else if (*encoded_divisor == 0x4001) {
82static int ftdi_to_clkbits(
int baudrate,
unsigned int clk,
int clk_div,
uint32_t *encoded_divisor) {
83 static const char FRAC_CODE[8] = {0, 3, 2, 4, 1, 5, 6, 7};
85 int divisor, best_divisor;
86 if (baudrate >= clk / clk_div) {
88 best_baud = clk / clk_div;
89 }
else if (baudrate >= clk / (clk_div + clk_div / 2)) {
91 best_baud = clk / (clk_div + clk_div / 2);
92 }
else if (baudrate >= clk / (2 * clk_div)) {
94 best_baud = clk / (2 * clk_div);
96 divisor = clk * 16 / clk_div / baudrate;
98 best_divisor = divisor / 2 + 1;
100 best_divisor = divisor / 2;
102 if (best_divisor > 0x20000)
103 best_divisor = 0x1ffff;
104 best_baud = clk * 16 / clk_div / best_divisor;
106 best_baud = best_baud / 2 + 1;
108 best_baud = best_baud / 2;
110 *encoded_divisor = (best_divisor >> 3) | (FRAC_CODE[best_divisor & 0x7] << 14);
121static FtdiConfig ftdi_convert_baudrate(
int baudrate, uint8_t chip_type, uint8_t channel_index) {
130 static constexpr uint32_t H_CLK = 120000000;
131 static constexpr uint32_t C_CLK = 48000000;
133 if (baudrate * 10 > H_CLK / 0x3fff) {
134 config.best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
135 encoded_divisor |= 0x20000;
137 config.best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
140 config.best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
142 config.best_baud = ftdi_to_clkbits_am(baudrate, &encoded_divisor);
145 config.value = (uint16_t) (encoded_divisor & 0xFFFF);
147 config.ftdi_index = (uint16_t) (encoded_divisor >> 8);
148 config.ftdi_index &= 0xFF00;
149 config.ftdi_index |= (channel_index + 1);
151 config.ftdi_index = (uint16_t) (encoded_divisor >> 16);
157static optional<CdcEps> get_uart(
const usb_config_desc_t *config_desc, uint8_t intf_idx) {
158 int conf_offset, ep_offset;
161 const auto *intf_desc = usb_parse_interface_descriptor(config_desc, intf_idx, 0, &conf_offset);
163 ESP_LOGD(TAG,
"usb_parse_interface_descriptor failed for intf_idx=%d (end of interfaces)", intf_idx);
167 "intf_desc [idx=%d]: bInterfaceClass=%02X, bInterfaceSubClass=%02X, bInterfaceProtocol=%02X, "
168 "bNumEndpoints=%d, bInterfaceNumber=%d",
169 intf_idx, intf_desc->bInterfaceClass, intf_desc->bInterfaceSubClass, intf_desc->bInterfaceProtocol,
170 intf_desc->bNumEndpoints, intf_desc->bInterfaceNumber);
172 std::vector<const usb_ep_desc_t *> endpoints;
173 for (uint8_t i = 0; i != intf_desc->bNumEndpoints; i++) {
174 ep_offset = conf_offset;
175 const auto *ep = usb_parse_endpoint_descriptor_by_index(intf_desc, i, config_desc->wTotalLength, &ep_offset);
177 ESP_LOGE(TAG,
"Ran out of endpoints at %d before finding all %d endpoints", i, intf_desc->bNumEndpoints);
180 ESP_LOGD(TAG,
"ep: bEndpointAddress=%02X, bmAttributes=%02X", ep->bEndpointAddress, ep->bmAttributes);
182 if (ep->bmAttributes != 0x2) {
183 ESP_LOGD(TAG,
"Skipping non-bulk endpoint: %02X", ep->bEndpointAddress);
186 endpoints.push_back(ep);
189 const usb_ep_desc_t *ep1 =
nullptr;
190 const usb_ep_desc_t *ep2 =
nullptr;
191 for (
const auto *ep : endpoints) {
192 if (ep1 ==
nullptr) {
194 }
else if (ep2 ==
nullptr) {
200 if (ep1 ==
nullptr || ep2 ==
nullptr) {
201 ESP_LOGD(TAG,
"Interface %d has %zu endpoints (need 2 bulk endpoints)", intf_idx, endpoints.size());
205 ESP_LOGD(TAG,
"Interface %d: ep1=0x%02X, ep2=0x%02X", intf_idx, ep1->bEndpointAddress, ep2->bEndpointAddress);
207 if (ep1->bEndpointAddress & usb_host::USB_DIR_IN) {
210 ESP_LOGD(TAG,
"ep1 is IN (RX): ep1=0x%02X (in_ep), ep2=0x%02X (out_ep)", ep1->bEndpointAddress,
211 ep2->bEndpointAddress);
215 ESP_LOGD(TAG,
"ep1 is OUT (TX): ep1=0x%02X (out_ep), ep2=0x%02X (in_ep)", ep1->bEndpointAddress,
216 ep2->bEndpointAddress);
219 eps.bulk_interface_number = intf_desc->bInterfaceNumber;
224 const usb_config_desc_t *config_desc;
225 const usb_device_desc_t *device_desc;
226 std::vector<CdcEps> cdc_devs{};
227 std::string type_string;
229 if (usb_host_get_device_descriptor(dev_hdl, &device_desc) != ESP_OK) {
230 ESP_LOGE(TAG,
"get_device_descriptor failed");
233 if (usb_host_get_active_config_descriptor(dev_hdl, &config_desc) != ESP_OK) {
234 ESP_LOGE(TAG,
"get_active_config_descriptor failed");
237 if (device_desc->bcdDevice == 0x400 || (device_desc->bcdDevice == 0x200 && device_desc->iSerialNumber == 0)) {
239 type_string =
"BM type chip";
240 }
else if (device_desc->bcdDevice == 0x200) {
242 type_string =
"AM type chip";
243 }
else if (device_desc->bcdDevice == 0x500) {
245 type_string =
"2232C chip";
246 }
else if (device_desc->bcdDevice == 0x600) {
248 type_string =
"type R chip";
249 }
else if (device_desc->bcdDevice == 0x700) {
251 type_string =
"2232H chip";
252 }
else if (device_desc->bcdDevice == 0x800) {
254 type_string =
"4232H chip";
255 }
else if (device_desc->bcdDevice == 0x900) {
257 type_string =
"232H type chip";
258 }
else if (device_desc->bcdDevice == 0x1000) {
260 type_string =
"230x chip";
263 ESP_LOGD(TAG,
"Found FTDI %s based device", type_string.c_str());
264 for (
size_t intf_idx = 0; intf_idx < this->
channels_.size(); intf_idx++) {
265 if (
auto eps = get_uart(config_desc,
static_cast<uint8_t
>(intf_idx))) {
266 cdc_devs.push_back(*eps);
267 ESP_LOGD(TAG,
"Found CDC interface at USB interface index %zu", intf_idx);
280 auto started =
false;
281 if (!channel->
input_started_.compare_exchange_strong(started,
true))
288 ESP_LOGE(TAG,
"RX Transfer failed, status=%s", esp_err_to_name(
status.error_code));
294 size_t uart_data_len = (
status.data_len > 2) ? (
status.data_len - 2) : 0;
296 if (uart_data_len > 0) {
297 ESP_LOGV(TAG,
"RX callback: Received %zu bytes, channel=%d", uart_data_len, channel->
index_);
300 if (chunk ==
nullptr) {
310 memcpy(chunk->
data,
status.data + 2, uart_data_len);
311 chunk->
length =
static_cast<uint16_t
>(uart_data_len);
314#ifdef USE_UART_DEBUGGER
317 std::vector<uint8_t>(
status.data + 2,
status.data + 2 + uart_data_len),
',',
324 }
else if (
status.data_len >= 2) {
325 ESP_LOGVV(TAG,
"RX: Status packet, modem=0x%02X line=0x%02X, ch=%d",
status.data[0],
status.data[1],
333 if (!this->
transfer_in(ep->bEndpointAddress, callback, ep->wMaxPacketSize)) {
334 ESP_LOGE(TAG,
"RX transfer submission failed for ep=0x%02X", ep->bEndpointAddress);
340 ESP_LOGW(TAG,
"RX buffer overflow on channel %d, clearing to resync", channel->
index_);
345 const uint8_t *response) {
356 auto config = ftdi_convert_baudrate(channel->
baud_rate_, this->chip_type_, channel->
index_);
358 ESP_LOGD(TAG,
"Baudrate: %u, value=0x%04X, ftdi_index=0x%04X", (
unsigned) channel->
baud_rate_, config.value,
360 this->
config_transfer_(USB_VENDOR_DEV | usb_host::USB_DIR_OUT, 0x03, config.value, usb_index);
367 value |= (0x00 << 8);
370 value |= (0x01 << 8);
373 value |= (0x02 << 8);
376 value |= (0x03 << 8);
379 value |= (0x04 << 8);
384 value |= (0x00 << 11);
387 value |= (0x01 << 11);
390 value |= (0x02 << 11);
393 value |= (0x00 << 14);
401 this->
config_transfer_(USB_VENDOR_DEV | usb_host::USB_DIR_OUT, 0x01, 0x0000,
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
static void log_hex(UARTDirection direction, std::vector< uint8_t > bytes, uint8_t separator, StringRef prefix=StringRef())
Log the bytes as hex values, separated by the provided separator character.
bool transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length)
Performs a transfer input operation.
std::atomic< bool > input_started_
std::atomic< bool > initialised_
UARTParityOptions parity_
void config_transfer_(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const std::vector< uint8_t > &data={})
std::vector< USBUartChannel * > channels_
LockFreeQueue< UsbDataChunk, USB_DATA_QUEUE_SIZE > usb_data_queue_
EventPool< UsbDataChunk, USB_DATA_QUEUE_SIZE - 1 > chunk_pool_
bool config_step(USBUartChannel *channel, uint8_t step, bool reload, bool ok, const uint8_t *response) override
void on_rx_overflow(USBUartChannel *channel) override
std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl) override
void start_input(USBUartChannel *channel) override
@ UART_CONFIG_STOP_BITS_1_5
@ UART_CONFIG_STOP_BITS_2
@ UART_CONFIG_PARITY_SPACE
@ UART_CONFIG_PARITY_MARK
@ UART_CONFIG_PARITY_NONE
@ UART_CONFIG_PARITY_EVEN
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t bulk_interface_number
const usb_ep_desc_t * in_ep
uint8_t data[usb_host::USB_MAX_PACKET_SIZE]