Skip to content

Commit fcba97c

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 fcba97c

File tree

13 files changed

+61
-47
lines changed

13 files changed

+61
-47
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_USBD_CDC.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,6 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
9595

9696
extern Adafruit_USBD_CDC SerialTinyUSB;
9797

98-
// Built-in support "Serial" is assigned to TinyUSB CDC
99-
// CH32 defines Serial as alias in WSerial.h
100-
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32)
101-
#define SerialTinyUSB Serial
102-
#endif
103-
104-
extern Adafruit_USBD_CDC SerialTinyUSB;
105-
10698
#endif // else of ESP32
10799
#endif // __cplusplus
108100

cores/nRF5/Uart.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include "Uart.h"
2120
#include "Arduino.h"
2221
#include "wiring_private.h"
2322

@@ -256,10 +255,10 @@ Uart::operator bool() {
256255
#define NRF_UART0_IRQn UART0_IRQn
257256
#endif
258257

259-
#if !defined(USB_CDC_DEFAULT_SERIAL)
258+
#if !USB_CDC_DEFAULT_SERIAL
260259
# if defined(PIN_SERIAL_CTS) && defined(PIN_SERIAL_RTS)
261260
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX, PIN_SERIAL_CTS, PIN_SERIAL_RTS );
262-
# else
261+
# elif defined(PIN_SERIAL_RX) && defined(PIN_SERIAL_TX) // may not be defined if USB CDC default is disabled
263262
Uart Serial( NRF_UART0, NRF_UART0_IRQn, PIN_SERIAL_RX, PIN_SERIAL_TX );
264263
# endif
265264
#endif

cores/nRF5/Uart.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
#include <cstddef>
2828

2929
#include "variant.h"
30+
#ifndef USB_CDC_DEFAULT_SERIAL
31+
#define USB_CDC_DEFAULT_SERIAL 0
32+
#endif
33+
#ifdef USE_TINYUSB
34+
#include "Adafruit_USBD_CDC.h"
35+
#endif
3036

3137
class Uart : public HardwareSerial
3238
{
@@ -62,7 +68,6 @@ class Uart : public HardwareSerial
6268
uint8_t uc_hwFlow;
6369
};
6470

65-
6671
// These serial port names are intended to allow libraries and architecture-neutral
6772
// sketches to automatically default to the correct port name for a particular type
6873
// of use. For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
@@ -78,19 +83,28 @@ class Uart : public HardwareSerial
7883
//
7984
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
8085
// pins are NOT connected to anything by default.
81-
#if defined(USB_CDC_DEFAULT_SERIAL)
86+
#if defined(USE_TINYUSB) && USB_CDC_DEFAULT_SERIAL
87+
// Built-in support "Serial" is assigned to TinyUSB CDC
88+
// Don't define Serial if user wants to use Serial for other purpose, e.g. UART
89+
#define Serial SerialTinyUSB
8290
#define SERIAL_PORT_MONITOR Serial
8391
#define SERIAL_PORT_USBVIRTUAL Serial
8492

8593
#define SERIAL_PORT_HARDWARE Serial1
8694
#define SERIAL_PORT_HARDWARE_OPEN Serial1
8795
#else
96+
// If USB CDC default is disabled, make Serial to be the default UART port
97+
#if defined(PIN_SERIAL1_RX) && defined(PIN_SERIAL1_TX) && \
98+
!defined(PIN_SERIAL_RX) && !defined(PIN_SERIAL_TX)
99+
#define Serial Serial1
100+
#endif
101+
88102
#define SERIAL_PORT_MONITOR Serial
89103
#define SERIAL_PORT_HARDWARE Serial
90104
#endif
91105

92-
extern Uart SERIAL_PORT_HARDWARE;
93-
94106
#if defined(PIN_SERIAL1_RX) && defined(PIN_SERIAL1_TX)
95-
extern Uart Serial1;
107+
extern Uart Serial1;
96108
#endif
109+
110+
extern Uart SERIAL_PORT_HARDWARE;

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ static const uint8_t A5 = PIN_A5;
8787
// Serial interfaces
8888
#define PIN_SERIAL1_RX (7)
8989
#define PIN_SERIAL1_TX (6)
90-
#define USB_CDC_DEFAULT_SERIAL 1
90+
#ifndef USB_CDC_DEFAULT_SERIAL
91+
#define USB_CDC_DEFAULT_SERIAL (1)
92+
#endif
9193

9294
// SPI Interfaces
9395
#define SPI_INTERFACES_COUNT (2)

variants/circuitplayground_nrf52840/variant.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ static const uint8_t A9 = PIN_A9 ;
9292
/*
9393
* Serial interfaces
9494
*/
95-
#define PIN_SERIAL1_RX (0)
96-
#define PIN_SERIAL1_TX (1)
97-
#define USB_CDC_DEFAULT_SERIAL 1
95+
#define PIN_SERIAL1_RX (0)
96+
#define PIN_SERIAL1_TX (1)
97+
#ifndef USB_CDC_DEFAULT_SERIAL
98+
#define USB_CDC_DEFAULT_SERIAL (1)
99+
#endif
98100

99101
/*
100102
* SPI Interfaces

variants/clue_nrf52840/variant.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ static const uint8_t A7 = PIN_A7 ;
8989
/*
9090
* Serial interfaces
9191
*/
92-
#define PIN_SERIAL1_RX (0)
93-
#define PIN_SERIAL1_TX (1)
94-
#define USB_CDC_DEFAULT_SERIAL 1
92+
#define PIN_SERIAL1_RX (0)
93+
#define PIN_SERIAL1_TX (1)
94+
#ifndef USB_CDC_DEFAULT_SERIAL
95+
#define USB_CDC_DEFAULT_SERIAL (1)
96+
#endif
9597

9698
/*
9799
* SPI Interfaces

variants/feather_nrf52840_express/variant.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ static const uint8_t AREF = PIN_AREF;
9191
/*
9292
* Serial interfaces
9393
*/
94-
#define PIN_SERIAL1_RX (1)
95-
#define PIN_SERIAL1_TX (0)
96-
#define USB_CDC_DEFAULT_SERIAL 1
94+
#define PIN_SERIAL1_RX (1)
95+
#define PIN_SERIAL1_TX (0)
96+
#ifndef USB_CDC_DEFAULT_SERIAL
97+
#define USB_CDC_DEFAULT_SERIAL (1)
98+
#endif
9799

98100
/*
99101
* SPI Interfaces

0 commit comments

Comments
 (0)