printf prints to console sporadically

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
boredqwerty
Newbie
Posts: 1
Joined: Fri Jul 15, 2022 4:28 pm

printf prints to console sporadically

Post by boredqwerty »

Hello,

I'm doing a little practice program on XMOS with UART TX/RX. To debug I tried using printf command to print stuff to the console.

This has been working fine until a specific case.

In uart_test_app task, calling printf ( printf("%c", data); ) only gives output every 65 char sent via serial, and all of the 65 char are output at the same time!
If I use ( printf("123456789\r"); ) I need to input via UART 7 chars before I can see this on the console:
123456789
123456789
123456789
123456789
123456789
123456789
1234
Here's the code for the task:

Code: Select all

void uart_test_app(client uart_tx_if uart_tx, client uart_rx_if uart_rx, server buttons_if btn_if)
{
    delay_milliseconds(200);
    uart_writeString("\r\n\r\nWelcome!\r\n", uart_tx);   // <--- this works fine

    while (1)
    {
        select
        {
            case uart_rx.data_ready():
                uint8_t data = uart_rx.read();
                if(data == CR || data == LF)
                {
                    uart_tx.write(LF);
                    uart_tx.write(CR);
                }
                else
                {
                    uart_tx.write(data); // this works fine 
                    printf("%c", data); // this prints all suddenly every 65 char received.
                    //printf("123456789\r"); // this prints all suddenly every 7 char received

                    //printchar(data);  // <--- This works!
                }
                break;
        }
    }
}

Here the main:

Code: Select all

int main(void)
{
    interface uart_rx_if i_rx;
    interface uart_tx_if i_tx;
    input_gpio_if i_gpio_rx[1];
    output_gpio_if i_gpio_tx[1];
    buttons_if btn_if;

    par
    {
        on tile[1]: buttons_read_task(btn_if);
        on tile[1]: led_flash_task(LED_FLASH_PERIOD);
        on tile[0]: say_hello();

        on tile[0]: output_gpio(i_gpio_tx, 1, p_uart_tx, null);
        on tile[0]: uart_tx(
                i_tx,
                null,
                UART_BAUDRATE,
                UART_PARITY_NONE,
                8,
                1,
                i_gpio_tx[0]);
        on tile[0].core[0] : input_gpio_with_events(i_gpio_rx, 1, p_uart_rx, null);
        on tile[0].core[0] : uart_rx(
                i_rx,
                null,
                RX_BUFFER_SIZE,
                UART_BAUDRATE,
                UART_PARITY_NONE,
                8,
                1,
                i_gpio_rx[0]);

        on tile[0]: uart_test_app(i_tx, i_rx, btn_if);
    }

    return 0;
}
As mentioned on the code, direct echo via uart_tx works perfectly.
Also printchar(data); works fine interestingly.

Printf works OK in other places (say_hello task):

Code: Select all

void say_hello(void)
{
    printf("Hello world!\r\n");

}
any advice on what to try?

thank you


User avatar
andrewxcav
Active Member
Posts: 62
Joined: Wed Feb 17, 2016 5:10 pm
Contact:

Post by andrewxcav »

I think this is just normal printf behaviour. The output will be buffered until a newline character is printed OR the buffer is flushed for another reason.

If you want to print immediately without a newline you could try adding:

fflush(stdout);

after your printf statement.
Post Reply