Use Serial0 as the default UART in USB-CDC mode #4869
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What does this implement/fix?
This change fixes an error that occurs with a default configuration on ESP32 devices that have USB-CDC support (like ESP32-S2 and ESP32-C3). If you bootstrap one of these devices (esphome seems to default to Arduino rather than ESP-IDF) and try to configure them to use UART for something, you'll see the following error at compile time (in my case, this is with an ESP32-S2):
A workaround that has arisen within the community is to disable USB-CDC with a PlatformIO build flag, however this has the effect of breaking hardware logging over USB which is very useful to have:
It seems like the root cause here is that, when USB-CDC mode is enabled, the
Serial
device points to an instance ofUSBCDC
orHWCDC
rather thanHardwareSerial
. This is great for the logger, which accepts anything that extends theStream
base class (which bothUSBCDC
andHWCDC
do), however the UART component expects instances ofHardwareSerial
explicitly - which is appropriate since CDC serial is probably not useful for UART components anyway.In USB-CDC mode, it's necessary to reference
Serial0
rather thanSerial
to access UART0 - note the following code from the Arduino HardwareSerial library:This change updates the UART component code to reference Serial0 if ARDUINO_USB_CDC_ON_BOOT is true. This allows the code to compile and run as expected. We unfortunately can't always use Serial0 because it isn't defined as such outside of USB-CDC mode.
Types of changes
Test Environment
Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: