This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

basic UART sample for nRF Connect SDK

Hi,

I am struggling to find a basic sample for UART communication in NCS. As an experienced Nrf5 SDK programmer, I find it very hard to convert my firmware to nRF Connect and Zephyr. There is no small and simple example in the NCS, the Zephyr website just lists of bunch of declarations, and searching the internet and devzone yields a bewildering amount of different solutions, dead links, with or without FIFO, UART or UARTE, incomprehensible config settings and lots of code that is documented as "not running", "unreliable" etc. I just need 2 UARTs to transmit commands  to sensors and receive data from them, preferably under interrupt.

thx,

Henk

  • found a sample in Zephyr/tests/drivers/uart/uart_basic_api but it does not run. Initially device_get_binding failed although all config settings were added, but after hours of searching it turned out to be the non-standard baudrate I needed (125000). Took me another hour of so to discover how to change the baudrate after binding, since the baudrate formula is not in the datasheet of the nRF9160 (?why?).

    Next, and still a problem, is the CONFIG_UART_INTERRUPT_DRIVEN flag that is set to "y" in prj.conf, but is off when I debug drivers/uart.h.  Apparantly, somewhere in the spaghetti heap of include files this flag turns off, and I cannot find where.

    The piece of code might work, since, if I patch uart.h and define the flag, data is transmitted from the UART. But patching library code is no sustainable solution in frequently updating IDEs.

    Please do not let me struggle for another 5 days or so......

    thx, Henk.

  • Hi, I'm very sorry for keeping you waiting so long. Here is a basic UART loopback sample:

    #include <zephyr.h>
    #include <device.h>
    #include <devicetree.h>
    #include <drivers/uart.h>
    
    static uint8_t uart_buf[1024];
    
    void uart_cb(const struct device *x, void *user_data)
    {
    	uart_irq_update(x);
    	int data_length = 0;
    
    	if (uart_irq_rx_ready(x)) {
    		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
    		uart_buf[data_length] = 0;
    	}
        uart_fifo_fill(x, uart_buf, data_length);
    }
    
    void main(void)
    {
    
        const struct device *uart = device_get_binding("UART_2"); // Use UART2 for the loopback
    	uart_irq_callback_set(uart, uart_cb);
    	uart_irq_rx_enable(uart);
    	uart_fifo_fill(uart, "UART loopback start!\r\n", 22);     // Output on UART2
    	printk("UART loopback start!\n");                         // Output on UART0, which is default for logging
    	while (1) {
    		k_cpu_idle();
    	}
    }
    

    In order to configure UART2, you can use an overlay file.

    &uart2 {
    	status = "okay";
    	current-speed = <115200>;
    	tx-pin = <31>;
    	rx-pin = <30>;
    };

    The standard device configuration is located in "zephyr/boards/arm/nrf9160dk_nrf9160/nrf9160dk_nrf9160_common.dts", so the overlay file will change the uart2 entry.

    EDIT: About the non standard baudrate, that's unfortunate, and the formula should be in the PS. I will follow up on that

  • I would also recommend watching this video on how devices are handled in Zephyr: https://www.youtube.com/watch?v=sWaxQyIgEBY

  • Hi Stian,

    thanks for your reply. Can you explain the problem with the CONFIG_UART_INTERRUPT_DRIVEN flag?

    thx, Henk

Related