scanf is not working in NRF5340 using nrf-connect SDK

Hi,

I am trying to use scanf to take the user input from the debug console for the app core.

I have added the below code snippet in my application.

#include <stdio.h>
    int read_data = 0;
    printk("press anything to scan \r\n");
    scanf("%d", &read_data);
    printk("read_data: %d \r\n",read_data);

But the above code does not wait for scanf and it outputs a "0" always. The output is pasted below.

press anything to scan
read_string: 0

I have tried and added the all below configs to prj.conf.

# enable console
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_RTT_CONSOLE=n
CONFIG_USE_SEGGER_RTT=n
CONFIG_LOG_BACKEND_RTT=n

CONFIG_CBPRINTF_LIBC_SUBSTS=y

CONFIG_MINIMAL_LIBC=n
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE=2048


CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_NEWLIB_LIBC_FLOAT_SCANF=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_CONSOLE_HANDLER=y
CONFIG_STDOUT_CONSOLE=y

CONFIG_PICOLIBC_IO_FLOAT=y

But none of the above configs worked for scanf.

However I was able to get the uart rx working via interrupt and read-fifo API and the code is mentioned below.

static uint8_t uart_buf[1024];
void uart_cb(const struct device *x, void *user_data)
{
    uart_irq_update(x);
    int data_length = 0;

    if (uart_irq_rx_ready(x)) {
        data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
        uart_buf[data_length] = 0;
    }
    printk("data_length:%d \r\n",data_length);
    printk("%s %s \r\n", uart_buf, uart_buf);
}
static const struct device *gpUART_Device = DEVICE_DT_GET(DT_NODELABEL(uart0));

    uart_irq_callback_set(gpUART_Device, uart_cb);
    uart_irq_rx_enable(gpUART_Device);
The above code has a limitation that the "uart_fifo_read" API is able to read only 1 byte of character at a time which is not what we want.

Please could someone help me with the correct configuration to get the scanf working on NRF5340?

Thanks and regards,

Ajit S J

Parents Reply Children
No Data
Related