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

Using multiple UARTs on nRF9160dk

Hello,

I am using UART_1 in my program to comminicate with other sensor. So the data is read from UART_1 and print to UART_0. Now I want to communicate with other sensor simultaneously. So I am using UART_2 for that purpose. When I connect both sensors to UART_1 and UART_2, only the data from UART_1 is read and it is printed on the terminal. But the data from UART_2 is not read and printed. So how to read and print the data from both UART on the terminal at the same time.

The Code is:

#include <zephyr.h>
#include <misc/printk.h>
#include <uart.h>

static u8_t uart_buf[1024];
static u8_t uart_buf1[1024];


void uart_cb(struct device *x)
{
	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;
	}
	printk("%s", uart_buf);
}

void uart_cb1(struct device *x1)
{
	uart_irq_update(x1);
	int data_length = 0;

	if (uart_irq_rx_ready(x1)) {
		data_length = uart_fifo_read(x1, uart_buf1, sizeof(uart_buf1));
		uart_buf1[data_length] = 0;
	}
	printk("%s", uart_buf1);
}


void main(void)
{
	struct device *uart = device_get_binding("UART_2");
    struct device *uart1 = device_get_binding("UART_1");

	uart_irq_callback_set(uart, uart_cb);
	uart_irq_rx_enable(uart);
        uart_irq_callback_set(uart1, uart_cb1);
	uart_irq_rx_enable(uart1);
	printk("UART loopback start!\n");
	while (1) {
		k_cpu_idle();
	}
}

Is this code correct or is there any other way to use 2 UARTS simultaneously?

Related