Skip to content

Commit 138a336

Browse files
authored
Merge pull request #319 from adafruit/add-multiple-deviceinfo
Add multiple deviceinfo
2 parents ffaa85e + 2b4f48d commit 138a336

File tree

2 files changed

+74
-37
lines changed

2 files changed

+74
-37
lines changed

examples/DualRole/device_info_rp2040/device_info_rp2040.ino renamed to examples/DualRole/Simple/device_info/device_info.ino

Lines changed: 74 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,23 @@
6565
// USB Host object
6666
Adafruit_USBH_Host USBHost;
6767

68-
// holding device descriptor
69-
tusb_desc_device_t desc_device;
68+
typedef struct {
69+
tusb_desc_device_t desc_device;
70+
uint16_t manufacturer[32];
71+
uint16_t product[32];
72+
uint16_t serial[16];
73+
bool mounted;
74+
} dev_info_t;
75+
76+
// CFG_TUH_DEVICE_MAX is defined by tusb_config header
77+
dev_info_t dev_info[CFG_TUH_DEVICE_MAX] = { 0 };
7078

7179
//--------------------------------------------------------------------+
7280
// Setup and Loop on Core0
7381
//--------------------------------------------------------------------+
7482

7583
// the setup function runs once when you press reset or power the board
76-
void setup()
77-
{
84+
void setup() {
7885
Serial1.begin(115200);
7986

8087
Serial.begin(115200);
@@ -83,8 +90,7 @@ void setup()
8390
Serial.println("TinyUSB Dual Device Info Example");
8491
}
8592

86-
void loop()
87-
{
93+
void loop() {
8894
}
8995

9096
//--------------------------------------------------------------------+
@@ -144,20 +150,50 @@ void loop1()
144150
//--------------------------------------------------------------------+
145151
// TinyUSB Host callbacks
146152
//--------------------------------------------------------------------+
153+
void print_device_descriptor(tuh_xfer_t* xfer);
154+
void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len);
155+
156+
void print_lsusb(void) {
157+
bool no_device = true;
158+
for ( uint8_t daddr = 1; daddr < CFG_TUH_DEVICE_MAX+1; daddr++ ) {
159+
// TODO can use tuh_mounted(daddr), but tinyusb has an bug
160+
// use local connected flag instead
161+
dev_info_t* dev = &dev_info[daddr-1];
162+
if ( dev->mounted ) {
163+
Serial.printf("Device %u: ID %04x:%04x %s %s\r\n", daddr,
164+
dev->desc_device.idVendor, dev->desc_device.idProduct,
165+
(char*) dev->manufacturer, (char*) dev->product);
166+
167+
no_device = false;
168+
}
169+
}
170+
171+
if (no_device) {
172+
Serial.println("No device connected (except hub)");
173+
}
174+
}
147175

148176
// Invoked when device is mounted (configured)
149177
void tuh_mount_cb (uint8_t daddr)
150178
{
151179
Serial.printf("Device attached, address = %d\r\n", daddr);
152180

181+
dev_info_t* dev = &dev_info[daddr-1];
182+
dev->mounted = true;
183+
153184
// Get Device Descriptor
154-
tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0);
185+
tuh_descriptor_get_device(daddr, &dev->desc_device, 18, print_device_descriptor, 0);
155186
}
156187

157188
/// Invoked when device is unmounted (bus reset/unplugged)
158189
void tuh_umount_cb(uint8_t daddr)
159190
{
160191
Serial.printf("Device removed, address = %d\r\n", daddr);
192+
dev_info_t* dev = &dev_info[daddr-1];
193+
dev->mounted = false;
194+
195+
// print device summary
196+
print_lsusb();
161197
}
162198

163199
void print_device_descriptor(tuh_xfer_t* xfer)
@@ -169,45 +205,48 @@ void print_device_descriptor(tuh_xfer_t* xfer)
169205
}
170206

171207
uint8_t const daddr = xfer->daddr;
208+
dev_info_t* dev = &dev_info[daddr-1];
209+
tusb_desc_device_t* desc = &dev->desc_device;
172210

173-
Serial.printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct);
211+
Serial.printf("Device %u: ID %04x:%04x\r\n", daddr, desc->idVendor, desc->idProduct);
174212
Serial.printf("Device Descriptor:\r\n");
175-
Serial.printf(" bLength %u\r\n" , desc_device.bLength);
176-
Serial.printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType);
177-
Serial.printf(" bcdUSB %04x\r\n" , desc_device.bcdUSB);
178-
Serial.printf(" bDeviceClass %u\r\n" , desc_device.bDeviceClass);
179-
Serial.printf(" bDeviceSubClass %u\r\n" , desc_device.bDeviceSubClass);
180-
Serial.printf(" bDeviceProtocol %u\r\n" , desc_device.bDeviceProtocol);
181-
Serial.printf(" bMaxPacketSize0 %u\r\n" , desc_device.bMaxPacketSize0);
182-
Serial.printf(" idVendor 0x%04x\r\n" , desc_device.idVendor);
183-
Serial.printf(" idProduct 0x%04x\r\n" , desc_device.idProduct);
184-
Serial.printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice);
213+
Serial.printf(" bLength %u\r\n" , desc->bLength);
214+
Serial.printf(" bDescriptorType %u\r\n" , desc->bDescriptorType);
215+
Serial.printf(" bcdUSB %04x\r\n" , desc->bcdUSB);
216+
Serial.printf(" bDeviceClass %u\r\n" , desc->bDeviceClass);
217+
Serial.printf(" bDeviceSubClass %u\r\n" , desc->bDeviceSubClass);
218+
Serial.printf(" bDeviceProtocol %u\r\n" , desc->bDeviceProtocol);
219+
Serial.printf(" bMaxPacketSize0 %u\r\n" , desc->bMaxPacketSize0);
220+
Serial.printf(" idVendor 0x%04x\r\n" , desc->idVendor);
221+
Serial.printf(" idProduct 0x%04x\r\n" , desc->idProduct);
222+
Serial.printf(" bcdDevice %04x\r\n" , desc->bcdDevice);
185223

186224
// Get String descriptor using Sync API
187-
uint16_t temp_buf[128];
188-
189-
Serial.printf(" iManufacturer %u " , desc_device.iManufacturer);
190-
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)) )
191-
{
192-
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
225+
Serial.printf(" iManufacturer %u ", desc->iManufacturer);
226+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, dev->manufacturer, sizeof(dev->manufacturer)) ) {
227+
utf16_to_utf8(dev->manufacturer, sizeof(dev->manufacturer));
228+
Serial.printf((char*) dev->manufacturer);
193229
}
194230
Serial.printf("\r\n");
195231

196-
Serial.printf(" iProduct %u " , desc_device.iProduct);
197-
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)))
198-
{
199-
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
232+
Serial.printf(" iProduct %u ", desc->iProduct);
233+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, dev->product, sizeof(dev->product))) {
234+
utf16_to_utf8(dev->product, sizeof(dev->product));
235+
Serial.printf((char*) dev->product);
200236
}
201237
Serial.printf("\r\n");
202238

203-
Serial.printf(" iSerialNumber %u " , desc_device.iSerialNumber);
204-
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)))
205-
{
206-
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
239+
Serial.printf(" iSerialNumber %u ", desc->iSerialNumber);
240+
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, dev->serial, sizeof(dev->serial))) {
241+
utf16_to_utf8(dev->serial, sizeof(dev->serial));
242+
Serial.printf((char*) dev->serial);
207243
}
208244
Serial.printf("\r\n");
209245

210-
Serial.printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations);
246+
Serial.printf(" bNumConfigurations %u\r\n", desc->bNumConfigurations);
247+
248+
// print device summary
249+
print_lsusb();
211250
}
212251

213252
//--------------------------------------------------------------------+
@@ -253,13 +292,11 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
253292
return total_bytes;
254293
}
255294

256-
static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
295+
void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) {
257296
size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
258297
size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len);
259298

260-
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
299+
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, buf_len);
261300
((uint8_t*) temp_buf)[utf8_len] = '\0';
262-
263-
Serial.printf((char*)temp_buf);
264301
}
265302

0 commit comments

Comments
 (0)