diff options
| author | hathach <[email protected]> | 2019-05-12 15:54:11 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-05-12 15:54:11 +0700 |
| commit | aaf5848ce15c5d6ccf4c51c3ef7b04fb787639ee (patch) | |
| tree | 2a04ff594ea42234c532fc4ab5f8e6f0b4603623 /examples | |
| parent | bfa073818c83266c2353ccf4fce580eef62e4a0c (diff) | |
| parent | 1174949308bb7595e99fd5789ebf3466cf2ab123 (diff) | |
Merge pull request #64 from hathach/develop
add callback for descriptors device/configuration/string/hid
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/device/cdc_msc_hid/src/usb_descriptors.c | 110 | ||||
| -rw-r--r-- | examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c | 110 | ||||
| -rw-r--r-- | examples/device/hid_generic_inout/src/usb_descriptors.c | 79 | ||||
| -rw-r--r-- | examples/device/msc_dual_lun/src/usb_descriptors.c | 71 |
4 files changed, 254 insertions, 116 deletions
diff --git a/examples/device/cdc_msc_hid/src/usb_descriptors.c b/examples/device/cdc_msc_hid/src/usb_descriptors.c index ef3cc9cef..73c8e7ce1 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -79,25 +79,34 @@ uint8_t const desc_hid_report[] = TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) }; + +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} + #endif //------------- Configuration Descriptor -------------// enum { - #if CFG_TUD_CDC - ITF_NUM_CDC = 0, - ITF_NUM_CDC_DATA, - #endif +#if CFG_TUD_CDC + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, +#endif - #if CFG_TUD_MSC - ITF_NUM_MSC, - #endif +#if CFG_TUD_MSC + ITF_NUM_MSC, +#endif - #if CFG_TUD_HID - ITF_NUM_HID, - #endif +#if CFG_TUD_HID + ITF_NUM_HID, +#endif - ITF_NUM_TOTAL + ITF_NUM_TOTAL }; enum @@ -136,42 +145,65 @@ uint8_t const desc_configuration[] = #endif }; +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} + //------------- String Descriptors -------------// + // array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = +char const* string_desc_arr [] = { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), - - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456", // 3: Serials, should use chip ID + "TinyUSB CDC", // 4: CDC Interface + "TinyUSB MSC", // 5: MSC Interface + "TinyUSB HID" // 6: HID +}; - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +static uint16_t _desc_str[32]; - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) +{ + uint8_t chr_count; - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), + const char* str = string_desc_arr[index]; - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = -{ - .device = &desc_device, - .config = desc_configuration, + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); -#if CFG_TUD_HID - .hid_report = desc_hid_report, -#endif -}; + return _desc_str; +} diff --git a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c index 1faaaed66..92ce8c6ec 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -79,25 +79,34 @@ uint8_t const desc_hid_report[] = TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ), TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), ) }; + +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} + #endif //------------- Configuration Descriptor -------------// enum { - #if CFG_TUD_CDC - ITF_NUM_CDC = 0, - ITF_NUM_CDC_DATA, - #endif +#if CFG_TUD_CDC + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, +#endif - #if CFG_TUD_MSC - ITF_NUM_MSC, - #endif +#if CFG_TUD_MSC + ITF_NUM_MSC, +#endif - #if CFG_TUD_HID - ITF_NUM_HID, - #endif +#if CFG_TUD_HID + ITF_NUM_HID, +#endif - ITF_NUM_TOTAL + ITF_NUM_TOTAL }; enum @@ -136,42 +145,65 @@ uint8_t const desc_configuration[] = #endif }; +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} + //------------- String Descriptors -------------// + // array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = +char const* string_desc_arr [] = { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), - - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456", // 3: Serials, should use chip ID + "TinyUSB CDC", // 4: CDC Interface + "TinyUSB MSC", // 5: MSC Interface + "TinyUSB HID" // 6: HID +}; - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +static uint16_t _desc_str[32]; - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) +{ + uint8_t chr_count; - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), + const char* str = string_desc_arr[index]; - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = -{ - .device = &desc_device, - .config = desc_configuration, + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); -#if CFG_TUD_HID - .hid_report = desc_hid_report, -#endif -}; + return _desc_str; +} diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 231eb4fa6..fb4dd378c 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -88,30 +88,71 @@ uint8_t const desc_configuration[] = TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, 16, 10) }; -//------------- String Descriptors -------------// -// array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = + +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + return desc_configuration; +} - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) +{ + return desc_hid_report; +} - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +//------------- String Descriptors -------------// - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), +// array of pointer to string descriptors +char const* string_desc_arr [] = +{ + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456", // 3: Serials, should use chip ID }; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = +static uint16_t _desc_str[32]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) { - .device = &desc_device, - .config = desc_configuration, + uint8_t chr_count; - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), - .hid_report = desc_hid_report, -}; + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; + + const char* str = string_desc_arr[index]; + + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; + + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } + + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); + + return _desc_str; +} diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index af2b1a091..1ae000fd9 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -82,29 +82,62 @@ uint8_t const desc_configuration[] = TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512 }; -//------------- String Descriptors -------------// -// array of pointer to string descriptors -uint16_t const * const string_desc_arr [] = +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + return (uint8_t const *) &desc_device; +} - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(void) +{ + return desc_configuration; +} - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), +//------------- String Descriptors -------------// - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6') +// array of pointer to string descriptors +char const* string_desc_arr [] = +{ + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456", // 3: Serials, should use chip ID }; -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = +static uint16_t _desc_str[32]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) { - .device = &desc_device, - .config = desc_configuration, - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), - .hid_report = NULL, -}; + uint8_t chr_count; + + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; + + const char* str = string_desc_arr[index]; + + // Cap at max char + chr_count = strlen(str); + if ( chr_count > 31 ) chr_count = 31; + + for(uint8_t i=0; i<chr_count; i++) + { + _desc_str[1+i] = str[i]; + } + } + + // first byte is len, second byte is string type + _desc_str[0] = TUD_DESC_STR_HEADER(chr_count); + + return _desc_str; +} |
