diff options
| author | hathach <[email protected]> | 2019-05-11 16:31:08 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2019-05-11 16:31:52 +0700 |
| commit | de56a0ca8987c15c06112ece2589dab18f1d6666 (patch) | |
| tree | 101bd3bf6cb994043e3347e2e473f252996fd215 /examples | |
| parent | bfa073818c83266c2353ccf4fce580eef62e4a0c (diff) | |
add tud_descriptor_string_cb() for getting string descriptor from application
- remove tud_desc_set.string_arr/string_count
Diffstat (limited to 'examples')
4 files changed, 169 insertions, 95 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..6ddc11a82 100644 --- a/examples/device/cdc_msc_hid/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid/src/usb_descriptors.c @@ -136,31 +136,6 @@ uint8_t const desc_configuration[] = #endif }; -//------------- String Descriptors -------------// -// array of pointer to string descriptors -uint16_t const * 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'), - - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), - - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), - - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), - - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), - - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; // tud_desc_set is required by tinyusb stack tud_desc_set_t tud_desc_set = @@ -168,10 +143,49 @@ tud_desc_set_t tud_desc_set = .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]), - #if CFG_TUD_HID .hid_report = desc_hid_report, #endif }; + +//------------- String Descriptors -------------// + +// 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 + "TinyUSB CDC", // 4: CDC Interface + "TinyUSB MSC", // 5: MSC Interface + "TinyUSB HID" // 6: HID +}; + +// Invoked when received GET_STRING_DESC request +// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64 +// Return number of characters. Note usb string is in 16-bits unicode format +uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char) +{ + if ( index == 0) + { + memcpy(desc, string_desc_arr[0], 2); + return 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0; + + const char* str = string_desc_arr[index]; + + // Cap at max char + uint8_t count = strlen(str); + if ( count > max_char ) count = max_char; + + for(uint8_t i=0; i<count; i++) + { + *desc++ = str[i]; + } + + return count; + } +} 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..0a2970a63 100644 --- a/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c +++ b/examples/device/cdc_msc_hid_freertos/src/usb_descriptors.c @@ -136,42 +136,55 @@ uint8_t const desc_configuration[] = #endif }; -//------------- String Descriptors -------------// -// array of pointer to string descriptors -uint16_t const * 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'), - - // 2: Product - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'), - - // 3: Serials, should use chip ID - TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'), - - // 4: CDC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'), - - // 5: MSC Interface - TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'), - - // 6: HID - TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d') -}; - // tud_desc_set is required by tinyusb stack tud_desc_set_t tud_desc_set = { .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]), - #if CFG_TUD_HID .hid_report = desc_hid_report, #endif }; + +//------------- String Descriptors -------------// + +// 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 + "TinyUSB CDC", // 4: CDC Interface + "TinyUSB MSC", // 5: MSC Interface + "TinyUSB HID" // 6: HID +}; + +// Invoked when received GET_STRING_DESC request +// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64 +// Return number of characters. Note usb string is in 16-bits unicode format +uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char) +{ + if ( index == 0) + { + memcpy(desc, string_desc_arr[0], 2); + return 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0; + + const char* str = string_desc_arr[index]; + + // Cap at max char + uint8_t count = strlen(str); + if ( count > max_char ) count = max_char; + + for(uint8_t i=0; i<count; i++) + { + *desc++ = str[i]; + } + + return count; + } +} diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c index 231eb4fa6..b25915ceb 100644 --- a/examples/device/hid_generic_inout/src/usb_descriptors.c +++ b/examples/device/hid_generic_inout/src/usb_descriptors.c @@ -88,30 +88,53 @@ 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 [] = + +// tud_desc_set is required by tinyusb stack +tud_desc_set_t tud_desc_set = { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + .device = &desc_device, + .config = desc_configuration, - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +#if CFG_TUD_HID + .hid_report = desc_hid_report, +#endif +}; - // 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 = +// Invoked when received GET_STRING_DESC request +// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64 +// Return number of characters. Note usb string is in 16-bits unicode format +uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char) { - .device = &desc_device, - .config = desc_configuration, + if ( index == 0) + { + memcpy(desc, string_desc_arr[0], 2); + return 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0; - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), - .hid_report = desc_hid_report, -}; + const char* str = string_desc_arr[index]; + + // Cap at max char + uint8_t count = strlen(str); + if ( count > max_char ) count = max_char; + + for(uint8_t i=0; i<count; i++) + { + *desc++ = str[i]; + } + + return count; + } +} diff --git a/examples/device/msc_dual_lun/src/usb_descriptors.c b/examples/device/msc_dual_lun/src/usb_descriptors.c index af2b1a091..43a785024 100644 --- a/examples/device/msc_dual_lun/src/usb_descriptors.c +++ b/examples/device/msc_dual_lun/src/usb_descriptors.c @@ -82,29 +82,53 @@ 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 [] = + +// tud_desc_set is required by tinyusb stack +tud_desc_set_t tud_desc_set = { - // 0: is supported language = English - TUD_DESC_STRCONV(0x0409), + .device = &desc_device, + .config = desc_configuration, - // 1: Manufacturer - TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'), +#if CFG_TUD_HID + .hid_report = desc_hid_report, +#endif +}; - // 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 = +// Invoked when received GET_STRING_DESC request +// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64 +// Return number of characters. Note usb string is in 16-bits unicode format +uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char) { - .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, -}; + if ( index == 0) + { + memcpy(desc, string_desc_arr[0], 2); + return 1; + }else + { + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0; + + const char* str = string_desc_arr[index]; + + // Cap at max char + uint8_t count = strlen(str); + if ( count > max_char ) count = max_char; + + for(uint8_t i=0; i<count; i++) + { + *desc++ = str[i]; + } + + return count; + } +} |
