Skip to content

Respect user-defined CDC string descriptor#580

Open
verylowfreq wants to merge 1 commit into
adafruit:masterfrom
verylowfreq:pr-cdc-string
Open

Respect user-defined CDC string descriptor#580
verylowfreq wants to merge 1 commit into
adafruit:masterfrom
verylowfreq:pr-cdc-string

Conversation

@verylowfreq
Copy link
Copy Markdown

This PR allows user sketches to set a custom string descriptor for the CDC-ACM interface.

What changed

Previously, Adafruit_USBD_CDC::begin() always set the CDC interface string descriptor to "TinyUSB Serial". This prevented user sketches from overriding the CDC interface string with setStringDescriptor().

This patch changes the behavior to:

  1. Check whether _strid has already been set by the user sketch.
  2. If _strid is still 0, use the default string descriptor "TinyUSB Serial".
  3. If the user sketch already called setStringDescriptor(), keep that user-defined string.

Use case

On some boards supported by Adafruit TinyUSB, Serial.begin() may be called before setup(). In that case, a sketch can clear and rebuild the USB configuration with a custom CDC interface string:

void setup() {
  // Clear all configurations including default Serial interface
  TinyUSBDevice.clearConfiguration();

  // Set custom string for CDC interface
  Serial.setStringDescriptor("My Special Serial Port");
  // For re-building descriptor
  TinyUSBDevice.addInterface(Serial);
  // The custom string descriptor is preserved.
  Serial.begin(115200);
}

This makes it possible for the custom CDC-ACM interface name to appear on the host instead of always using "TinyUSB Serial".

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the CDC-ACM interface initialization so a user sketch can set a custom CDC interface string descriptor via setStringDescriptor() without it being overwritten during Adafruit_USBD_CDC::begin().

Changes:

  • Only applies the default CDC interface string (“TinyUSB Serial”) if _strid has not already been set by the user.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 104 to +107
_instance = _instance_count++;
this->setStringDescriptor("TinyUSB Serial");
if (_strid == 0) {
this->setStringDescriptor("TinyUSB Serial");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants