|
| 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 | +// pio-usb is required for rp2040 host |
| 13 | +#include "pio_usb.h" |
| 14 | +#define HOST_PIN_DP 2 // Pin used as D+ for host, D- = D+ + 1 |
| 15 | + |
| 16 | +#include "Adafruit_TinyUSB.h" |
| 17 | + |
| 18 | +#define LANGUAGE_ID 0x0409 // English |
| 19 | + |
| 20 | +// USB Host object |
| 21 | +Adafruit_USBH_Host USBHost; |
| 22 | + |
| 23 | +// holding device descriptor |
| 24 | +tusb_desc_device_t desc_device; |
| 25 | + |
| 26 | +// the setup function runs once when you press reset or power the board |
| 27 | +void setup() |
| 28 | +{ |
| 29 | + Serial1.begin(115200); |
| 30 | + |
| 31 | + Serial.begin(115200); |
| 32 | + //while ( !Serial ) delay(10); // wait for native usb |
| 33 | + |
| 34 | + Serial.println("TinyUSB Dual Device Info Example"); |
| 35 | +} |
| 36 | + |
| 37 | +void loop() |
| 38 | +{ |
| 39 | +} |
| 40 | + |
| 41 | +// core1's setup |
| 42 | +void setup1() { |
| 43 | + //while ( !Serial ) delay(10); // wait for native usb |
| 44 | + Serial.println("Core1 setup to run TinyUSB host with pio-usb"); |
| 45 | + |
| 46 | + // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB |
| 47 | + uint32_t cpu_hz = clock_get_hz(clk_sys); |
| 48 | + if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) { |
| 49 | + while ( !Serial ) delay(10); // wait for native usb |
| 50 | + Serial.printf("Error: CPU Clock = %u, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz); |
| 51 | + Serial.printf("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n", cpu_hz); |
| 52 | + while(1) delay(1); |
| 53 | + } |
| 54 | + |
| 55 | + pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG; |
| 56 | + pio_cfg.pin_dp = HOST_PIN_DP; |
| 57 | + USBHost.configure_pio_usb(1, &pio_cfg); |
| 58 | + |
| 59 | + // run host stack on controller (rhport) 1 |
| 60 | + // Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the |
| 61 | + // host bit-banging processing works done in core1 to free up core0 for other works |
| 62 | + USBHost.begin(1); |
| 63 | +} |
| 64 | + |
| 65 | +// core1's loop |
| 66 | +void loop1() |
| 67 | +{ |
| 68 | + USBHost.task(); |
| 69 | +} |
| 70 | + |
| 71 | +//--------------------------------------------------------------------+ |
| 72 | +// TinyUSB Host callbacks |
| 73 | +//--------------------------------------------------------------------+ |
| 74 | + |
| 75 | +// Invoked when device is mounted (configured) |
| 76 | +void tuh_mount_cb (uint8_t daddr) |
| 77 | +{ |
| 78 | + Serial.printf("Device attached, address = %d\r\n", daddr); |
| 79 | + |
| 80 | + // Get Device Descriptor |
| 81 | + tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0); |
| 82 | +} |
| 83 | + |
| 84 | +/// Invoked when device is unmounted (bus reset/unplugged) |
| 85 | +void tuh_umount_cb(uint8_t daddr) |
| 86 | +{ |
| 87 | + Serial.printf("Device removed, address = %d\r\n", daddr); |
| 88 | +} |
| 89 | + |
| 90 | +void print_device_descriptor(tuh_xfer_t* xfer) |
| 91 | +{ |
| 92 | + if ( XFER_RESULT_SUCCESS != xfer->result ) |
| 93 | + { |
| 94 | + Serial.printf("Failed to get device descriptor\r\n"); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + uint8_t const daddr = xfer->daddr; |
| 99 | + |
| 100 | + Serial.printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct); |
| 101 | + Serial.printf("Device Descriptor:\r\n"); |
| 102 | + Serial.printf(" bLength %u\r\n" , desc_device.bLength); |
| 103 | + Serial.printf(" bDescriptorType %u\r\n" , desc_device.bDescriptorType); |
| 104 | + Serial.printf(" bcdUSB %04x\r\n" , desc_device.bcdUSB); |
| 105 | + Serial.printf(" bDeviceClass %u\r\n" , desc_device.bDeviceClass); |
| 106 | + Serial.printf(" bDeviceSubClass %u\r\n" , desc_device.bDeviceSubClass); |
| 107 | + Serial.printf(" bDeviceProtocol %u\r\n" , desc_device.bDeviceProtocol); |
| 108 | + Serial.printf(" bMaxPacketSize0 %u\r\n" , desc_device.bMaxPacketSize0); |
| 109 | + Serial.printf(" idVendor 0x%04x\r\n" , desc_device.idVendor); |
| 110 | + Serial.printf(" idProduct 0x%04x\r\n" , desc_device.idProduct); |
| 111 | + Serial.printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice); |
| 112 | + |
| 113 | + // Get String descriptor using Sync API |
| 114 | + uint16_t temp_buf[128]; |
| 115 | + |
| 116 | + Serial.printf(" iManufacturer %u " , desc_device.iManufacturer); |
| 117 | + if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)) ) |
| 118 | + { |
| 119 | + print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf)); |
| 120 | + } |
| 121 | + Serial.printf("\r\n"); |
| 122 | + |
| 123 | + Serial.printf(" iProduct %u " , desc_device.iProduct); |
| 124 | + if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf))) |
| 125 | + { |
| 126 | + print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf)); |
| 127 | + } |
| 128 | + Serial.printf("\r\n"); |
| 129 | + |
| 130 | + Serial.printf(" iSerialNumber %u " , desc_device.iSerialNumber); |
| 131 | + if (XFER_RESULT_SUCCESS == tuh_descriptor_get_serial_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf))) |
| 132 | + { |
| 133 | + print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf)); |
| 134 | + } |
| 135 | + Serial.printf("\r\n"); |
| 136 | + |
| 137 | + Serial.printf(" bNumConfigurations %u\r\n" , desc_device.bNumConfigurations); |
| 138 | +} |
| 139 | + |
| 140 | +//--------------------------------------------------------------------+ |
| 141 | +// String Descriptor Helper |
| 142 | +//--------------------------------------------------------------------+ |
| 143 | + |
| 144 | +static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { |
| 145 | + // TODO: Check for runover. |
| 146 | + (void)utf8_len; |
| 147 | + // Get the UTF-16 length out of the data itself. |
| 148 | + |
| 149 | + for (size_t i = 0; i < utf16_len; i++) { |
| 150 | + uint16_t chr = utf16[i]; |
| 151 | + if (chr < 0x80) { |
| 152 | + *utf8++ = chr & 0xff; |
| 153 | + } else if (chr < 0x800) { |
| 154 | + *utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F)); |
| 155 | + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
| 156 | + } else { |
| 157 | + // TODO: Verify surrogate. |
| 158 | + *utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F)); |
| 159 | + *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); |
| 160 | + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
| 161 | + } |
| 162 | + // TODO: Handle UTF-16 code points that take two entries. |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// Count how many bytes a utf-16-le encoded string will take in utf-8. |
| 167 | +static int _count_utf8_bytes(const uint16_t *buf, size_t len) { |
| 168 | + size_t total_bytes = 0; |
| 169 | + for (size_t i = 0; i < len; i++) { |
| 170 | + uint16_t chr = buf[i]; |
| 171 | + if (chr < 0x80) { |
| 172 | + total_bytes += 1; |
| 173 | + } else if (chr < 0x800) { |
| 174 | + total_bytes += 2; |
| 175 | + } else { |
| 176 | + total_bytes += 3; |
| 177 | + } |
| 178 | + // TODO: Handle UTF-16 code points that take two entries. |
| 179 | + } |
| 180 | + return total_bytes; |
| 181 | +} |
| 182 | + |
| 183 | +static void print_utf16(uint16_t *temp_buf, size_t buf_len) { |
| 184 | + size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t); |
| 185 | + size_t utf8_len = _count_utf8_bytes(temp_buf + 1, utf16_len); |
| 186 | + |
| 187 | + _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len); |
| 188 | + ((uint8_t*) temp_buf)[utf8_len] = '\0'; |
| 189 | + |
| 190 | + Serial.printf((char*)temp_buf); |
| 191 | +} |
| 192 | + |
0 commit comments