Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions cores/nRF5/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,11 @@ void loop( void ) ;
#include "WMath.h"
#include "HardwareSerial.h"
#include "pulse.h"
#endif
#include "delay.h"
#include "binary.h"
#ifdef __cplusplus
#include "Uart.h"
#endif

#ifdef USE_TINYUSB
#include "Adafruit_USBD_CDC.h"
#endif
#include "delay.h"
#include "binary.h"

// Include board variant
#include "variant.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@

#if CFG_TUD_ENABLED || CFG_TUH_ENABLED

#include "Adafruit_USBD_Device.h"
#include "Arduino.h"
#include "Adafruit_USBD_Device.h"
#include "Adafruit_USBD_CDC.h"

extern "C" {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "Arduino.h"
#include "Adafruit_USBD_Device.h"
#include "Adafruit_TinyUSB_API.h"

//--------------------------------------------------------------------+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,9 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
bool isValid(void) { return _instance != INVALID_INSTANCE; }
};

extern Adafruit_USBD_CDC SerialTinyUSB;

// Built-in support "Serial" is assigned to TinyUSB CDC
// CH32 defines Serial as alias in WSerial.h
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32)
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32) && USB_CDC_DEFAULT_SERIAL
#define SerialTinyUSB Serial
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
config->bNumInterfaces = _itf_count;
#endif
#else
SerialTinyUSB.begin(115200);

// Init device hardware and call tusb_init()
TinyUSB_Port_InitDevice(rhport);
#endif
Comment on lines 263 to 264
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.
Expand Down
2 changes: 1 addition & 1 deletion cores/nRF5/Uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Uart::operator bool() {
#define NRF_UART0_IRQn UART0_IRQn
#endif

#if !defined(USB_CDC_DEFAULT_SERIAL)
#if !USB_CDC_DEFAULT_SERIAL
# if defined(PIN_SERIAL_CTS) && defined(PIN_SERIAL_RTS)
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX, PIN_SERIAL_CTS, PIN_SERIAL_RTS );
# else
Expand Down
10 changes: 9 additions & 1 deletion cores/nRF5/Uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@

#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.
#endif

#if defined(USE_TINYUSB) && USB_CDC_DEFAULT_SERIAL
#include "Adafruit_USBD_CDC.h"
#endif

class Uart : public HardwareSerial
{
public:
Expand Down Expand Up @@ -78,7 +86,7 @@ class Uart : public HardwareSerial
//
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
// pins are NOT connected to anything by default.
#if defined(USB_CDC_DEFAULT_SERIAL)
#if USB_CDC_DEFAULT_SERIAL
#define SERIAL_PORT_MONITOR Serial
#define SERIAL_PORT_USBVIRTUAL Serial

Expand Down
4 changes: 2 additions & 2 deletions cores/nRF5/libc/printf/putchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ extern "C"
Serial.write((const uint8_t *)pbuf->buffer, pbuf->index);
pbuf->index = 0;
}
#ifdef USB_CDC_DEFAULT_SERIAL
#if USB_CDC_DEFAULT_SERIAL
Serial.flush();
#endif
}
Expand All @@ -85,7 +85,7 @@ extern "C"
Serial.write('\r');
}
Serial.write(c);
#ifdef USB_CDC_DEFAULT_SERIAL
#if USB_CDC_DEFAULT_SERIAL
Serial.flush();
#endif
return;
Expand Down
6 changes: 3 additions & 3 deletions cores/nRF5/utils/debug_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static size_t capture_backtrace(uint32_t* backtrace_buffer, size_t buffer_size,
*/
static void store_fault_to_ram(exception_frame* ef, uint32_t exc_return_val)
{
#ifdef USB_CDC_DEFAULT_SERIAL
#if USB_CDC_DEFAULT_SERIAL
volatile fault_data_t* fault = FAULT_DATA_ADDR;
fault->magic = FAULT_DATA_MAGIC;
fault->pc = ef->pc;
Expand Down Expand Up @@ -240,7 +240,7 @@ static void store_fault_to_ram(exception_frame* ef, uint32_t exc_return_val)
static void print_exception_data(exception_frame* ef)
{
// If USB is used for Serial we must avoid using it in HardFault handler
#ifndef USB_CDC_DEFAULT_SERIAL
#if !USB_CDC_DEFAULT_SERIAL
if (Serial)
{
Serial.println("\n======== HARD FAULT DETECTED ========");
Expand Down Expand Up @@ -320,7 +320,7 @@ static void print_exception_data(exception_frame* ef)
*/
void check_and_report_fault()
{
#ifdef USB_CDC_DEFAULT_SERIAL
#if USB_CDC_DEFAULT_SERIAL
volatile fault_data_t* fault = FAULT_DATA_ADDR;

if (fault->magic == FAULT_DATA_MAGIC)
Expand Down
15 changes: 12 additions & 3 deletions variants/Seeed_XIAO_nRF52840_Sense/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,18 @@ static const uint8_t A5 = PIN_A5;
#define PIN_NFC2 (31)

// Serial interfaces
#define PIN_SERIAL1_RX (7)
#define PIN_SERIAL1_TX (6)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (7)
#define PIN_SERIAL1_TX (6)
#else
#define PIN_SERIAL_RX (7)
#define PIN_SERIAL_TX (6)
#define Serial1 Serial
#endif

// SPI Interfaces
#define SPI_INTERFACES_COUNT (2)
Expand Down
15 changes: 12 additions & 3 deletions variants/circuitplayground_nrf52840/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,18 @@ static const uint8_t A9 = PIN_A9 ;
/*
* Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#else
#define PIN_SERIAL_RX (0)
#define PIN_SERIAL_TX (1)
#define Serial1 Serial
#endif

/*
* SPI Interfaces
Expand Down
15 changes: 12 additions & 3 deletions variants/clue_nrf52840/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ static const uint8_t A7 = PIN_A7 ;
/*
* Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#else
#define PIN_SERIAL_RX (0)
#define PIN_SERIAL_TX (1)
#define Serial1 Serial
#endif

/*
* SPI Interfaces
Expand Down
15 changes: 12 additions & 3 deletions variants/feather_nrf52840_express/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,18 @@ static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
#define PIN_SERIAL1_RX (1)
#define PIN_SERIAL1_TX (0)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (1)
#define PIN_SERIAL1_TX (0)
#else
#define PIN_SERIAL_RX (1)
#define PIN_SERIAL_TX (0)
#define Serial1 Serial
#endif

/*
* SPI Interfaces
Expand Down
15 changes: 12 additions & 3 deletions variants/feather_nrf52840_sense/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,18 @@ static const uint8_t AREF = PIN_AREF;
/*
* Serial interfaces
*/
#define PIN_SERIAL1_RX (1)
#define PIN_SERIAL1_TX (0)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (1)
#define PIN_SERIAL1_TX (0)
#else
#define PIN_SERIAL_RX (1)
#define PIN_SERIAL_TX (0)
#define Serial1 Serial
#endif

/*
* SPI Interfaces
Expand Down
15 changes: 12 additions & 3 deletions variants/itsybitsy_nrf52840_express/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ static const uint8_t A6 = PIN_A6 ;
/*
* Serial interfaces
*/
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (0)
#define PIN_SERIAL1_TX (1)
#else
#define PIN_SERIAL_RX (0)
#define PIN_SERIAL_TX (1)
#define Serial1 Serial
#endif

/*
* SPI Interfaces
Expand Down
14 changes: 11 additions & 3 deletions variants/nRF52840_Dongle/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ static const uint8_t AREF = PIN_AREF;
*/

// Arduino Header D0, D1
#define PIN_SERIAL1_RX (33) // P1.01
#define PIN_SERIAL1_TX (34) // P1.02
#define USB_CDC_DEFAULT_SERIAL 1
#ifndef USB_CDC_DEFAULT_SERIAL
#define USB_CDC_DEFAULT_SERIAL (1)
#endif

#if USB_CDC_DEFAULT_SERIAL
#define PIN_SERIAL1_RX (33)
#define PIN_SERIAL1_TX (34)
#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.
#endif

/*
* SPI Interfaces
Expand Down