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();
}
}

Parents
  • Didrik

    I will look at the board controller link you referred me too. I want to do something very simple. I want to have a loop where I can read characters and echo them. The example is below. I do not want interrupts. I want a simple polling method. I want to poll for an input character and wait until I see a 0 (chacter available) and print it. the example below never sees a character. I get a "-35 x" for printk("%d %c\r\n",resIn,chIn); line. I am using TeraTerm for UART0 and I can see the printk and uart_poll_out() messages.

    any idea why I do not see any characters on uart_poll_in(). I can output the character using uart_poll_out() so I know the uart is selected correctly.

    #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("%c",chIn);
    uart_poll_out(uart,chIn);
    }
    k_sleep(1000);
    }

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

Reply
  • Didrik

    I will look at the board controller link you referred me too. I want to do something very simple. I want to have a loop where I can read characters and echo them. The example is below. I do not want interrupts. I want a simple polling method. I want to poll for an input character and wait until I see a 0 (chacter available) and print it. the example below never sees a character. I get a "-35 x" for printk("%d %c\r\n",resIn,chIn); line. I am using TeraTerm for UART0 and I can see the printk and uart_poll_out() messages.

    any idea why I do not see any characters on uart_poll_in(). I can output the character using uart_poll_out() so I know the uart is selected correctly.

    #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("%c",chIn);
    uart_poll_out(uart,chIn);
    }
    k_sleep(1000);
    }

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

Children
  • The code you sent me worked for me:

    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    -1 x
    
    a
    0 a
    
    aa0 
    
    
    
    0 
    
    
    
    
    -1 
    
    
    v
    0 v
    
    vv0 
    
    
    
    
    0 
    
    
    
    
    c
    0 c
    
    cc0 
    
    
    
    
    g
    0 
    
    
    
    
    0 g
    
    gghei
    0 
    
    
    
    
    0 
    
    
    
    
    0 h
    
    hh0 e
    
    ee0 i
    
    ii0 
    
    
    
    
    0 
    
    
    
    
    -1 
    
    
    -1 
    
    
    -1 
    
    
    
    

    (the -1 means that there were no input)

Related