I can't print message on PuTTY terminal when i use UARTE

Hi, when I use other modules (like NRF_LOG ecc) no problem, when i use UART or UARTE the script is ok but i can't see anything on PuTTY terminal. In my case, a LED should toogle when I puty any button on keyboard (as it is) and print a message like "The character is x". 

That's the program I've written:

#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_uarte.h"


// Define a constant for TX & RX buffer size this size is really important and we can increase or decrease it
// according to our application, more buffer size means more RAM memory being used.
#define UART_TX_BUF_SIZE 64 // TX buffer size
#define UART_RX_BUF_SIZE 64 // RX buffer size


#define led 17 // define a pin for LED 1


// This event handler will be called whenever a uart event occurs
void uart_event_handler(app_uart_evt_t * p_event)
{

// create a varibale which will hold the characters that we are going to read over uart
uint8_t c;


// check if some communication error occured, this event will be trigered
if(p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
// Check the reason of error by accessing the error variable and then pass it to error handler
APP_ERROR_HANDLER(p_event->data.error_communication);
}

// check if fifo error occured, this might be fifo overflow or other relevant errors
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
// Check the reason of error by accessing the error variable and then pass it to error handler
APP_ERROR_HANDLER(p_event->data.error_code);
}
// check if there is a character available to be read from the buffer
else if (p_event->evt_type == APP_UART_DATA_READY)
{
// Read the available character and store it in the variable
app_uart_get(&c);

// print a message over uart port along with the character information
printf("The char received over UART is: %c \r\n", c);
nrf_gpio_pin_toggle(led);
}

// check if the transmission over uart port is finished i.e. uart tx empty event was generated
else if (p_event->evt_type == APP_UART_TX_EMPTY)
{
// toggle an led or use any other function here
nrf_gpio_pin_toggle(led);
}


}

// A funtion that will configure the UART settings
void uart_config(void)
{
uint32_t err_code; // a variable to hold error value

const app_uart_comm_params_t comm_params = // a struct that will hold all the uart configurations
{
RX_PIN_NUMBER, // Mention the Pin number for RX pin
TX_PIN_NUMBER, // Mention the Pin number for TX pin
RTS_PIN_NUMBER, // Mention the Pin number for RTS pin
CTS_PIN_NUMBER, // Mention the Pin number for CTS pin
APP_UART_FLOW_CONTROL_DISABLED, // Disable the hardware flow control, we only need it if we are using high baud rates, we can save some pins by disabling this
false, // event parity if true, if false no parity
NRF_UARTE_BAUDRATE_115200 // make sure to write NRF_UARTE_BAUDRATE_115200 and not the NRF_UART_BAUDRATE_115200
};


// initialize the uart with the fifo and pass it the parameters
// Parameters = (configurations, rx buffer size, tx buffer size, event handler, interrupt priority, error code variable)
APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_event_handler, APP_IRQ_PRIORITY_LOWEST, err_code);
APP_ERROR_CHECK(err_code); // check if some error occured during initialization

}

/**
* @brief Function for main application entry.
*/
int main(void)
{

// configure the led pin as output pin
nrf_gpio_cfg_output(led);

// call the uart configuration function and now wait for the events from the uart
uart_config();

// infinite loop
while (true)
{
// perform some other tasks here, if uart event occurs the processor will automatically take care of the interrupts

}

}


Please, someone can help me? Thanks a lot!

  • This is what I see when running your application on a DK:

    I can write in the UART terminal (Putty), and it is printed in the UART log from the nRF. 

    By default, the printf will print using RTT (which is not UART). This is why you can see it in the debug terminal in Segger Embedded Studio. If you want printf to be printed over UART instead, you need to enable it from sdk_config.h by setting:

    #define RETARGER_ENABLED 1

    (find it and change from 0 to 1)

    Then the printf() messages will be printed over UART (putty) instead of RTT.

    Ste_ said:
    Can I ask also why NRF_LOG_INFO doesn't work instead of printf()?

    In order to use NRF_LOG_INFO() you need to add it and start it from your application. The easiest way to do this is to look at some example that is already doing it. 

    Some clues are that you need a couple of extra sdk_config.h definitions, such as one that is choosing the backend for the log module (RTT or UART), and you need a function that starts the logging (nrf_log_init()), and some function to process the log, (NRF_LOG_PROCESS()).

    Look at how this is done in e.g. the sdk\examples\ble_peripheral\ble_app_hrs example.

    Best regards,

    Edvin

Related