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

Parents
  • 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

Reply
  • 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

Children
No Data
Related