Allow USB Serial to be disabled, redirect to UART Serial#93
Allow USB Serial to be disabled, redirect to UART Serial#93
Conversation
There was a problem hiding this comment.
Pull request overview
This PR makes USB_CDC_DEFAULT_SERIAL overridable so that when USB CDC is disabled (USB_CDC_DEFAULT_SERIAL=0), Serial is redirected to the UART hardware port (using the board’s Serial1 pins where applicable).
Changes:
- Make
USB_CDC_DEFAULT_SERIALin several nRF52840 variants conditional (#ifndef ... #define ...) to allow build-time overrides. - Replace
#ifdef/#ifndef USB_CDC_DEFAULT_SERIALwith value-based#if USB_CDC_DEFAULT_SERIALchecks in core utilities. - Introduce UART pin remapping logic intended to convert
Serial1pins intoSerialpins when USB CDC is not the default.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| variants/nRF52840_Dongle/variant.h | Make USB_CDC_DEFAULT_SERIAL override-friendly while defaulting to 1. |
| variants/itsybitsy_nrf52840_express/variant.h | Same: allow override, default to USB CDC serial. |
| variants/feather_nrf52840_sense/variant.h | Same: allow override, default to USB CDC serial. |
| variants/feather_nrf52840_express/variant.h | Same: allow override, default to USB CDC serial. |
| variants/clue_nrf52840/variant.h | Same: allow override, default to USB CDC serial. |
| variants/circuitplayground_nrf52840/variant.h | Same: allow override, default to USB CDC serial. |
| variants/Seeed_XIAO_nRF52840_Sense/variant.h | Same: allow override, default to USB CDC serial. |
| cores/nRF5/utils/debug_utils.cpp | Use value-based checks so USB_CDC_DEFAULT_SERIAL=0 behaves correctly. |
| cores/nRF5/libc/printf/putchar.cpp | Gate Serial.flush() based on the macro’s value (USB CDC vs UART). |
| cores/nRF5/Uart.h | Switch to value-based macro logic and add pin remapping for UART-as-Serial. |
| cores/nRF5/Uart.cpp | Instantiate Serial UART object when USB_CDC_DEFAULT_SERIAL is 0. |
| cores/nRF5/Arduino.h | Ensure variant.h is included early and provide a fallback default for the macro. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8e9da42 to
ec11dfd
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8a1d4f5 to
fcba97c
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
fcba97c to
c2b2818
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore/Adafruit_TinyUSB_API.cpp
Outdated
Show resolved
Hide resolved
This will set the default Serial output to UART if USB CDC is disabled by setting USB_CDC_DEFAULT_SERIAL to 0. If the board has serial1 pins defined they will be redefined to Serial and Serial1 will be aliased to Serial
c2b2818 to
515355a
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| #else | ||
| #define PIN_SERIAL_RX (33) | ||
| #define PIN_SERIAL_TX (34) | ||
| #define Serial1 Serial |
There was a problem hiding this comment.
Using a macro to alias Serial1 to Serial (when USB CDC is disabled) could cause issues in contexts where Serial1 is used as more than just a variable reference. For example, if any code attempts to take the address of Serial1 (&Serial1), uses it in template specializations, or passes it as a reference parameter with explicit type checking, the macro substitution will create type confusion. A safer approach would be to use a reference alias (e.g., 'Uart& Serial1 = Serial;') or document the limitations of this macro-based aliasing.
| TinyUSB_Port_InitDevice(rhport); | ||
| #endif |
There was a problem hiding this comment.
The removal of SerialTinyUSB.begin(115200) changes the initialization behavior for USB CDC Serial. Previously, USB CDC Serial was automatically initialized; now it requires explicit Serial.begin() calls in user code. While this creates consistency with UART Serial behavior (which always required initialization), it represents a breaking API change that could cause existing sketches to fail silently. Consider documenting this breaking change, or add conditional initialization based on USB_CDC_DEFAULT_SERIAL to maintain backward compatibility for USB CDC mode while requiring initialization for UART mode.
| TinyUSB_Port_InitDevice(rhport); | |
| #endif | |
| TinyUSB_Port_InitDevice(rhport); | |
| // For boards that use USB CDC as the default serial port, preserve | |
| // backward-compatible behavior by auto-initializing Serial. | |
| // This matches the previous implicit SerialTinyUSB.begin(115200). | |
| #if defined(USB_CDC_DEFAULT_SERIAL) && USB_CDC_DEFAULT_SERIAL | |
| if (!Serial) { | |
| Serial.begin(115200); | |
| } | |
| #endif | |
| #endif |
| #include "variant.h" | ||
|
|
||
| #ifndef USB_CDC_DEFAULT_SERIAL | ||
| #define USB_CDC_DEFAULT_SERIAL (0) |
There was a problem hiding this comment.
The default value for USB_CDC_DEFAULT_SERIAL in Uart.h is set to 0, while all variant.h files default it to 1. This creates an inconsistency in the expected behavior. While the include order ensures variant.h defines it first (since variant.h is included in Uart.h on line 29), the semantic inconsistency is confusing. Consider either removing this default from Uart.h (letting variant.h always control it), or changing it to match the variant defaults (1), or documenting why the global default differs from per-variant defaults.
| #define USB_CDC_DEFAULT_SERIAL (0) | |
| #define USB_CDC_DEFAULT_SERIAL (1) |
Allow USB Serial to be disabled, redirect to UART Serial
This will set the default Serial output to UART if USB CDC is disabled by setting USB_CDC_DEFAULT_SERIAL to 0.
If the board has serial1 pins defined they will be redefined to Serial and Serial1 will be aliased to Serial