Skip to content

Commit dde6a09

Browse files
committed
Allow USB CDC Serial to be disabled, redirect to UART
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.
1 parent 0d46b3d commit dde6a09

File tree

18 files changed

+101
-40
lines changed

18 files changed

+101
-40
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ For example: `'-DCONFIG_MAIN_TASK_STACK_SIZE=512'` This will set the main task s
143143
* `CONFIG_WDT_TIMEOUT_SECONDS` - set the number of seconds before the watchdog times out (0 = disable watchdog, default = 0).
144144
* `CONFIG_PRINTF_BUFFER_SIZE` - set the size of the buffer used for `printf` and `Serial.printf` (default 128 bytes, set to 1 for no buffer).
145145
* `CONFIG_PRINTF_BUFFER_INDEX` - set the FreeRTOS storage pointer index used for managing the `printf` buffer (default 0).
146+
* `USB_CDC_DEFAULT_SERIAL` - For devices with USB CDC support, this enables the USB CDC serial by default, set to 0 to disable (default 1), Serial will be redirected to the UART if this is disabled.
146147
* Nimble configuration options can also be included, the list of those can be found [here](https://h2zero.github.io/NimBLE-Arduino/md__command_line_config.html)
147148
* Other compiler options or definitions for other libraries can also be specified.
148149

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
#if CFG_TUD_ENABLED || CFG_TUH_ENABLED
2828

29-
#include "Adafruit_USBD_Device.h"
3029
#include "Arduino.h"
30+
#include "Adafruit_USBD_Device.h"
31+
#include "Adafruit_USBD_CDC.h"
3132

3233
extern "C" {
3334

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
259259
config->bNumInterfaces = _itf_count;
260260
#endif
261261
#else
262-
SerialTinyUSB.begin(115200);
263262

264263
// Init device hardware and call tusb_init()
265264
TinyUSB_Port_InitDevice(rhport);

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/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ int main( void )
6969

7070
#ifdef USE_TINYUSB
7171
TinyUSBDevice.begin(0);
72+
#if USB_CDC_DEFAULT_SERIAL
73+
Serial.begin(115200);
74+
#endif
7275
#endif
7376

7477
_loopTaskHandle = xTaskCreateStatic(loopTask, "mlt", MAIN_TASK_STACK_SIZE,

0 commit comments

Comments
 (0)