ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
usb_uart.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
8
9namespace esphome {
10namespace usb_uart {
11class USBUartTypeCdcAcm;
12class USBUartComponent;
13
14static const char *const TAG = "usb_uart";
15
16static constexpr uint8_t USB_CDC_SUBCLASS_ACM = 0x02;
17static constexpr uint8_t USB_SUBCLASS_COMMON = 0x02;
18static constexpr uint8_t USB_SUBCLASS_NULL = 0x00;
19static constexpr uint8_t USB_PROTOCOL_NULL = 0x00;
20static constexpr uint8_t USB_DEVICE_PROTOCOL_IAD = 0x01;
21static constexpr uint8_t USB_VENDOR_IFC = usb_host::USB_TYPE_VENDOR | usb_host::USB_RECIP_INTERFACE;
22static constexpr uint8_t USB_VENDOR_DEV = usb_host::USB_TYPE_VENDOR | usb_host::USB_RECIP_DEVICE;
23
24struct CdcEps {
25 const usb_ep_desc_t *notify_ep;
26 const usb_ep_desc_t *in_ep;
27 const usb_ep_desc_t *out_ep;
30};
31
39
45
46static const char *const PARITY_NAMES[] = {"NONE", "ODD", "EVEN", "MARK", "SPACE"};
47static const char *const STOP_BITS_NAMES[] = {"1", "1.5", "2"};
48
50 public:
51 RingBuffer(uint16_t buffer_size) : buffer_size_(buffer_size), buffer_(new uint8_t[buffer_size]) {}
52 bool is_empty() const { return this->read_pos_ == this->insert_pos_; }
53 size_t get_available() const {
54 return (this->insert_pos_ + this->buffer_size_ - this->read_pos_) % this->buffer_size_;
55 };
56 size_t get_free_space() const { return this->buffer_size_ - 1 - this->get_available(); }
57 uint8_t peek() const { return this->buffer_[this->read_pos_]; }
58 void push(uint8_t item);
59 void push(const uint8_t *data, size_t len);
60 uint8_t pop();
61 size_t pop(uint8_t *data, size_t len);
62 void clear() { this->read_pos_ = this->insert_pos_ = 0; }
63
64 protected:
65 uint16_t insert_pos_ = 0;
66 uint16_t read_pos_ = 0;
67 uint16_t buffer_size_;
68 uint8_t *buffer_;
69};
70
71class USBUartChannel : public uart::UARTComponent, public Parented<USBUartComponent> {
72 friend class USBUartComponent;
73 friend class USBUartTypeCdcAcm;
74 friend class USBUartTypeCP210X;
75 friend class USBUartTypeCH34X;
76
77 public:
78 USBUartChannel(uint8_t index, uint16_t buffer_size)
79 : index_(index), input_buffer_(RingBuffer(buffer_size)), output_buffer_(RingBuffer(buffer_size)) {}
80 void write_array(const uint8_t *data, size_t len) override;
81 ;
82 bool peek_byte(uint8_t *data) override;
83 ;
84 bool read_array(uint8_t *data, size_t len) override;
85 int available() override { return static_cast<int>(this->input_buffer_.get_available()); }
86 void flush() override {}
87 void check_logger_conflict() override {}
88 void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
89 void set_debug(bool debug) { this->debug_ = debug; }
90 void set_dummy_receiver(bool dummy_receiver) { this->dummy_receiver_ = dummy_receiver; }
91
92 protected:
93 const uint8_t index_;
97 bool input_started_{true};
98 bool output_started_{true};
100 bool debug_{};
103};
104
106 public:
107 USBUartComponent(uint16_t vid, uint16_t pid) : usb_host::USBClient(vid, pid) {}
108 void setup() override;
109 void loop() override;
110 void dump_config() override;
111 std::vector<USBUartChannel *> get_channels() { return this->channels_; }
112
113 void add_channel(USBUartChannel *channel) { this->channels_.push_back(channel); }
114
115 void start_input(USBUartChannel *channel);
116 void start_output(USBUartChannel *channel);
117
118 protected:
119 std::vector<USBUartChannel *> channels_{};
120};
121
123 public:
124 USBUartTypeCdcAcm(uint16_t vid, uint16_t pid) : USBUartComponent(vid, pid) {}
125
126 protected:
127 virtual std::vector<CdcEps> parse_descriptors(usb_device_handle_t dev_hdl);
128 void on_connected() override;
129 virtual void enable_channels();
130 void on_disconnected() override;
131};
132
134 public:
135 USBUartTypeCP210X(uint16_t vid, uint16_t pid) : USBUartTypeCdcAcm(vid, pid) {}
136
137 protected:
138 std::vector<CdcEps> parse_descriptors(usb_device_handle_t dev_hdl) override;
139 void enable_channels() override;
140};
142 public:
143 USBUartTypeCH34X(uint16_t vid, uint16_t pid) : USBUartTypeCdcAcm(vid, pid) {}
144
145 protected:
146 void enable_channels() override;
147};
148
149} // namespace usb_uart
150} // namespace esphome
151
152#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
Helper class to easily give an object a parent of type T.
Definition helpers.h:656
USBClient(uint16_t vid, uint16_t pid)
Definition usb_host.h:66
void push(uint8_t item)
Definition usb_uart.cpp:108
RingBuffer(uint16_t buffer_size)
Definition usb_uart.h:51
size_t get_free_space() const
Definition usb_uart.h:56
size_t get_available() const
Definition usb_uart.h:53
void set_dummy_receiver(bool dummy_receiver)
Definition usb_uart.h:90
bool peek_byte(uint8_t *data) override
Definition usb_uart.cpp:147
void set_parity(UARTParityOptions parity)
Definition usb_uart.h:88
void write_array(const uint8_t *data, size_t len) override
Definition usb_uart.cpp:132
void check_logger_conflict() override
Definition usb_uart.h:87
bool read_array(uint8_t *data, size_t len) override
Definition usb_uart.cpp:154
USBUartChannel(uint8_t index, uint16_t buffer_size)
Definition usb_uart.h:78
void add_channel(USBUartChannel *channel)
Definition usb_uart.h:113
USBUartComponent(uint16_t vid, uint16_t pid)
Definition usb_uart.h:107
std::vector< USBUartChannel * > channels_
Definition usb_uart.h:119
void start_output(USBUartChannel *channel)
Definition usb_uart.cpp:220
void start_input(USBUartChannel *channel)
Definition usb_uart.cpp:189
std::vector< USBUartChannel * > get_channels()
Definition usb_uart.h:111
void enable_channels() override
CH34x.
Definition ch34x.cpp:16
USBUartTypeCH34X(uint16_t vid, uint16_t pid)
Definition usb_uart.h:143
USBUartTypeCP210X(uint16_t vid, uint16_t pid)
Definition usb_uart.h:135
std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl) override
Definition cp210x.cpp:46
USBUartTypeCdcAcm(uint16_t vid, uint16_t pid)
Definition usb_uart.h:124
virtual std::vector< CdcEps > parse_descriptors(usb_device_handle_t dev_hdl)
Definition usb_uart.cpp:62
const char *const TAG
Definition spi.cpp:8
@ UART_CONFIG_STOP_BITS_1_5
Definition usb_uart.h:42
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
const usb_ep_desc_t * out_ep
Definition usb_uart.h:27
const usb_ep_desc_t * notify_ep
Definition usb_uart.h:25
const usb_ep_desc_t * in_ep
Definition usb_uart.h:26
uint8_t interrupt_interface_number
Definition usb_uart.h:29