forked from bertmelis/USBHostSerial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSBHostSerial.cpp
More file actions
264 lines (233 loc) · 7.76 KB
/
USBHostSerial.cpp
File metadata and controls
264 lines (233 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
Copyright (c) 2024 Bert Melis. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT> or
the LICENSE file.
Based on example code:
SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
SPDX-License-Identifier: CC0-1.0
*/
#include "USBHostSerial.h"
using namespace esp_usb;
USBHostSerial::USBHostSerial(uint16_t vid, uint16_t pid)
: _host_config{}
, _line_coding{}
, _tx_buf_mem{}
, _tx_buf_handle(nullptr)
, _tx_buf_data{}
, _rx_buf_mem{}
, _rx_buf_handle(nullptr)
, _rx_buf_data{}
, _setupDone(false)
, _fallback(false)
, _vid(vid)
, _pid(pid)
, _device_disconnected_sem(nullptr)
, _usb_lib_task_handle(nullptr)
, _logger(nullptr) {
_tx_buf_handle = xRingbufferCreateStatic(USBHOSTSERIAL_BUFFERSIZE, RINGBUF_TYPE_BYTEBUF, _tx_buf_mem, &_tx_buf_data);
_rx_buf_handle = xRingbufferCreateStatic(USBHOSTSERIAL_BUFFERSIZE, RINGBUF_TYPE_BYTEBUF, _rx_buf_mem, &_rx_buf_data);
if (!_tx_buf_handle || !_rx_buf_handle) {
abort();
}
}
USBHostSerial::~USBHostSerial() {
// TODO (bertmelis): implement destruction.
vRingbufferDelete(_tx_buf_handle);
vRingbufferDelete(_rx_buf_handle);
abort();
}
USBHostSerial::operator bool() const {
if (xSemaphoreTake(_device_disconnected_sem, 0) == pdTRUE) {
xSemaphoreGive(_device_disconnected_sem);
return false;
}
return true;
}
bool USBHostSerial::begin(int baud, int stopbits, int parity, int databits) {
if (!_setupDone) {
_setupDone = true;
_setup();
}
_line_coding.dwDTERate = baud;
_line_coding.bCharFormat = stopbits;
_line_coding.bParityType = parity;
_line_coding.bDataBits = databits;
if (xTaskCreate(_USBHostSerial_task, "usb_dev_lib", 4096, this, 1, &_USBHostSerial_task_handle) == pdTRUE) {
_log("USB setup done");
return true;
}
_log("USB setup failed");
return false;
}
void USBHostSerial::end() {
// TODO (bertmelis): implement end, together with destruction.
}
std::size_t USBHostSerial::write(uint8_t data) {
if (xRingbufferSend(_tx_buf_handle, &data, 1, pdMS_TO_TICKS(1)) == pdTRUE) {
return 1;
}
return 0;
}
std::size_t USBHostSerial::write(const uint8_t *data, std::size_t len) {
UBaseType_t numItemsWaiting;
vRingbufferGetInfo(_tx_buf_handle, nullptr, nullptr, nullptr, nullptr, &numItemsWaiting);
std::size_t maxSize = USBHOSTSERIAL_BUFFERSIZE - numItemsWaiting;
if (maxSize < len) {
char buf[40];
snprintf(buf, 40, "USB buf overflow: %u-%u", len, maxSize);
_log(buf);
} else {
if (xRingbufferSend(_tx_buf_handle, data, len, pdMS_TO_TICKS(1)) == pdTRUE) {
return len;
}
}
return 0;
}
std::size_t USBHostSerial::available() {
UBaseType_t numItemsWaiting;
vRingbufferGetInfo(_rx_buf_handle, nullptr, nullptr, nullptr, nullptr, &numItemsWaiting);
return numItemsWaiting;
}
uint8_t USBHostSerial::read() {
std::size_t pxItemSize = 0;
uint8_t retVal = 0;
void* ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), 1);
if (pxItemSize > 0) {
retVal = *reinterpret_cast<uint8_t*>(ret);
vRingbufferReturnItem(_rx_buf_handle, ret);
}
return retVal;
}
std::size_t USBHostSerial::read(uint8_t *dest, std::size_t size) {
std::size_t retVal = 0;
std::size_t pxItemSize = 0;
while (size > pxItemSize) {
void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - pxItemSize);
if (ret) {
std::memcpy(dest + retVal, ret, pxItemSize);
retVal += pxItemSize;
vRingbufferReturnItem(_rx_buf_handle, ret);
} else {
break;
}
}
return retVal;
}
void USBHostSerial::setLogger(USBHostSerialLoggerFunc logger) {
_logger = logger;
}
void USBHostSerial::_setup() {
_device_disconnected_sem = xSemaphoreCreateBinary();
assert(_device_disconnected_sem);
xSemaphoreGive(_device_disconnected_sem); // make available for first use
// Install USB Host driver. Should only be called once in entire application
_host_config.skip_phy_setup = false;
_host_config.intr_flags = ESP_INTR_FLAG_LEVEL1;
ESP_ERROR_CHECK(usb_host_install(&_host_config));
// Create a task that will handle USB library events
BaseType_t task_created = xTaskCreate(_usb_lib_task, "usb_lib", 4096, this, 1, &_usb_lib_task_handle);
assert(task_created == pdTRUE);
ESP_ERROR_CHECK(cdc_acm_host_install(NULL));
// Register VCP drivers to VCP service
VCP::register_driver<FT23x>();
VCP::register_driver<CP210x>();
VCP::register_driver<CH34x>();
}
bool USBHostSerial::_handle_rx(const uint8_t *data, size_t data_len, void *arg) {
std::size_t lenReceived = 0;
while (lenReceived < data_len && xRingbufferSend(static_cast<USBHostSerial*>(arg)->_rx_buf_handle, &data[lenReceived], 1, pdMS_TO_TICKS(10)) == pdTRUE) {
++lenReceived;
}
if (lenReceived < data_len) {
// log overflow warning
static_cast<USBHostSerial*>(arg)->_log("USB rx buf overflow");
}
return true;
}
void USBHostSerial::_handle_event(const cdc_acm_host_dev_event_data_t *event, void *user_ctx) {
if (event->type == CDC_ACM_HOST_DEVICE_DISCONNECTED) {
xSemaphoreGive(static_cast<USBHostSerial*>(user_ctx)->_device_disconnected_sem);
}
}
void USBHostSerial::_usb_lib_task(void *arg) {
while (1) {
uint32_t event_flags;
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
ESP_ERROR_CHECK(usb_host_device_free_all());
}
}
}
void USBHostSerial::_USBHostSerial_task(void *arg) {
USBHostSerial* thisInstance = static_cast<USBHostSerial*>(arg);
esp_err_t err = ESP_OK; // reusable
while (1) {
// try to open USB VCP device
const cdc_acm_host_device_config_t dev_config = {
.connection_timeout_ms = 10,
.out_buffer_size = USBHOSTSERIAL_BUFFERSIZE,
.in_buffer_size = USBHOSTSERIAL_BUFFERSIZE,
.event_cb = _handle_event,
.data_cb = _handle_rx,
.user_arg = thisInstance,
};
cdc_acm_dev_hdl_t cdc_dev = NULL;
auto vcp = std::unique_ptr<CdcAcmDevice>(VCP::open(&dev_config));
if (vcp == nullptr) {
// try to fallback to CDC
err = cdc_acm_host_open(thisInstance->_vid, thisInstance->_pid, 0, &dev_config, &cdc_dev);
if (err != ESP_OK) {
continue;
}
thisInstance->_fallback = true;
thisInstance->_log("USB CDC device opened");
} else {
thisInstance->_fallback = false;
thisInstance->_log("USB VCP device opened");
}
// mark connected
xSemaphoreTake(thisInstance->_device_disconnected_sem, portMAX_DELAY);
// set line coding
err = ESP_OK;
if (thisInstance->_fallback) {
err = cdc_acm_host_line_coding_get(cdc_dev, &(thisInstance->_line_coding));
} else {
err = vcp->line_coding_set(&(thisInstance->_line_coding));
}
if (err == ESP_OK) {
thisInstance->_log("USB line coding set");
} else {
thisInstance->_log("USB line coding error");
continue;
}
// all set, enter loop to start sending
while (1) {
// check if still connected
if (xSemaphoreTake(thisInstance->_device_disconnected_sem, 0) == pdTRUE) {
break;
}
// check for data to send
std::size_t pxItemSize = 0;
void *data = xRingbufferReceiveUpTo(thisInstance->_tx_buf_handle, &pxItemSize, pdMS_TO_TICKS(10), USBHOSTSERIAL_BUFFERSIZE);
if (data) {
if (thisInstance->_fallback) {
err = cdc_acm_host_data_tx_blocking(cdc_dev, (uint8_t*)data, pxItemSize, 1000);
} else {
err = vcp->tx_blocking((uint8_t*)data, pxItemSize, 1000);
}
if (err == ESP_OK) {
vRingbufferReturnItem(thisInstance->_tx_buf_handle, data);
} else {
thisInstance->_log("Error writing to USB");
}
}
taskYIELD();
}
}
}
void USBHostSerial::_log(const char* msg) {
if (_logger) {
_logger(msg);
}
}