Skip to content

Commit ec11dfd

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 calls to Serial1 will error in compilation, will need to be renamed to Serial.
1 parent 0d46b3d commit ec11dfd

File tree

12 files changed

+55
-29
lines changed

12 files changed

+55
-29
lines changed

cores/nRF5/Arduino.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ void loop( void ) ;
5757
#endif
5858
#include "delay.h"
5959
#include "binary.h"
60+
61+
// Include board variant
62+
#include "variant.h"
63+
#ifndef USB_CDC_DEFAULT_SERIAL
64+
#define USB_CDC_DEFAULT_SERIAL (0)
65+
#endif
66+
6067
#ifdef __cplusplus
6168
#include "Uart.h"
6269
#endif
@@ -65,9 +72,6 @@ void loop( void ) ;
6572
#include "Adafruit_USBD_CDC.h"
6673
#endif
6774

68-
// Include board variant
69-
#include "variant.h"
70-
7175
#include "wiring.h"
7276
#include "wiring_digital.h"
7377
#include "wiring_analog.h"

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
@@ -78,13 +78,21 @@ class Uart : public HardwareSerial
7878
//
7979
// SERIAL_PORT_HARDWARE_OPEN Hardware serial ports which are open for use. Their RX & TX
8080
// pins are NOT connected to anything by default.
81-
#if defined(USB_CDC_DEFAULT_SERIAL)
81+
#if USB_CDC_DEFAULT_SERIAL
8282
#define SERIAL_PORT_MONITOR Serial
8383
#define SERIAL_PORT_USBVIRTUAL Serial
8484

8585
#define SERIAL_PORT_HARDWARE Serial1
8686
#define SERIAL_PORT_HARDWARE_OPEN Serial1
8787
#else
88+
// If USB CDC default is overridden, we want Serial to be the default hardware port
89+
#if defined(PIN_SERIAL1_RX) && defined(PIN_SERIAL1_TX) && !defined(PIN_SERIAL_RX) && !defined(PIN_SERIAL_TX)
90+
#define PIN_SERIAL_RX PIN_SERIAL1_RX
91+
#define PIN_SERIAL_TX PIN_SERIAL1_TX
92+
#undef PIN_SERIAL1_RX
93+
#undef PIN_SERIAL1_TX
94+
#endif
95+
8896
#define SERIAL_PORT_MONITOR Serial
8997
#define SERIAL_PORT_HARDWARE Serial
9098
#endif

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

variants/feather_nrf52840_sense/variant.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ static const uint8_t AREF = PIN_AREF;
9696
/*
9797
* Serial interfaces
9898
*/
99-
#define PIN_SERIAL1_RX (1)
100-
#define PIN_SERIAL1_TX (0)
101-
#define USB_CDC_DEFAULT_SERIAL 1
99+
#define PIN_SERIAL1_RX (1)
100+
#define PIN_SERIAL1_TX (0)
101+
#ifndef USB_CDC_DEFAULT_SERIAL
102+
#define USB_CDC_DEFAULT_SERIAL (1)
103+
#endif
102104

103105
/*
104106
* SPI Interfaces

0 commit comments

Comments
 (0)