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?

Parents
  • Hi,

     

    Have you enabled the UART2 in your overlay? This is done by creating a nrf9160_pca10090ns.overlay file in your application folder, which should contain (P0.10 and P0.11 chosen as an example here):

    &uart2 {
    	status = "okay";
    	current-speed = <115200>;
    	tx-pin = <11>;
    	rx-pin = <10>;
    	rts-pin = <0xFFFFFFFF>;
    	cts-pin = <0xFFFFFFFF>;
    };

     

    Kind regards,

    Håkon

  • Yes, I have enabled UART2 in nrf9160_pca10090ns.overlay file.  At tx-pin of UART_2 I can see the voltage also. But when I connect the sensor, the data is not printed on the terminal. When I connect the sensor to UART_1 pins, the data is printed on the terminal. Does UART_2 also uses UART_0 for printing?

  • Jagruti said:
    But when I connect the sensor, the data is not printed on the terminal. When I connect the sensor to UART_1 pins, the data is printed on the terminal. Does UART_2 also uses UART_0 for printing?

    printk() goes through uart0 by default.

    Are you using the DK for testing, if yes, what pins are you using? The TXD pin is active low, while the RXD should float (ie: driven by the other side). If you measure it as high before attaching your external UART, its the TXD line.

Reply
  • Jagruti said:
    But when I connect the sensor, the data is not printed on the terminal. When I connect the sensor to UART_1 pins, the data is printed on the terminal. Does UART_2 also uses UART_0 for printing?

    printk() goes through uart0 by default.

    Are you using the DK for testing, if yes, what pins are you using? The TXD pin is active low, while the RXD should float (ie: driven by the other side). If you measure it as high before attaching your external UART, its the TXD line.

Children
No Data
Related