How to use two UART in nRF52840-DK [Arduino Framework]

Hi! I want to use the two UART available in the nRF52840, but I do not find any documentation about how to do it with the Arduino Framework in PlatformIO. Does anybody have an idea of how to do it?

I am using the nRF52840 Development Kit.

Which exact board and core are you using? The Adafruit nRF52840 Feather Express and the GitHub - adafruit/Adafruit_nRF52_Arduino: Adafruit code for the Nordic nRF52 BLE SoC on Arduino core? If yes, the Uart.cpp code looks for the definition or the second UART pins and then gives you a Serial2 object.

There are only 2 variants which have these pins defined however. One is the Particle Xenon.

Which is also a NRF52840 board. So by using this board (board = particle_xenon) you should be able to do a quick sanity check whether Serial2 is accessible.

If this is not your board you can always

build_flags = 
   -D PIN_SERIAL2_RX=5
   -D PIN_SERIAL2_TX=4

(with correct pin numbers) in the platformio.ini. Then the core should see those pins as defined and give you Serial2.

If you are not using this core however, I’d need to know which one exactly you’re using :stuck_out_tongue:

Oh okay sorry you say you’re using the nRF52840_DK, so I guess it’s board = nrf52840_dk in your platformio.ini? Is that right?

In that case your underlying core is different.

The standard Serial one is constructed as

However, does the nRF52840 actually have two UART interfaces? I only see NRF_UART0 and NRF_UARTE0 which point to the same thing.

The Adafruit core apparently knows about NRF_UARTE1

Yeah sorry for the long chain of posts but this doesn’t seem to be possible in the sandeepmistry/arduino-nRF5 Core that is used when specifying board = nrf52840_dk. The startup .S file missing the needed interrupt vector for UARTE1_IRQHandler in its gcc_startup_nrf52.S file.

E.g. compare (missing)

to correct

Also the Uart handler only sems to be written against UART and not UARTE peripherals. You may try this code but it’s 99% not going to work.

#include <Arduino.h>
#include <Uart.h>

/* may be incorrect. pins numbers are 0 = D0, ..13 = D13, 14 = A0, .. A7, SCL, SDA, RX1, RX2 */
#define PIN_SERIAL2_RX         5 /* D5 */
#define PIN_SERIAL2_TX         4 /* D4 */

/* stolen from adafruit definitions */
#define   UARTE1_IRQn               40              /*!< 40 UARTE1                                                                 */
#define NRF_UARTE1_BASE             0x40028000UL
#define NRF_UARTE1                  ((NRF_UARTE_Type*)         NRF_UARTE1_BASE)

/* treat UARTE peripheral as UART */
Uart Serial2((NRF_UART_Type*) NRF_UARTE1, (IRQn_Type) UARTE1_IRQn, PIN_SERIAL2_RX, PIN_SERIAL2_TX);


extern "C"
{
  void UARTE1_IRQHandler()
  {
    Serial2.IrqHandler();
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("Hello world!!!");

  Serial2.begin(115200);
  Serial2.println("[FROM SECOND UART] Hello world!!!");
  Serial2.flush();
}

void loop() {
  Serial.println("Loop");
  Serial2.println("[FROM SECOND UART] Loop");
  Serial2.flush();
}

You should try to use a different board that has the Arduino nRF52 core, as e.g. the Particle Xenon and see if it works there. For feature request in the other core, create an issue at Issues · sandeepmistry/arduino-nRF5 · GitHub.

Hi Max! Thank you so much for answering my question, and as you expected, the code didn’t work. I’m going to request that feature vía Issues · sandeepmistry/arduino-nRF5 · GitHub.