ESPHome 2025.6.0
Loading...
Searching...
No Matches
ch34x.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
2#include "usb_uart.h"
3#include "usb/usb_host.h"
4#include "esphome/core/log.h"
5
7
8namespace esphome {
9namespace usb_uart {
10
11using namespace bytebuffer;
17 // enable the channels
18 for (auto channel : this->channels_) {
19 if (!channel->initialised_)
20 continue;
22 if (!status.success) {
23 ESP_LOGE(TAG, "Control transfer failed, status=%s", esp_err_to_name(status.error_code));
24 channel->initialised_ = false;
25 }
26 };
27
28 uint8_t divisor = 7;
29 uint32_t clk = 12000000;
30
31 auto baud_rate = channel->baud_rate_;
32 if (baud_rate < 256000) {
33 if (baud_rate > 6000000 / 255) {
34 divisor = 3;
35 clk = 6000000;
36 } else if (baud_rate > 750000 / 255) {
37 divisor = 2;
38 clk = 750000;
39 } else if (baud_rate > 93750 / 255) {
40 divisor = 1;
41 clk = 93750;
42 } else {
43 divisor = 0;
44 clk = 11719;
45 }
46 }
47 ESP_LOGV(TAG, "baud_rate: %" PRIu32 ", divisor: %d, clk: %" PRIu32, baud_rate, divisor, clk);
48 auto factor = static_cast<uint8_t>(clk / baud_rate);
49 if (factor == 0 || factor == 0xFF) {
50 ESP_LOGE(TAG, "Invalid baud rate %" PRIu32, baud_rate);
51 channel->initialised_ = false;
52 continue;
53 }
54 if ((clk / factor - baud_rate) > (baud_rate - clk / (factor + 1)))
55 factor++;
56 factor = 256 - factor;
57
58 uint16_t value = 0xC0;
59 if (channel->stop_bits_ == UART_CONFIG_STOP_BITS_2)
60 value |= 4;
61 switch (channel->parity_) {
63 break;
64 default:
65 value |= 8 | ((channel->parity_ - 1) << 4);
66 break;
67 }
68 value |= channel->data_bits_ - 5;
69 value <<= 8;
70 value |= 0x8C;
71 uint8_t cmd = 0xA1 + channel->index_;
72 if (channel->index_ >= 2)
73 cmd += 0xE;
74 this->control_transfer(USB_VENDOR_DEV | usb_host::USB_DIR_OUT, cmd, value, (factor << 8) | divisor, callback);
75 }
77}
78} // namespace usb_uart
79} // namespace esphome
80#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
uint8_t status
Definition bl0942.h:8
bool control_transfer(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const transfer_cb_t &callback, const std::vector< uint8_t > &data={})
std::vector< USBUartChannel * > channels_
Definition usb_uart.h:118
void enable_channels() override
CH34x.
Definition ch34x.cpp:16
std::function< void(const TransferStatus &)> transfer_cb_t
Definition usb_host.h:40
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7