Skip to content

Commit c2b2818

Browse files
committed
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
1 parent 0d46b3d commit c2b2818

File tree

16 files changed

+103
-40
lines changed

16 files changed

+103
-40
lines changed

cores/nRF5/Arduino.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,11 @@ void loop( void ) ;
5454
#include "WMath.h"
5555
#include "HardwareSerial.h"
5656
#include "pulse.h"
57-
#endif
58-
#include "delay.h"
59-
#include "binary.h"
60-
#ifdef __cplusplus
6157
#include "Uart.h"
6258
#endif
6359

64-
#ifdef USE_TINYUSB
65-
#include "Adafruit_USBD_CDC.h"
66-
#endif
60+
#include "delay.h"
61+
#include "binary.h"
6762

6863
// Include board variant
6964
#include "variant.h"

cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore/Adafruit_TinyUSB_API.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#if CFG_TUD_ENABLED || CFG_TUH_ENABLED
2828

2929
#include "Adafruit_USBD_Device.h"
30+
#include "Adafruit_USBD_CDC.h"
3031
#include "Arduino.h"
3132

3233
extern "C" {

cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore/Adafruit_TinyUSB_nrf.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "Arduino.h"
3333
#include "Adafruit_USBD_Device.h"
34+
#include "Adafruit_TinyUSB_API.h"
3435

3536
//--------------------------------------------------------------------+
3637
// MACRO TYPEDEF CONSTANT ENUM DECLARATION

cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore/Adafruit_USBD_CDC.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
9393
bool isValid(void) { return _instance != INVALID_INSTANCE; }
9494
};
9595

96-
extern Adafruit_USBD_CDC SerialTinyUSB;
97-
9896
// Built-in support "Serial" is assigned to TinyUSB CDC
9997
// CH32 defines Serial as alias in WSerial.h
100-
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32)
98+
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32) && USB_CDC_DEFAULT_SERIAL
10199
#define SerialTinyUSB Serial
102100
#endif
103101

cores/nRF5/TinyUSB/Adafruit_TinyUSB_ArduinoCore/Adafruit_USBD_Device.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,6 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
259259
config->bNumInterfaces = _itf_count;
260260
#endif
261261
#else
262-
SerialTinyUSB.begin(115200);
263-
264262
// Init device hardware and call tusb_init()
265263
TinyUSB_Port_InitDevice(rhport);
266264
#endif

cores/nRF5/Uart.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ Uart::operator bool() {
256256
#define NRF_UART0_IRQn UART0_IRQn
257257
#endif
258258

259-
#if !defined(USB_CDC_DEFAULT_SERIAL)
259+
#if !USB_CDC_DEFAULT_SERIAL
260260
# if defined(PIN_SERIAL_CTS) && defined(PIN_SERIAL_RTS)
261261
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX, PIN_SERIAL_CTS, PIN_SERIAL_RTS );
262262
# else

cores/nRF5/Uart.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828

2929
#include "variant.h"
3030

31+
#ifndef USB_CDC_DEFAULT_SERIAL
32+
#define USB_CDC_DEFAULT_SERIAL (0)
33+
#endif
34+
35+
#if defined(USE_TINYUSB) && USB_CDC_DEFAULT_SERIAL
36+
#include "Adafruit_USBD_CDC.h"
37+
#endif
38+
3139
class Uart : public HardwareSerial
3240
{
3341
public:
@@ -78,7 +86,7 @@ class Uart : public HardwareSerial
7886
//
7987
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
8088
// pins are NOT connected to anything by default.
81-
#if defined(USB_CDC_DEFAULT_SERIAL)
89+
#if USB_CDC_DEFAULT_SERIAL
8290
#define SERIAL_PORT_MONITOR Serial
8391
#define SERIAL_PORT_USBVIRTUAL Serial
8492

cores/nRF5/libc/printf/putchar.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ extern "C"
6868
Serial.write((const uint8_t *)pbuf->buffer, pbuf->index);
6969
pbuf->index = 0;
7070
}
71-
#ifdef USB_CDC_DEFAULT_SERIAL
71+
#if USB_CDC_DEFAULT_SERIAL
7272
Serial.flush();
7373
#endif
7474
}
@@ -85,7 +85,7 @@ extern "C"
8585
Serial.write('\r');
8686
}
8787
Serial.write(c);
88-
#ifdef USB_CDC_DEFAULT_SERIAL
88+
#if USB_CDC_DEFAULT_SERIAL
8989
Serial.flush();
9090
#endif
9191
return;

cores/nRF5/utils/debug_utils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ static size_t capture_backtrace(uint32_t* backtrace_buffer, size_t buffer_size,
177177
*/
178178
static void store_fault_to_ram(exception_frame* ef, uint32_t exc_return_val)
179179
{
180-
#ifdef USB_CDC_DEFAULT_SERIAL
180+
#if USB_CDC_DEFAULT_SERIAL
181181
volatile fault_data_t* fault = FAULT_DATA_ADDR;
182182
fault->magic = FAULT_DATA_MAGIC;
183183
fault->pc = ef->pc;
@@ -240,7 +240,7 @@ static void store_fault_to_ram(exception_frame* ef, uint32_t exc_return_val)
240240
static void print_exception_data(exception_frame* ef)
241241
{
242242
// If USB is used for Serial we must avoid using it in HardFault handler
243-
#ifndef USB_CDC_DEFAULT_SERIAL
243+
#if !USB_CDC_DEFAULT_SERIAL
244244
if (Serial)
245245
{
246246
Serial.println("\n======== HARD FAULT DETECTED ========");
@@ -320,7 +320,7 @@ static void print_exception_data(exception_frame* ef)
320320
*/
321321
void check_and_report_fault()
322322
{
323-
#ifdef USB_CDC_DEFAULT_SERIAL
323+
#if USB_CDC_DEFAULT_SERIAL
324324
volatile fault_data_t* fault = FAULT_DATA_ADDR;
325325

326326
if (fault->magic == FAULT_DATA_MAGIC)

variants/Seeed_XIAO_nRF52840_Sense/variant.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,18 @@ static const uint8_t A5 = PIN_A5;
8585
#define PIN_NFC2 (31)
8686

8787
// Serial interfaces
88-
#define PIN_SERIAL1_RX (7)
89-
#define PIN_SERIAL1_TX (6)
90-
#define USB_CDC_DEFAULT_SERIAL 1
88+
#ifndef USB_CDC_DEFAULT_SERIAL
89+
#define USB_CDC_DEFAULT_SERIAL (1)
90+
#endif
91+
92+
#if USB_CDC_DEFAULT_SERIAL
93+
#define PIN_SERIAL1_RX (7)
94+
#define PIN_SERIAL1_TX (6)
95+
#else
96+
#define PIN_SERIAL_RX (7)
97+
#define PIN_SERIAL_TX (6)
98+
#define Serial1 Serial
99+
#endif
91100

92101
// SPI Interfaces
93102
#define SPI_INTERFACES_COUNT (2)

0 commit comments

Comments
 (0)