ESPHome 2025.6.0
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;
29};
30
38
44
45static const char *const PARITY_NAMES[] = {"NONE", "ODD", "EVEN", "MARK", "SPACE"};
46static const char *const STOP_BITS_NAMES[] = {"1", "1.5", "2"};
47
49 public:
50 RingBuffer(uint16_t buffer_size) : buffer_size_(buffer_size), buffer_(new uint8_t[buffer_size]) {}
51 bool is_empty() const { return this->read_pos_ == this->insert_pos_; }
52 size_t get_available() const {
53 return (this->insert_pos_ + this->buffer_size_ - this->read_pos_) % this->buffer_size_;
54 };
55 size_t get_free_space() const { return this->buffer_size_ - 1 - this->get_available(); }
56 uint8_t peek() const { return this->buffer_[this->read_pos_]; }
57 void push(uint8_t item);
58 void push(const uint8_t *data, size_t len);
59 uint8_t pop();
60 size_t pop(uint8_t *data, size_t len);
61 void clear() { this->read_pos_ = this->insert_pos_ = 0; }
62
63 protected:
64 uint16_t insert_pos_ = 0;
65 uint16_t read_pos_ = 0;
66 uint16_t buffer_size_;
67 uint8_t *buffer_;
68};
69
70class USBUartChannel : public uart::UARTComponent, public Parented<USBUartComponent> {
71 friend class USBUartComponent;
72 friend class USBUartTypeCdcAcm;
73 friend class USBUartTypeCP210X;
74 friend class USBUartTypeCH34X;
75
76 public:
77 USBUartChannel(uint8_t index, uint16_t buffer_size)
78 : index_(index), input_buffer_(RingBuffer(buffer_size)), output_buffer_(RingBuffer(buffer_size)) {}
79 void write_array(const uint8_t *data, size_t len) override;
80 ;
81 bool peek_byte(uint8_t *data) override;
82 ;
83 bool read_array(uint8_t *data, size_t len) override;
84 int available() override { return static_cast<int>(this->input_buffer_.get_available()); }
85 void flush() override {}
86 void check_logger_conflict() override {}
87 void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
88 void set_debug(bool debug) { this->debug_ = debug; }
89 void set_dummy_receiver(bool dummy_receiver) { this->dummy_receiver_ = dummy_receiver; }
90
91 protected:
92 const uint8_t index_;
96 bool input_started_{true};
97 bool output_started_{true};
99 bool debug_{};
102};
103
105 public:
106 USBUartComponent(uint16_t vid, uint16_t pid) : usb_host::USBClient(vid, pid) {}
107 void setup() override;
108 void loop() override;
109 void dump_config() override;
110 std::vector<USBUartChannel *> get_channels() { return this->channels_; }
111
112 void add_channel(USBUartChannel *channel) { this->channels_.push_back(channel); }
113
114 void start_input(USBUartChannel *channel);
115 void start_output(USBUartChannel *channel);
116
117 protected:
118 std::vector<USBUartChannel *> channels_{};
119};
120
122 public:
123 USBUartTypeCdcAcm(uint16_t vid, uint16_t pid) : USBUartComponent(vid, pid) {}
124
125 protected:
126 virtual std::vector<CdcEps> parse_descriptors_(usb_device_handle_t dev_hdl);
127 void on_connected() override;
128 virtual void enable_channels();
129 void on_disconnected() override;
130};
131
133 public:
134 USBUartTypeCP210X(uint16_t vid, uint16_t pid) : USBUartTypeCdcAcm(vid, pid) {}
135
136 protected:
137 std::vector<CdcEps> parse_descriptors_(usb_device_handle_t dev_hdl) override;
138 void enable_channels() override;
139};
141 public:
142 USBUartTypeCH34X(uint16_t vid, uint16_t pid) : USBUartTypeCdcAcm(vid, pid) {}
143
144 protected:
145 void enable_channels() override;
146};
147
148} // namespace usb_uart
149} // namespace esphome
150
151#endif // USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3
Helper class to easily give an object a parent of type T.
Definition helpers.h:539
USBClient(uint16_t vid, uint16_t pid)
Definition usb_host.h:66
void push(uint8_t item)
Definition usb_uart.cpp:112
RingBuffer(uint16_t buffer_size)
Definition usb_uart.h:50
size_t get_free_space() const
Definition usb_uart.h:55
size_t get_available() const
Definition usb_uart.h:52
void set_dummy_receiver(bool dummy_receiver)
Definition usb_uart.h:89
bool peek_byte(uint8_t *data) override
Definition usb_uart.cpp:151
void set_parity(UARTParityOptions parity)
Definition usb_uart.h:87
void write_array(const uint8_t *data, size_t len) override
Definition usb_uart.cpp:136
void check_logger_conflict() override
Definition usb_uart.h:86
bool read_array(uint8_t *data, size_t len) override
Definition usb_uart.cpp:158
USBUartChannel(uint8_t index, uint16_t buffer_size)
Definition usb_uart.h:77
void add_channel(USBUartChannel *channel)
Definition usb_uart.h:112
USBUartComponent(uint16_t vid, uint16_t pid)
Definition usb_uart.h:106
std::vector< USBUartChannel * > channels_
Definition usb_uart.h:118
void start_output(USBUartChannel *channel)
Definition usb_uart.cpp:224
void start_input(USBUartChannel *channel)
Definition usb_uart.cpp:193
std::vector< USBUartChannel * > get_channels()
Definition usb_uart.h:110
void enable_channels() override
CH34x.
Definition ch34x.cpp:16
USBUartTypeCH34X(uint16_t vid, uint16_t pid)
Definition usb_uart.h:142
USBUartTypeCP210X(uint16_t vid, uint16_t pid)
Definition usb_uart.h:134
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:123
virtual std::vector< CdcEps > parse_descriptors_(usb_device_handle_t dev_hdl)
Definition usb_uart.cpp:66
const char *const TAG
Definition spi.cpp:8
@ UART_CONFIG_STOP_BITS_1_5
Definition usb_uart.h:41
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
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