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

printf with UART - blocking way?

Hello,

I used the uart example to print text over serial communication from the SDK v11.

The problem is when I write lot of text consecutively, I don't receive all data on the terminal. (It clearly appears when I change the UART_TX_BUF_SIZE to a small value) I found that the file \sdk\components\libraries\uart\retarget.c implements the IO primitives. In the function _write, it appears that the FIFO memory is quiclky full.

int _write(int file, const char * p_char, int len) {
    int i;
    UNUSED_PARAMETER(file);

    for (i = 0; i < len; i++)
    {
        if (app_uart_put(*p_char++) == NRF_ERROR_NO_MEM)
		blinkErr(); // or return i; to allow printf to report error to application code
    }
    return len;
}

And it blinks. I also found that app_uart_put is a non-blocking call. It explains that I can fill the FIFO before it is empty.

According to this question, there was a bug (devzone.nordicsemi.com/.../) but it should be fixed in the SDK v11.

In any case, I am looking for a way to send data over UART synchronously (yes, 'A' is for asynchronous...) Do you have any ideas ? Make a loop, uart option ?

Thank you in advance !

Parents Reply
  • Hi Mathias

    When you run the code from interrupt context you will block the UART interrupts, which prevents the UART driver from working properly to empty the UART buffers. 

    An alternative workaround to this is to make sure the UART interrupts run at a higher interrupt priority (lower pri number) than the interrupt context you are currently in. 

    Best regards
    Torbjørn

Children
Related