UART + RTT LOG Setup

Hello, 

I have my own custom board with nRF52832 and a NB-iot module SIM7080G that interfaces via UART. 

I can easily program the device through SWD using J-Link. I am even able to print things using RTT.

I have a button and made a simple program to print a thing once the button is pressed:

 

while (true)
    { 
        if(nrf_gpio_pin_read(button) == 1){
          NRF_LOG_INFO("Button pressed!\r\n");
          NRF_LOG_FLUSH();
          nrf_gpio_pin_clear(led1);
          nrf_delay_ms(1200);
        }else{
          nrf_gpio_pin_set(led1);
        }
    }

The thing is I want to communicate via UART (TX pin P0.04, RX pin P0.03) to the SIM7080G module at the same time I print things using RTT Log. This module is AT COMMAND so it would be really practical to write on the RTT in the computer and send this information to TX pin in the UART. After that, listen to the RX PIN (answer from the module) and print it again to the RTT Log so I can see it in the computer.

I tried setting up the UART in the corresponding pins like that (attached the full code):

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "nrf_drv_uart.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

static nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0);

static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
{
    if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
    {
        if (p_event->data.rxtx.bytes)
        {
            // Event to notify that data has been received
        }
    }
    else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
    {
        // Event to notify that an error has occured in the UART peripheral
    }
    else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
    {
       // Event to notify that the last byte from FIFO has been transmitted

    }
}

static void uart_init()
{
    nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
    uart_config.baudrate = UART_BAUDRATE_BAUDRATE_Baud115200; //User defined
    uart_config.hwfc = NRF_UART_HWFC_DISABLED; //User defined
    uart_config.interrupt_priority = APP_IRQ_PRIORITY_LOWEST; //User defined
    uart_config.parity = NRF_UART_PARITY_EXCLUDED; //User defined
    uart_config.pselrxd = 3; //User defined
    uart_config.pseltxd = 4; //User defined

    uint32_t err_code = nrf_drv_uart_init(&m_uart, &uart_config, uart_event_handler);
    APP_ERROR_CHECK(err_code);
}

#define led1 15
#define led2 16
#define power_key 5
#define button 7

int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();
    
    //uart_init(); // After default definition, before Advertise Start

    /* Configure board. */
    nrf_gpio_cfg_output(power_key);
    nrf_gpio_cfg_output(led1);
    nrf_gpio_cfg_output(led2);
    nrf_gpio_pin_set(led1);
    nrf_gpio_pin_set(led2);
    nrf_gpio_pin_clear(power_key);
    nrf_delay_ms(200);
    nrf_gpio_pin_set(power_key);

    nrf_gpio_cfg_input(button, NRF_GPIO_PIN_NOPULL);
    NRF_LOG_INFO("OXEEN Init\r\n");
    NRF_LOG_FLUSH();
    /* Toggle LEDs. */
    while (true)
    { 
        if(nrf_gpio_pin_read(button) == 1){
          NRF_LOG_INFO("Button pressed!\r\n");
          NRF_LOG_FLUSH();
          nrf_gpio_pin_clear(led1);
          nrf_delay_ms(1200);
        }else{
          nrf_gpio_pin_set(led1);
        }
    }
}

But I am getting error 

<error> app: ERROR 8 [NRF_ERROR_INVALID_STATE]
at line 47 in the code attached.

I am really new in the Nordic Semiconductor environment so any kind of help would be really appreciated.

Thank you,

Toni

  • I want to add that line 60 is uncommented in the uploaded code.

  • Hi,

    You are getting NRF_ERROR_INVALID_STATE returned from nrf_drv_uart_init(), which means that UART is already enabled. Logging is enabled beforehand, so I suspect you have enabled the UART backend for nrf_log? If so, you need to modify sdk_config.h so that NRF_LOG_BACKEND_UART_ENABLED is set to 0.

    As you write you want RTT logging you also need to ensure you set NRF_LOG_BACKEND_RTT_ENABLED to 1, and I also recommend you set NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF_ENABLED to 0.

  • Thanks Einar,

    I solved the ERROR 8 with what you said. Now I am having trouble transmiting. I am doing this:

    char toSend[2] = {'X','E'};
    
    uint32_t err_code = nrf_drv_uart_tx(&m_uart, &toSend[0], 2);
    if(err_code = NRFX_SUCCESS){
      NRF_LOG_INFO("OK!");
    }else{
      NRF_LOG_INFO("no OK!");
    }
    APP_ERROR_CHECK(err_code);

    I am trying to send two characters via UART. I am receiving message "no Ok!" in RTT viewer. Why am I getting error?

    I understand I am using the most low level UART library and I do so because the app_uart is getting me into a lot of trouble because of dependencies... I did start my project from ble_app_multilink_central_pca10040 example. When I tried to add app_uart_fifo.c and everything I started having a lot of problems.

    Why is that uart not working?

    Could you help me developing a simple tx/rx using nrf_drv_uart library?

    Thanks!

  • Hi,

    Toni Alomar said:
    I am receiving message "no Ok!" in RTT viewer. Why am I getting error?

    I don't know. Can you print the error code as well? That should give some hints.

    Toni Alomar said:
    Could you help me developing a simple tx/rx using nrf_drv_uart library?

    The SDK has the UART driver and several UART libraries built on top, I would think you should be able to use one of those. Building your own does not immediately seem like a good idea (and if you want to do so, that is up to you).

Related