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

When use the template_project (SDK16) UART working only on debug mode

Hi,

I try to use UART in the template_project. But if I try to use printf UART doesn't work. The project works fine in debug mode. How can i fix the issue?


#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
#include "nrf_uart.h"

#define UART_TX_BUFF_SIZE 128
#define UART_RX_BUFF_SIZE 128

#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
#define LED 8

void uart_err_handle(app_uart_evt_type_t *p)
{

}

int main(void)
{

  uint32_t err_code;

  nrf_gpio_cfg_output(LED);

  const app_uart_comm_params_t com_params = 
  {
    RX_PIN_NUMBER,
    TX_PIN_NUMBER,
    RTS_PIN_NUMBER,
    CTS_PIN_NUMBER,
    UART_HWFC,
    false,
    NRF_UART_BAUDRATE_115200
  };

  APP_UART_FIFO_INIT(&com_params, UART_RX_BUFF_SIZE, UART_TX_BUFF_SIZE, uart_err_handle, APP_IRQ_PRIORITY_LOWEST, err_code);

  APP_ERROR_CHECK(err_code);

  printf("Hello World!\r\n");

  while(true)
  {
    uint8_t cr;

    while( app_uart_get(&cr) != NRF_SUCCESS );// wait here until get character until from pc

    if(cr == 't')
    {
      nrf_gpio_pin_write(LED, 1 );
      printf("Leds Turned On!!\r\n");
    }

    if( cr == 'k')
    {
      nrf_gpio_pin_write(LED, 0 );
      printf("Led Turned Off!!\r\n");
    }

  }

  • Hello, 

    But if I try to use printf UART doesn't work.

    Could you elaborate what you mean when you say that it does not work?
    Are you receiving an error message, does the device reboot, or are you just unable to see the output in your serial terminal (but other than that the device seems to be working)?
    It is just the printf statement that is not working as expected, or does the app_uart_get also not work as intended?

    You will also need to have enabled the RETARGET library in order to have printf routed to the UART.

    Best regards,
    Karl 

Related