Skip to content

Allow USB Serial to be disabled, redirect to UART Serial#93

Open
h2zero wants to merge 1 commit intomasterfrom
usb-serial-override
Open

Allow USB Serial to be disabled, redirect to UART Serial#93
h2zero wants to merge 1 commit intomasterfrom
usb-serial-override

Conversation

@h2zero
Copy link
Owner

@h2zero h2zero commented Feb 10, 2026

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

Copy link

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 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_SERIAL in several nRF52840 variants conditional (#ifndef ... #define ...) to allow build-time overrides.
  • Replace #ifdef/#ifndef USB_CDC_DEFAULT_SERIAL with value-based #if USB_CDC_DEFAULT_SERIAL checks in core utilities.
  • Introduce UART pin remapping logic intended to convert Serial1 pins into Serial pins 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.

@h2zero h2zero force-pushed the usb-serial-override branch from 8e9da42 to ec11dfd Compare February 11, 2026 00:10
@h2zero h2zero marked this pull request as ready for review February 11, 2026 00:10
@h2zero h2zero requested a review from Copilot February 11, 2026 00:10
Copy link

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

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.

@h2zero h2zero force-pushed the usb-serial-override branch 2 times, most recently from 8a1d4f5 to fcba97c Compare February 12, 2026 00:42
@h2zero h2zero requested a review from Copilot February 12, 2026 01:46
Copy link

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

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.

Copy link

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

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.

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
Copy link

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

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
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Comment on lines 263 to 264
TinyUSB_Port_InitDevice(rhport);
#endif
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
#include "variant.h"

#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (0)
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

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

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.

Suggested change
#define USB_CDC_DEFAULT_SERIAL (0)
#define USB_CDC_DEFAULT_SERIAL (1)

Copilot uses AI. Check for mistakes.
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.

1 participant