Skip to content

Commit 015735a

Browse files
committed
fine tune esp32 port
1 parent a6da6f2 commit 015735a

File tree

3 files changed

+94
-83
lines changed

3 files changed

+94
-83
lines changed

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 94 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,88 @@ Adafruit_USBD_Device TinyUSBDevice;
8686

8787
Adafruit_USBD_Device::Adafruit_USBD_Device(void) {}
8888

89+
void Adafruit_USBD_Device::setConfigurationBuffer(uint8_t *buf,
90+
uint32_t buflen) {
91+
if (buflen < _desc_cfg_maxlen) {
92+
return;
93+
}
94+
95+
memcpy(buf, _desc_cfg, _desc_cfg_len);
96+
_desc_cfg = buf;
97+
_desc_cfg_maxlen = buflen;
98+
}
99+
100+
void Adafruit_USBD_Device::setID(uint16_t vid, uint16_t pid) {
101+
_desc_device.idVendor = vid;
102+
_desc_device.idProduct = pid;
103+
}
104+
105+
void Adafruit_USBD_Device::setVersion(uint16_t bcd) {
106+
_desc_device.bcdUSB = bcd;
107+
}
108+
109+
void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd) {
110+
_desc_device.bcdDevice = bcd;
111+
}
112+
113+
void Adafruit_USBD_Device::setLanguageDescriptor(uint16_t language_id) {
114+
_desc_str_arr[STRID_LANGUAGE] = (const char *)((uint32_t)language_id);
115+
}
116+
117+
void Adafruit_USBD_Device::setManufacturerDescriptor(const char *s) {
118+
_desc_str_arr[STRID_MANUFACTURER] = s;
119+
}
120+
121+
void Adafruit_USBD_Device::setProductDescriptor(const char *s) {
122+
_desc_str_arr[STRID_PRODUCT] = s;
123+
}
124+
125+
void Adafruit_USBD_Device::task(void) { tud_task(); }
126+
127+
bool Adafruit_USBD_Device::mounted(void) { return tud_mounted(); }
128+
129+
bool Adafruit_USBD_Device::suspended(void) { return tud_suspended(); }
130+
131+
bool Adafruit_USBD_Device::ready(void) { return tud_ready(); }
132+
133+
bool Adafruit_USBD_Device::remoteWakeup(void) { return tud_remote_wakeup(); }
134+
135+
bool Adafruit_USBD_Device::detach(void) { return tud_disconnect(); }
136+
137+
bool Adafruit_USBD_Device::attach(void) { return tud_connect(); }
138+
139+
// EPS32 use built-in core descriptor builder.
140+
// Therefore most of descriptors are stubs only
141+
#ifdef ARDUINO_ARCH_ESP32
142+
143+
void Adafruit_USBD_Device::clearConfiguration(void) {
144+
145+
}
146+
147+
bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
148+
(void)itf;
149+
return true;
150+
}
151+
152+
bool Adafruit_USBD_Device::begin(uint8_t rhport) {
153+
(void) rhport;
154+
return true;
155+
}
156+
157+
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t *serial_utf16) {
158+
(void) serial_utf16;
159+
return 0;
160+
}
161+
162+
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
163+
uint16_t langid) {
164+
(void)index;
165+
(void)langid;
166+
return NULL;
167+
}
168+
169+
#else
170+
89171
void Adafruit_USBD_Device::clearConfiguration(void) {
90172
tusb_desc_device_t const desc_dev = {.bLength = sizeof(tusb_desc_device_t),
91173
.bDescriptorType = TUSB_DESC_DEVICE,
@@ -186,59 +268,6 @@ bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
186268
return true;
187269
}
188270

189-
void Adafruit_USBD_Device::setConfigurationBuffer(uint8_t *buf,
190-
uint32_t buflen) {
191-
if (buflen < _desc_cfg_maxlen) {
192-
return;
193-
}
194-
195-
memcpy(buf, _desc_cfg, _desc_cfg_len);
196-
_desc_cfg = buf;
197-
_desc_cfg_maxlen = buflen;
198-
}
199-
200-
void Adafruit_USBD_Device::setID(uint16_t vid, uint16_t pid) {
201-
_desc_device.idVendor = vid;
202-
_desc_device.idProduct = pid;
203-
}
204-
205-
void Adafruit_USBD_Device::setVersion(uint16_t bcd) {
206-
_desc_device.bcdUSB = bcd;
207-
}
208-
209-
void Adafruit_USBD_Device::setDeviceVersion(uint16_t bcd) {
210-
_desc_device.bcdDevice = bcd;
211-
}
212-
213-
void Adafruit_USBD_Device::setLanguageDescriptor(uint16_t language_id) {
214-
_desc_str_arr[STRID_LANGUAGE] = (const char *)((uint32_t)language_id);
215-
}
216-
217-
void Adafruit_USBD_Device::setManufacturerDescriptor(const char *s) {
218-
_desc_str_arr[STRID_MANUFACTURER] = s;
219-
}
220-
221-
void Adafruit_USBD_Device::setProductDescriptor(const char *s) {
222-
_desc_str_arr[STRID_PRODUCT] = s;
223-
}
224-
225-
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t *serial_utf16) {
226-
uint8_t serial_id[16] __attribute__((aligned(4)));
227-
uint8_t const serial_len = TinyUSB_Port_GetSerialNumber(serial_id);
228-
229-
for (uint8_t i = 0; i < serial_len; i++) {
230-
for (uint8_t j = 0; j < 2; j++) {
231-
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
232-
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
233-
234-
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
235-
serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
236-
}
237-
}
238-
239-
return 2 * serial_len;
240-
}
241-
242271
bool Adafruit_USBD_Device::begin(uint8_t rhport) {
243272
clearConfiguration();
244273

@@ -250,43 +279,31 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
250279
_desc_device.bDeviceSubClass = MISC_SUBCLASS_COMMON;
251280
_desc_device.bDeviceProtocol = MISC_PROTOCOL_IAD;
252281

253-
#ifndef ARDUINO_ARCH_ESP32
254282
SerialTinyUSB.begin(115200);
255283

256284
// Init device hardware and call tusb_init()
257285
TinyUSB_Port_InitDevice(rhport);
258-
#endif
259286

260287
return true;
261288
}
262289

263-
void Adafruit_USBD_Device::task(void) { tud_task(); }
264-
265-
bool Adafruit_USBD_Device::mounted(void) { return tud_mounted(); }
266-
267-
bool Adafruit_USBD_Device::suspended(void) { return tud_suspended(); }
268-
269-
bool Adafruit_USBD_Device::ready(void) { return tud_ready(); }
270-
271-
bool Adafruit_USBD_Device::remoteWakeup(void) { return tud_remote_wakeup(); }
272-
273-
bool Adafruit_USBD_Device::detach(void) { return tud_disconnect(); }
274-
275-
bool Adafruit_USBD_Device::attach(void) { return tud_connect(); }
290+
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t *serial_utf16) {
291+
uint8_t serial_id[16] __attribute__((aligned(4)));
292+
uint8_t const serial_len = TinyUSB_Port_GetSerialNumber(serial_id);
276293

277-
#ifdef ARDUINO_ARCH_ESP32
294+
for (uint8_t i = 0; i < serial_len; i++) {
295+
for (uint8_t j = 0; j < 2; j++) {
296+
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
297+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
278298

279-
// EPS32 use built-in core descriptor builder
280-
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
281-
uint16_t langid) {
282-
(void)index;
283-
(void)langid;
299+
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
300+
serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
301+
}
302+
}
284303

285-
return NULL;
304+
return 2 * serial_len;
286305
}
287306

288-
#else
289-
290307
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
291308
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
292309
uint16_t langid) {

src/arduino/hid/Adafruit_USBD_HID.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,9 @@ uint16_t Adafruit_USBD_HID::getInterfaceDescriptor(uint8_t itfnum, uint8_t *buf,
162162
}
163163

164164
bool Adafruit_USBD_HID::begin(void) {
165-
166-
#ifndef ARDUINO_ARCH_ESP32
167165
if (!TinyUSBDevice.addInterface(*this)) {
168166
return false;
169167
}
170-
#endif
171168

172169
_hid_dev = this;
173170
return true;

src/arduino/msc/Adafruit_USBD_MSC.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,9 @@ void Adafruit_USBD_MSC::setReadyCallback(uint8_t lun, ready_callback_t cb) {
111111
}
112112

113113
bool Adafruit_USBD_MSC::begin(void) {
114-
115-
#ifndef ARDUINO_ARCH_ESP32
116114
if (!TinyUSBDevice.addInterface(*this)) {
117115
return false;
118116
}
119-
#endif
120117

121118
_msc_dev = this;
122119
return true;

0 commit comments

Comments
 (0)