This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF5340 UART connection

nRF52840DK devicetree describes both uart0 and uart1 explicitly in nrf52840dk_nrf52840.dts file: https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs1/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts#L142-L157.

&uart0 {
    compatible = "nordic,nrf-uart";
    status = "okay";
    current-speed = <115200>;
    tx-pin = <6>;
    rx-pin = <8>;
    rts-pin = <5>;
    cts-pin = <7>;
};

arduino_serial: &uart1 {
    status = "okay";
    current-speed = <115200>;
    rx-pin = <33>;
    tx-pin = <34>;
};

Firstly, I want you to locate pins for uart1: <33> and <34>. Where are they on nRF52840DK?

And nRF5340PDK devicetree describes only uart0 in nrf5340pdk_nrf5340_cpuapp_common.dts file: https://github.com/nrfconnect/sdk-zephyr/blob/v2.3.0-rc1-ncs1/boards/arm/nrf5340pdk_nrf5340/nrf5340pdk_nrf5340_cpuapp_common.dts#L96-L103.

&uart0 {
    status = "okay";
    current-speed = <115200>;
    tx-pin = <20>;
    rx-pin = <22>;
    rts-pin = <19>;
    cts-pin = <21>;
};

As I understand, uart0 is routed to the interface MCU. Thus, how can I use another serial uart on nRF5340DK? Does it exist there at least? Should I edit a devicetree specification (nrf5340pdk_nrf5340_cpuapp_common.dts, nrf5340pdk_nrf5340_cpuapp.dts, nrf5340pdk_nrf5340_cpuappns.dts, nrf5340pdk_nrf5340_cpunet.dts) myself to define uart1, for example? And how can I define uart1 there? Can I just add something like this to the devicetree file:

&uart1 {
    status = "okay";
    current-speed = <115200>;
    rx-pin = <ANY_PIN>;
    tx-pin = <ANY_PIN>;
};

When I built and run such an example on nRF52840DK, then uart1 initialized properly, but in the case nRF5340PDK uart1 binding failed. It's expectable since uart1 isn't defined in devicetree file. Tell me, please, should I define it there myself, as I described above, or should I do something else to use other Serial UARTs?

#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <sys/printk.h>
#include <drivers/uart.h>

void main(void) {
    struct device *uart = device_get_binding(DT_LABEL(DT_NODELABEL(uart1))); // uart = device_get_binding("UART_1");
    if (!uart) {
        printk("UART binding failed\n");
    } else {
        printk("UART initialized\n");
    }
}
# Enable the UART driver
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_SERIAL=y
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_UART_0_NRF_FLOW_CONTROL=y

Related