|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <tusb.h> |
| 8 | +#include <bsp/board_api.h> |
| 9 | + |
| 10 | +// set some example Vendor and Product ID |
| 11 | +// the board will use to identify at the host |
| 12 | +#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) |
| 13 | +#define CDC_EXAMPLE_VID 0xCafe |
| 14 | +// use _PID_MAP to generate unique PID for each interface |
| 15 | +#define CDC_EXAMPLE_PID (0x4000 | _PID_MAP(CDC, 0)) |
| 16 | +// set USB 2.0 |
| 17 | +#define CDC_EXAMPLE_BCD 0x0200 |
| 18 | + |
| 19 | +// defines a descriptor that will be communicated to the host |
| 20 | +tusb_desc_device_t const desc_device = { |
| 21 | + .bLength = sizeof(tusb_desc_device_t), |
| 22 | + .bDescriptorType = TUSB_DESC_DEVICE, |
| 23 | + .bcdUSB = CDC_EXAMPLE_BCD, |
| 24 | + |
| 25 | + .bDeviceClass = TUSB_CLASS_MISC, // CDC is a subclass of misc |
| 26 | + .bDeviceSubClass = MISC_SUBCLASS_COMMON, // CDC uses common subclass |
| 27 | + .bDeviceProtocol = MISC_PROTOCOL_IAD, // CDC uses IAD |
| 28 | + |
| 29 | + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, // 64 bytes |
| 30 | + |
| 31 | + .idVendor = CDC_EXAMPLE_VID, |
| 32 | + .idProduct = CDC_EXAMPLE_PID, |
| 33 | + .bcdDevice = 0x0100, // Device release number |
| 34 | + |
| 35 | + .iManufacturer = 0x01, // Index of manufacturer string |
| 36 | + .iProduct = 0x02, // Index of product string |
| 37 | + .iSerialNumber = 0x03, // Index of serial number string |
| 38 | + |
| 39 | + .bNumConfigurations = 0x01 // 1 configuration |
| 40 | +}; |
| 41 | + |
| 42 | +// called when host requests to get device descriptor |
| 43 | +uint8_t const *tud_descriptor_device_cb(void); |
| 44 | + |
| 45 | +enum { |
| 46 | + ITF_NUM_CDC_0 = 0, |
| 47 | + ITF_NUM_CDC_0_DATA, |
| 48 | + ITF_NUM_CDC_1, |
| 49 | + ITF_NUM_CDC_1_DATA, |
| 50 | + ITF_NUM_TOTAL |
| 51 | +}; |
| 52 | + |
| 53 | +// total length of configuration descriptor |
| 54 | +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN) |
| 55 | + |
| 56 | +// define endpoint numbers |
| 57 | +#define EPNUM_CDC_0_NOTIF 0x81 // notification endpoint for CDC 0 |
| 58 | +#define EPNUM_CDC_0_OUT 0x02 // out endpoint for CDC 0 |
| 59 | +#define EPNUM_CDC_0_IN 0x82 // in endpoint for CDC 0 |
| 60 | + |
| 61 | +#define EPNUM_CDC_1_NOTIF 0x84 // notification endpoint for CDC 1 |
| 62 | +#define EPNUM_CDC_1_OUT 0x05 // out endpoint for CDC 1 |
| 63 | +#define EPNUM_CDC_1_IN 0x85 // in endpoint for CDC 1 |
| 64 | + |
| 65 | +// configure descriptor (for 2 CDC interfaces) |
| 66 | +uint8_t const desc_configuration[] = { |
| 67 | + // config descriptor | how much power in mA, count of interfaces, ... |
| 68 | + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x80, 100), |
| 69 | + |
| 70 | + // CDC 0: Communication Interface - TODO: get 64 from tusb_config.h |
| 71 | + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, 4, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64), |
| 72 | + // CDC 0: Data Interface |
| 73 | + //TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0_DATA, 4, 0x01, 0x02), |
| 74 | + |
| 75 | + // CDC 1: Communication Interface - TODO: get 64 from tusb_config.h |
| 76 | + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, 4, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64), |
| 77 | + // CDC 1: Data Interface |
| 78 | + //TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1_DATA, 4, 0x03, 0x04), |
| 79 | +}; |
| 80 | + |
| 81 | +// called when host requests to get configuration descriptor |
| 82 | +uint8_t const * tud_descriptor_configuration_cb(uint8_t index); |
| 83 | + |
| 84 | +// more device descriptor this time the qualifier |
| 85 | +tusb_desc_device_qualifier_t const desc_device_qualifier = { |
| 86 | + .bLength = sizeof(tusb_desc_device_t), |
| 87 | + .bDescriptorType = TUSB_DESC_DEVICE, |
| 88 | + .bcdUSB = CDC_EXAMPLE_BCD, |
| 89 | + |
| 90 | + .bDeviceClass = TUSB_CLASS_CDC, |
| 91 | + .bDeviceSubClass = MISC_SUBCLASS_COMMON, |
| 92 | + .bDeviceProtocol = MISC_PROTOCOL_IAD, |
| 93 | + |
| 94 | + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, |
| 95 | + .bNumConfigurations = 0x01, |
| 96 | + .bReserved = 0x00 |
| 97 | +}; |
| 98 | + |
| 99 | +// called when host requests to get device qualifier descriptor |
| 100 | +uint8_t const* tud_descriptor_device_qualifier_cb(void); |
| 101 | + |
| 102 | +// String descriptors referenced with .i... in the descriptor tables |
| 103 | + |
| 104 | +enum { |
| 105 | + STRID_LANGID = 0, // 0: supported language ID |
| 106 | + STRID_MANUFACTURER, // 1: Manufacturer |
| 107 | + STRID_PRODUCT, // 2: Product |
| 108 | + STRID_SERIAL, // 3: Serials |
| 109 | + STRID_CDC_0, // 4: CDC Interface 0 |
| 110 | + STRID_CDC_1, // 5: CDC Interface 1 |
| 111 | +}; |
| 112 | + |
| 113 | +// array of pointer to string descriptors |
| 114 | +char const *string_desc_arr[] = { |
| 115 | + // switched because board is little endian |
| 116 | + (const char[]) { 0x09, 0x04 }, // 0: supported language is English (0x0409) |
| 117 | + "Raspberry Pi", // 1: Manufacturer |
| 118 | + "Pico (2)", // 2: Product |
| 119 | + NULL, // 3: Serials (null so it uses unique ID if available) |
| 120 | + "Pico SDK stdio" // 4: CDC Interface 0 |
| 121 | + "Custom CDC", // 5: CDC Interface 1, |
| 122 | + "RPiReset" // 6: Reset Interface |
| 123 | +}; |
| 124 | + |
| 125 | +// buffer to hold the string descriptor during the request | plus 1 for the null terminator |
| 126 | +static uint16_t _desc_str[32 + 1]; |
| 127 | + |
| 128 | +// called when host request to get string descriptor |
| 129 | +uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid); |
| 130 | + |
| 131 | +// --------------------------------------------------------------------+ |
| 132 | +// IMPLEMENTATION |
| 133 | +// --------------------------------------------------------------------+ |
| 134 | + |
| 135 | +uint8_t const *tud_descriptor_device_cb(void) |
| 136 | +{ |
| 137 | + return (uint8_t const *)&desc_device; |
| 138 | +} |
| 139 | + |
| 140 | +uint8_t const* tud_descriptor_device_qualifier_cb(void) |
| 141 | +{ |
| 142 | + return (uint8_t const *)&desc_device_qualifier; |
| 143 | +} |
| 144 | + |
| 145 | +uint8_t const * tud_descriptor_configuration_cb(uint8_t index) |
| 146 | +{ |
| 147 | + // avoid unused parameter warning and keep function signature consistent |
| 148 | + (void)index; |
| 149 | + |
| 150 | + return desc_configuration; |
| 151 | +} |
| 152 | + |
| 153 | +uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) |
| 154 | +{ |
| 155 | + // TODO: check lang id |
| 156 | + (void) langid; |
| 157 | + size_t char_count; |
| 158 | + |
| 159 | + // Determine which string descriptor to return |
| 160 | + switch (index) { |
| 161 | + case STRID_LANGID: |
| 162 | + memcpy(&_desc_str[1], string_desc_arr[STRID_LANGID], 2); |
| 163 | + char_count = 1; |
| 164 | + break; |
| 165 | + |
| 166 | + case STRID_SERIAL: |
| 167 | + // try to read the serial from the board |
| 168 | + char_count = board_usb_get_serial(_desc_str + 1, 32); |
| 169 | + break; |
| 170 | + |
| 171 | + default: |
| 172 | + // COPYRIGHT NOTE: Based on TinyUSB example |
| 173 | + // Windows wants utf16le |
| 174 | + |
| 175 | + // Determine which string descriptor to return |
| 176 | + if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) { |
| 177 | + return NULL; |
| 178 | + } |
| 179 | + |
| 180 | + // Copy string descriptor into _desc_str |
| 181 | + const char *str = string_desc_arr[index]; |
| 182 | + |
| 183 | + char_count = strlen(str); |
| 184 | + size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1; // -1 for string type |
| 185 | + // Cap at max char |
| 186 | + if (char_count > max_count) { |
| 187 | + char_count = max_count; |
| 188 | + } |
| 189 | + |
| 190 | + // Convert ASCII string into UTF-16 |
| 191 | + for (size_t i = 0; i < char_count; i++) { |
| 192 | + _desc_str[1 + i] = str[i]; |
| 193 | + } |
| 194 | + break; |
| 195 | + } |
| 196 | + |
| 197 | + // First byte is the length (including header), second byte is string type |
| 198 | + _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (char_count * 2 + 2)); |
| 199 | + |
| 200 | + return _desc_str; |
| 201 | +} |
0 commit comments