|
| 1 | +/********************************************************************* |
| 2 | + Adafruit invests time and resources providing this open source code, |
| 3 | + please support Adafruit and open-source hardware by purchasing |
| 4 | + products from Adafruit! |
| 5 | +
|
| 6 | + MIT license, check LICENSE for more information |
| 7 | + Copyright (c) 2019 Ha Thach for Adafruit Industries |
| 8 | + All text above, and the splash screen below must be included in |
| 9 | + any redistribution |
| 10 | +*********************************************************************/ |
| 11 | + |
| 12 | +/* This example demonstrates use of native controller as host (TinyUSB Host) |
| 13 | + * Note: |
| 14 | + * - For most mcu with only 1 native usb, Serial is not available. We will use Serial1 instead |
| 15 | + * |
| 16 | + * Host example will get device descriptors of attached devices and print it out as follows: |
| 17 | + Device 1: ID 046d:c52f |
| 18 | + Device Descriptor: |
| 19 | + bLength 18 |
| 20 | + bDescriptorType 1 |
| 21 | + bcdUSB 0200 |
| 22 | + bDeviceClass 0 |
| 23 | + bDeviceSubClass 0 |
| 24 | + bDeviceProtocol 0 |
| 25 | + bMaxPacketSize0 8 |
| 26 | + idVendor 0x046d |
| 27 | + idProduct 0xc52f |
| 28 | + bcdDevice 2200 |
| 29 | + iManufacturer 1 Logitech |
| 30 | + iProduct 2 USB Receiver |
| 31 | + iSerialNumber 0 |
| 32 | + bNumConfigurations 1 |
| 33 | + * |
| 34 | + */ |
| 35 | + |
| 36 | +#include "Adafruit_TinyUSB.h" |
| 37 | + |
| 38 | +#ifndef USE_TINYUSB_HOST |
| 39 | + #error This example requires usb stack configured as host in "Tools -> USB Stack -> Adafruit TinyUSB Host" |
| 40 | +#endif |
| 41 | + |
| 42 | +// Language ID: English |
| 43 | +#define LANGUAGE_ID 0x0409 |
| 44 | + |
| 45 | +typedef struct { |
| 46 | + tusb_desc_device_t desc_device; |
| 47 | + uint16_t manufacturer[32]; |
| 48 | + uint16_t product[48]; |
| 49 | + uint16_t serial[16]; |
| 50 | + bool mounted; |
| 51 | +} dev_info_t; |
| 52 | + |
| 53 | +// CFG_TUH_DEVICE_MAX is defined by tusb_config header |
| 54 | +dev_info_t dev_info[CFG_TUH_DEVICE_MAX] = { 0 }; |
| 55 | + |
| 56 | +Adafruit_USBH_Host USBHost; |
| 57 | + |
| 58 | +void setup() { |
| 59 | + Serial1.begin(115200); |
| 60 | + Serial1.println("TinyUSB Host: Device Info Example"); |
| 61 | + |
| 62 | + // Init USB Host on native controller roothub port0 |
| 63 | + USBHost.begin(0); |
| 64 | +} |
| 65 | + |
| 66 | +//------------- Core0 -------------// |
| 67 | +void loop() { |
| 68 | + USBHost.task(); |
| 69 | + Serial1.flush(); |
| 70 | +} |
| 71 | + |
| 72 | +//--------------------------------------------------------------------+ |
| 73 | +// TinyUSB Host callbacks |
| 74 | +//--------------------------------------------------------------------+ |
| 75 | +void print_device_descriptor(tuh_xfer_t *xfer); |
| 76 | + |
| 77 | +void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len); |
| 78 | + |
| 79 | +void print_lsusb(void) { |
| 80 | + bool no_device = true; |
| 81 | + for (uint8_t daddr = 1; daddr < CFG_TUH_DEVICE_MAX + 1; daddr++) { |
| 82 | + // TODO can use tuh_mounted(daddr), but tinyusb has an bug |
| 83 | + // use local connected flag instead |
| 84 | + dev_info_t *dev = &dev_info[daddr - 1]; |
| 85 | + if (dev->mounted) { |
| 86 | + Serial1.printf("Device %u: ID %04x:%04x %s %s\r\n", daddr, |
| 87 | + dev->desc_device.idVendor, dev->desc_device.idProduct, |
| 88 | + (char *) dev->manufacturer, (char *) dev->product); |
| 89 | + |
| 90 | + no_device = false; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (no_device) { |
| 95 | + Serial1.println("No device connected (except hub)"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Invoked when device is mounted (configured) |
| 100 | +void tuh_mount_cb(uint8_t daddr) { |
| 101 | + Serial1.printf("Device attached, address = %d\r\n", daddr); |
| 102 | + |
| 103 | + dev_info_t *dev = &dev_info[daddr - 1]; |
| 104 | + dev->mounted = true; |
| 105 | + |
| 106 | + // Get Device Descriptor |
| 107 | + tuh_descriptor_get_device(daddr, &dev->desc_device, 18, print_device_descriptor, 0); |
| 108 | +} |
| 109 | + |
| 110 | +/// Invoked when device is unmounted (bus reset/unplugged) |
| 111 | +void tuh_umount_cb(uint8_t daddr) { |
| 112 | + Serial1.printf("Device removed, address = %d\r\n", daddr); |
| 113 | + dev_info_t *dev = &dev_info[daddr - 1]; |
| 114 | + dev->mounted = false; |
| 115 | + |
| 116 | + // print device summary |
| 117 | + print_lsusb(); |
| 118 | +} |
| 119 | + |
| 120 | +void print_device_descriptor(tuh_xfer_t *xfer) { |
| 121 | + if (XFER_RESULT_SUCCESS != xfer->result) { |
| 122 | + Serial1.printf("Failed to get device descriptor\r\n"); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + uint8_t const daddr = xfer->daddr; |
| 127 | + dev_info_t *dev = &dev_info[daddr - 1]; |
| 128 | + tusb_desc_device_t *desc = &dev->desc_device; |
| 129 | + |
| 130 | + Serial1.printf("Device %u: ID %04x:%04x\r\n", daddr, desc->idVendor, desc->idProduct); |
| 131 | + Serial1.printf("Device Descriptor:\r\n"); |
| 132 | + Serial1.printf(" bLength %u\r\n" , desc->bLength); |
| 133 | + Serial1.printf(" bDescriptorType %u\r\n" , desc->bDescriptorType); |
| 134 | + Serial1.printf(" bcdUSB %04x\r\n" , desc->bcdUSB); |
| 135 | + Serial1.printf(" bDeviceClass %u\r\n" , desc->bDeviceClass); |
| 136 | + Serial1.printf(" bDeviceSubClass %u\r\n" , desc->bDeviceSubClass); |
| 137 | + Serial1.printf(" bDeviceProtocol %u\r\n" , desc->bDeviceProtocol); |
| 138 | + Serial1.printf(" bMaxPacketSize0 %u\r\n" , desc->bMaxPacketSize0); |
| 139 | + Serial1.printf(" idVendor 0x%04x\r\n" , desc->idVendor); |
| 140 | + Serial1.printf(" idProduct 0x%04x\r\n" , desc->idProduct); |
| 141 | + Serial1.printf(" bcdDevice %04x\r\n" , desc->bcdDevice); |
| 142 | + |
| 143 | + // Get String descriptor using Sync API |
| 144 | + Serial1.printf(" iManufacturer %u ", desc->iManufacturer); |
| 145 | + if (XFER_RESULT_SUCCESS == |
| 146 | + tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, dev->manufacturer, sizeof(dev->manufacturer))) { |
| 147 | + utf16_to_utf8(dev->manufacturer, sizeof(dev->manufacturer)); |
| 148 | + Serial1.printf((char *) dev->manufacturer); |
| 149 | + } |
| 150 | + Serial1.printf("\r\n"); |
| 151 | + |
| 152 | + Serial1.printf(" iProduct %u ", desc->iProduct); |
| 153 | + if (XFER_RESULT_SUCCESS == |
| 154 | + tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, dev->product, sizeof(dev->product))) { |
| 155 | + utf16_to_utf8(dev->product, sizeof(dev->product)); |
| 156 | + Serial1.printf((char *) dev->product); |
| 157 | + } |
| 158 | + Serial1.printf("\r\n"); |
| 159 | + |
| 160 | + Serial1.printf(" iSerialNumber %u ", desc->iSerialNumber); |
| 161 | + if (XFER_RESULT_SUCCESS == |
| 162 | + tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, dev->serial, sizeof(dev->serial))) { |
| 163 | + utf16_to_utf8(dev->serial, sizeof(dev->serial)); |
| 164 | + Serial1.printf((char *) dev->serial); |
| 165 | + } |
| 166 | + Serial1.printf("\r\n"); |
| 167 | + |
| 168 | + Serial1.printf(" bNumConfigurations %u\r\n", desc->bNumConfigurations); |
| 169 | + |
| 170 | + // print device summary |
| 171 | + print_lsusb(); |
| 172 | +} |
| 173 | + |
| 174 | +//--------------------------------------------------------------------+ |
| 175 | +// String Descriptor Helper |
| 176 | +//--------------------------------------------------------------------+ |
| 177 | + |
| 178 | +static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { |
| 179 | + // TODO: Check for runover. |
| 180 | + (void) utf8_len; |
| 181 | + // Get the UTF-16 length out of the data itself. |
| 182 | + |
| 183 | + for (size_t i = 0; i < utf16_len; i++) { |
| 184 | + uint16_t chr = utf16[i]; |
| 185 | + if (chr < 0x80) { |
| 186 | + *utf8++ = chr & 0xff; |
| 187 | + } else if (chr < 0x800) { |
| 188 | + *utf8++ = (uint8_t) (0xC0 | (chr >> 6 & 0x1F)); |
| 189 | + *utf8++ = (uint8_t) (0x80 | (chr >> 0 & 0x3F)); |
| 190 | + } else { |
| 191 | + // TODO: Verify surrogate. |
| 192 | + *utf8++ = (uint8_t) (0xE0 | (chr >> 12 & 0x0F)); |
| 193 | + *utf8++ = (uint8_t) (0x80 | (chr >> 6 & 0x3F)); |
| 194 | + *utf8++ = (uint8_t) (0x80 | (chr >> 0 & 0x3F)); |
| 195 | + } |
| 196 | + // TODO: Handle UTF-16 code points that take two entries. |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +// Count how many bytes a utf-16-le encoded string will take in utf-8. |
| 201 | +static int _count_utf8_bytes(const uint16_t *buf, size_t len) { |
| 202 | + size_t total_bytes = 0; |
| 203 | + for (size_t i = 0; i < len; i++) { |
| 204 | + uint16_t chr = buf[i]; |
| 205 | + if (chr < 0x80) { |
| 206 | + total_bytes += 1; |
| 207 | + } else if (chr < 0x800) { |
| 208 | + total_bytes += 2; |
| 209 | + } else { |
| 210 | + total_bytes += 3; |
| 211 | + } |
| 212 | + // TODO: Handle UTF-16 code points that take two entries. |
| 213 | + } |
| 214 | + return total_bytes; |
| 215 | +} |
| 216 | + |
| 217 | +void utf16_to_utf8(uint16_t *temp_buf, size_t buf_len) { |
| 218 | + size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t); |
| 219 | + size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len); |
| 220 | + |
| 221 | + _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, buf_len); |
| 222 | + ((uint8_t *) temp_buf)[utf8_len] = '\0'; |
| 223 | +} |
0 commit comments