Guest User

pico-sdk-patch-cdc-custom

a guest
Jul 15th, 2022
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.01 KB | None | 0 0
  1. diff --git a/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h b/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h
  2. index 8ac8244..2975447 100644
  3. --- a/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h
  4. +++ b/src/rp2_common/pico_stdio_usb/include/pico/stdio_usb.h
  5. @@ -120,6 +120,40 @@ bool stdio_usb_init(void);
  6.   *  \return true if stdio is connected over CDC
  7.   */
  8.  bool stdio_usb_connected(void);
  9. +
  10. +#if !defined(LIB_TINYUSB_DEVICE)
  11. +/*! \brief Holds information for custom CDC descriptors used by \ref stdio_usb_set_custom_descriptors
  12. + *
  13. + *  This struct is passed to \ref stdio_usb_set_custom_descriptors to apply custom VendorID, ProductID,
  14. + *  Manufacturer String, Product String, and Serial Number to the built-in tinyUSB CDC.
  15. + *  
  16. + *  \note The struct does not need to remain valid after calling \ref stdio_usb_set_custom_descriptors
  17. + *  but the string data pointed to by the the member fields must remain valid until the host has received
  18. + *  all descriptors.
  19. + */
  20. +typedef struct {
  21. +    uint16_t idVendor;
  22. +    uint16_t idProduct;
  23. +    char* str_manufacturer;
  24. +    char* str_product;
  25. +    char* str_serial;
  26. +} stdio_custom_CDC_config_t;
  27. +/*! \brief Set custom CDC descriptors for the built-in tinyUSB CDC
  28. + *  \ingroup pico_stdio_usb
  29. + *
  30. + *  Set custom USB CDC descriptors (idVendor, idProduct, ...) for use with the default tinyUSB CDC. The parameters
  31. + *  are wrapped inside a \ref stdio_custom_CDC_config_t struct.
  32. + *  
  33. + *  \par These settings should not be changed arbitrarily! For commercial products, a USB Vendor ID must be acquired
  34. + *  from the [USB-IF](https://www.usb.org/) if you want to change it from the default (0x2E8A) to your own!
  35. + *
  36. + *  \note This function must be called prior to \ref pico_stdio_usb to have any effect. This function is not
  37. + *  available if tinyUSB is explicitly linked; in that case the device should be configured using the tinyUSB API.
  38. + *
  39. + *  \param *CDC_config
  40. + */
  41. +void stdio_usb_set_custom_descriptors(stdio_custom_CDC_config_t* CDC_config);
  42. +#endif
  43.  #ifdef __cplusplus
  44.  }
  45.  #endif
  46. diff --git a/src/rp2_common/pico_stdio_usb/stdio_usb.c b/src/rp2_common/pico_stdio_usb/stdio_usb.c
  47. index cd2e2c9..4165304 100644
  48. --- a/src/rp2_common/pico_stdio_usb/stdio_usb.c
  49. +++ b/src/rp2_common/pico_stdio_usb/stdio_usb.c
  50. @@ -168,16 +168,59 @@ bool stdio_usb_init(void) {
  51.  bool stdio_usb_connected(void) {
  52.      return tud_cdc_connected();
  53.  }
  54. +
  55. +#if !defined(LIB_TINYUSB_DEVICE) // only provide the custom override if the user did not explicitly pull in tinyUSB
  56. +/* forward declarations of the customizing functions inside our stdio_usb_descriptors.c */
  57. +tusb_desc_device_t *tud_descriptor_device_ptr();
  58. +void tud_descriptor_override_manufacturer(const char *new_value);
  59. +void tud_descriptor_override_product(const char *new_value);
  60. +void tud_descriptor_override_serial(const char *new_value);
  61. +void stdio_usb_set_custom_descriptors(stdio_custom_CDC_config_t* CDC_config) {
  62. +    tusb_desc_device_t* tusb_device = tud_descriptor_device_ptr();
  63. +    if (CDC_config == 0 || tusb_device == 0 || stdio_usb_connected()) {
  64. +        return;
  65. +    }
  66. +    if (CDC_config->str_manufacturer != 0) {
  67. +        tud_descriptor_override_manufacturer(CDC_config->str_manufacturer);
  68. +    }
  69. +    if (CDC_config->str_product != 0) {
  70. +        tud_descriptor_override_product(CDC_config->str_product);
  71. +    }
  72. +    if (CDC_config->str_serial != 0) {
  73. +        tud_descriptor_override_serial(CDC_config->str_serial);
  74. +    }
  75. +    if (CDC_config->idVendor != 0) {
  76. +        tusb_device->idVendor = CDC_config->idVendor;
  77. +    }
  78. +    if (CDC_config->idProduct != 0) {
  79. +        tusb_device->idProduct = CDC_config->idProduct;
  80. +    }
  81. +}
  82. +#else /* !defined(LIB_TINYUSB_DEVICE) */
  83. +void stdio_usb_set_custom_descriptors(stdio_custom_CDC_config_t* CDC_config)
  84. +{
  85. +    (void)CDC_config;
  86. +}
  87. +#endif /* !defined(LIB_TINYUSB_DEVICE) */
  88. +
  89.  #else
  90.  #warning stdio USB was configured along with user use of TinyUSB device mode, but CDC is not enabled
  91.  bool stdio_usb_init(void) {
  92.      return false;
  93.  }
  94. +void stdio_usb_set_custom_descriptors(stdio_custom_CDC_config_t* CDC_config)
  95. +{
  96. +    (void)CDC_config;
  97. +}
  98.  #endif // CFG_TUD_ENABLED && CFG_TUD_CDC
  99.  #else
  100.  #warning stdio USB was configured, but is being disabled as TinyUSB host is explicitly linked
  101.  bool stdio_usb_init(void) {
  102.      return false;
  103.  }
  104. +void stdio_usb_set_custom_descriptors(stdio_custom_CDC_config_t* CDC_config)
  105. +{
  106. +    (void)CDC_config;  
  107. +}
  108.  #endif // !LIB_TINYUSB_HOST
  109.  
  110. diff --git a/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c b/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
  111. index b3cfed0..cee1141 100644
  112. --- a/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
  113. +++ b/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
  114. @@ -66,7 +66,7 @@
  115.  
  116.  // Note: descriptors returned from callbacks must exist long enough for transfer to complete
  117.  
  118. -static const tusb_desc_device_t usbd_desc_device = {
  119. +static tusb_desc_device_t usbd_desc_device = {
  120.      .bLength = sizeof(tusb_desc_device_t),
  121.      .bDescriptorType = TUSB_DESC_DEVICE,
  122.      .bcdUSB = 0x0200,
  123. @@ -101,7 +101,7 @@ static const uint8_t usbd_desc_cfg[USBD_DESC_LEN] = {
  124.  
  125.  static char usbd_serial_str[PICO_UNIQUE_BOARD_ID_SIZE_BYTES * 2 + 1];
  126.  
  127. -static const char *const usbd_desc_str[] = {
  128. +static const char *usbd_desc_str[] = {
  129.      [USBD_STR_MANUF] = "Raspberry Pi",
  130.      [USBD_STR_PRODUCT] = "Pico",
  131.      [USBD_STR_SERIAL] = usbd_serial_str,
  132. @@ -148,4 +148,20 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, __unused uint16_t langid
  133.      return desc_str;
  134.  }
  135.  
  136. +tusb_desc_device_t *tud_descriptor_device_ptr() {
  137. +    return &usbd_desc_device;
  138. +}
  139. +
  140. +void tud_descriptor_override_manufacturer(const char *new_value) {
  141. +    usbd_desc_str[USBD_STR_MANUF] = new_value;
  142. +}
  143. +
  144. +void tud_descriptor_override_product(const char *new_value) {
  145. +    usbd_desc_str[USBD_STR_PRODUCT] = new_value;
  146. +}
  147. +
  148. +void tud_descriptor_override_serial(const char *new_value) {
  149. +    usbd_desc_str[USBD_STR_SERIAL] = new_value;
  150. +}
  151. +
  152.  #endif
  153.  
Advertisement
Add Comment
Please, Sign In to add comment