adafruit / Adafruit_nRF52_Arduino

Adafruit code for the Nordic nRF52 BLE SoC on Arduino
Other
601 stars 489 forks source link

Use Serial1 without including "Adafruit_TinyUSB.h" #734

Open arms22 opened 2 years ago

arms22 commented 2 years ago

Is your feature request related to a problem? Please describe.

the following code causes undefined reference to 'Adafruit_USBD_CDC::available()' when targeting the nRF52840 boards. I understand that the header "Adafruit_TinyUSB.h" needs to be included, but I don't want to include it for power saving.

void setup() {
  Serial1.begin(115200);
}

void loop() {
}

This is because serialEventRun refers to Serial. https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/d75bb2a8524e7f00f725eb2ab27a8996be65e7a3/cores/nRF5/Uart.cpp#L27

How about changing the call to Serial.available to a call to the weak symbol?

int Serial_available() __attribute__((weak));

void serialEventRun(void)
{
  if (serialEvent && Serial_available() ) serialEvent();
}
hathach commented 2 years ago

I think it is better to add the #if defined(USE_TINYUSB) && defined(NRF52840) for the Serial, if you make an PR. I will be happy to review/merge.

Update: USE_TINYUSB seems to always defined for nrf52840, i will check this out later. If you could, please make an PR for your suggestion that will make it quicker to adopt the changes.

arms22 commented 2 years ago

I just compiled it. I will make PR after the test. https://github.com/arms22/Adafruit_nRF52_Arduino/commit/1f4f503192ca4a24831a6e8ec522789c9344f78a