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

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

Children
Related