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

simple UART example nrf9160dk

I am new to the nRF9160dk board. I have been able to compile and run some of the sample programs. I am now looking to have a simple program to talk to external uarts 0 and 1. I have found a githib project https://github.com/Rallare/fw-nrfconnect-nrf/tree/nrf9160_samples that has a simple UART example. I can build and load it but I am not sure where to configure the UART and how to get it to work.

I see the following for Uart0 in the device tree. so I am assuming this is where the baud rate and external pins are defined.

however when I run the program I do not get an indication that characters are seen on gpio_0 P28 which is on connector P27. I am using TeraTerm to connect.

I am assuming the TX/RX are relative to the nRF9160 board. in other words RX pin (28) is where it is expecting to see characters at 115200 baud.

I am assuming I do not need to connect to RTS/CTS.

I am sure it is something simple I left out or I am not doing. any help would be appreciated.

I am trying to build a simple application where I can type commands on a terminal emulator (putty, TeraTerm, etc.) and parse and execute those commands and send the output back to the terminal emulator. I have done this in the past on several other project and has proven to be quite effective in field support and debugging.


&uart0 {
status = "okay";
current-speed = <115200>;
tx-pin = <29>;
rx-pin = <28>;
rts-pin = <27>;
cts-pin = <26>;
};

the program is included below

/*

* Copyright (c) 2012-2014 Wind River Systems, Inc.

*

* SPDX-License-Identifier: Apache-2.0

*/

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

static u8_t uart_buf[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 main(void)
{
struct device *uart = device_get_binding("UART_0");

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

  • Didrik

    I tried the terminal emulator Termite. I do not see any characters on the input. chIn never gets changed from its initial value of 'x';

    below is my prj.conf file. should this be something different. I am sure it is something simple I have left out. any ideas?

    CONFIG_SERIAL=y
    CONFIG_TRUSTED_EXECUTION_NONSECURE=y
    CONFIG_UART_INTERRUPT_DRIVEN=n
    CONFIG_MAIN_STACK_SIZE=4096

    just to make sure I have repaste dthe code I am running.

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

    static u8_t uart_buf[1024];

    void main(void)
    {
    unsigned char chIn = 'x';
    int resIn = 0;

    struct device *uart = device_get_binding("UART_0");

    printk("Sensor Test Start!\n");
    while(1)
    {
    resIn = uart_poll_in(uart,&chIn);
    printk("%d %c\r\n",resIn,chIn);
    if(resIn == 0)
    {
    printk("chIn: %c",chIn);
    uart_poll_out(uart,chIn);
    }
    k_sleep(1000);
    }

    printk("UART loopback start!\n");
    while (1)
    {
    k_cpu_idle();
    }
    }

  • How do you connect to the board?

    Can you send me your <buildfolder>/zephyr/zephyr.dts file?

    Again, the code works fine for me. I have attached the project that I used, with a pre-compiled hex file so you can see if there are any differences.

    uart_loopback.zip

  • Didrik

    I was able to move the directory around under the sample directory and it is working for me now. I can use the virtual COM port UART_0

    for both input and output. Thanks for your help and suggestions.

    is there a way to get UART_0 pins GPIO 28/29 out to an edge connector? I want to be able to connect to a real COM port. If I wire up the nRF9160 part on a board by itself will UART_0 come to P0.28 and P0.29? this is the idea we are evaluating the device using the nRF9160dk but we will design our own board. so can I get these pins to an external connector? if we have our own board will the pins come out correctly? is there anything we would need to do special to get the pins out externally on our own board?

  • Timothy said:
    is there a way to get UART_0 pins GPIO 28/29 out to an edge connector?

     Yes.

     

    Timothy said:
    If I wire up the nRF9160 part on a board by itself will UART_0 come to P0.28 and P0.29?

     No, not without some extra steps.

    UART_0 and UART_1 are by default routed to the VCOM0 and 1 pins of the on-board debugger. However, you can change this using the board controller (the nRF52840 on the DK).

    You can read more about the board controller here, and on the infocenter.

    I've attached a modified version of Zephyr's hello_world sample where I have added CONFIG_BOARD_PCA10090_UART0_ARDUINO=y to the prj.conf. The sample is then built for the nrf52840_pca10090 board and flashed to the board controller. Remember to set SW5 on the DK to "nRF52".

    zephyr.hex

     

    Timothy said:
    if we have our own board will the pins come out correctly? is there anything we would need to do special to get the pins out externally on our own board?

     On a custom board, it should not be a problem. However, you might want to change which pins are used in the device tree. While you can do that by changing the .dts files in the boards/nrf9160_pca10090 folder, I recommend you instead define your own board. You can read more here: https://devzone.nordicsemi.com/nordic/cellular-iot-guides/b/getting-started-cellular/posts/nrf-connect-sdk-tutorial---part-3-temporary#h11sk6ks1jkp1figkuq6gxw1m1rctdp0

  • this can be closed. The ticket is answered for the using the UART on the nRF9160dk board.

Related