65
65
// USB Host object
66
66
Adafruit_USBH_Host USBHost;
67
67
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 };
70
78
71
79
// --------------------------------------------------------------------+
72
80
// Setup and Loop on Core0
73
81
// --------------------------------------------------------------------+
74
82
75
83
// the setup function runs once when you press reset or power the board
76
- void setup ()
77
- {
84
+ void setup () {
78
85
Serial1.begin (115200 );
79
86
80
87
Serial.begin (115200 );
@@ -83,8 +90,7 @@ void setup()
83
90
Serial.println (" TinyUSB Dual Device Info Example" );
84
91
}
85
92
86
- void loop ()
87
- {
93
+ void loop () {
88
94
}
89
95
90
96
// --------------------------------------------------------------------+
@@ -144,20 +150,50 @@ void loop1()
144
150
// --------------------------------------------------------------------+
145
151
// TinyUSB Host callbacks
146
152
// --------------------------------------------------------------------+
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
+ }
147
175
148
176
// Invoked when device is mounted (configured)
149
177
void tuh_mount_cb (uint8_t daddr)
150
178
{
151
179
Serial.printf (" Device attached, address = %d\r\n " , daddr);
152
180
181
+ dev_info_t * dev = &dev_info[daddr-1 ];
182
+ dev->mounted = true ;
183
+
153
184
// 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 );
155
186
}
156
187
157
188
// / Invoked when device is unmounted (bus reset/unplugged)
158
189
void tuh_umount_cb (uint8_t daddr)
159
190
{
160
191
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 ();
161
197
}
162
198
163
199
void print_device_descriptor (tuh_xfer_t * xfer)
@@ -169,45 +205,48 @@ void print_device_descriptor(tuh_xfer_t* xfer)
169
205
}
170
206
171
207
uint8_t const daddr = xfer->daddr ;
208
+ dev_info_t * dev = &dev_info[daddr-1 ];
209
+ tusb_desc_device_t * desc = &dev->desc_device ;
172
210
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 );
174
212
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 );
185
223
186
224
// 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 );
193
229
}
194
230
Serial.printf (" \r\n " );
195
231
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 );
200
236
}
201
237
Serial.printf (" \r\n " );
202
238
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 );
207
243
}
208
244
Serial.printf (" \r\n " );
209
245
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 ();
211
250
}
212
251
213
252
// --------------------------------------------------------------------+
@@ -253,13 +292,11 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
253
292
return total_bytes;
254
293
}
255
294
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) {
257
296
size_t utf16_len = ((temp_buf[0 ] & 0xff ) - 2 ) / sizeof (uint16_t );
258
297
size_t utf8_len = _count_utf8_bytes (temp_buf + 1 , utf16_len);
259
298
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);
261
300
((uint8_t *) temp_buf)[utf8_len] = ' \0 ' ;
262
-
263
- Serial.printf ((char *)temp_buf);
264
301
}
265
302
0 commit comments