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

thingy91:nRF52840 int64_t convert the value to string in sprintf

thingy91:nRF52840 int64_t convert the value to string in sprintf

Add next line in proj.conf

CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_NANO=n

If you do the following:

int64_t xxx = 1609665300;
int8_t unix_time[256] = {'\0'};

sprintf(unix_time, "%"PRIu64" ", xxx);
printk("unix_time = %s\n",unix_time);
sprintf(unix_time, "%llu", xxx);
printk("unix_time = %s\n",unix_time);
sprintf(unix_time, "%lld", xxx);
printk("unix_time = %s\n",unix_time);

As a result

unix_time = lu
unix_time = lu
unix_time = ld

Is there a CONFIG that is not enough?

Parents
  • Hi!

    Interesting. Your configuration should be correct.
    Which version of the nRF Connect SDK are you using? Are you getting any build errors/warnings?

    Best regards,
    Carl Richard

  • The nRFConnect SDK version is v1.4.2.

    No errors or warnings have been issued.

    To try, I added the following define, but the phenomenon did not change.

    #define CONFIG_EXTERNAL_LIBC 1
    #define CONFIG_REQUIRES_FULL_LIBC 1

  • I see. Could you share the prj.conf file of your project with me?

    When you say:

    unix_time = lu
    unix_time = lu
    unix_time = ld

    Do you mean that the full number "1609665300" isn't printed? 

    Best regards,
    carl Richard

  • What I want to do is a sprintf function unix_time expand the value of the int64_t to the string array.

    prj.conf:

    #
    # Copyright (c) 2019 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #

    CONFIG_BT=y
    CONFIG_BT_CENTRAL=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="Nordic Discovery Sample"

    CONFIG_BT_SCAN=y
    CONFIG_BT_SCAN_FILTER_ENABLE=y
    CONFIG_BT_SCAN_UUID_CNT=1

    CONFIG_BT_SMP=y
    CONFIG_BT_GATT_CLIENT=y
    CONFIG_BT_GATT_DM=y
    CONFIG_HEAP_MEM_POOL_SIZE=2048

    # Needed to print UUID.
    CONFIG_BT_DEBUG_LOG=y
    #CONFIG_BT_GATT_DM_DATA_PRINT=y

    CONFIG_USE_SEGGER_RTT=y
    CONFIG_UART_CONSOLE=n
    CONFIG_RTT_CONSOLE=y
    CONFIG_LOG_PRINTK=y

    # Config logger
    CONFIG_LOG=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=n

    CONFIG_BT_LL_SOFTDEVICE_DEFAULT=y
    CONFIG_BT_CTLR=y

    #CONFIG_BT_SETTINGS=y
    CONFIG_SETTINGS=y

    CONFIG_BT_OBSERVER=y
    CONFIG_BT_BACKGROUND_SCAN_INTERVAL=2048
    CONFIG_BT_BACKGROUND_SCAN_WINDOW=18

    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_NVS=y
    CONFIG_NVS_LOG_LEVEL_DBG=y
    CONFIG_REBOOT=y
    CONFIG_MPU_ALLOW_FLASH_WRITE=y

    CONFIG_NRFX_CLOCK=n

    # Enable the UART driver
    #CONFIG_UART_ASYNC_API=y
    #CONFIG_NRFX_UARTE0=y
    #CONFIG_NRFX_UARTE1=y
    #CONFIG_SERIAL=y
    #CONFIG_CONSOLE=y

    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_NANO=n

    Thank you for your consideration.

Reply
  • What I want to do is a sprintf function unix_time expand the value of the int64_t to the string array.

    prj.conf:

    #
    # Copyright (c) 2019 Nordic Semiconductor
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #

    CONFIG_BT=y
    CONFIG_BT_CENTRAL=y
    CONFIG_BT_PERIPHERAL=y
    CONFIG_BT_DEVICE_NAME="Nordic Discovery Sample"

    CONFIG_BT_SCAN=y
    CONFIG_BT_SCAN_FILTER_ENABLE=y
    CONFIG_BT_SCAN_UUID_CNT=1

    CONFIG_BT_SMP=y
    CONFIG_BT_GATT_CLIENT=y
    CONFIG_BT_GATT_DM=y
    CONFIG_HEAP_MEM_POOL_SIZE=2048

    # Needed to print UUID.
    CONFIG_BT_DEBUG_LOG=y
    #CONFIG_BT_GATT_DM_DATA_PRINT=y

    CONFIG_USE_SEGGER_RTT=y
    CONFIG_UART_CONSOLE=n
    CONFIG_RTT_CONSOLE=y
    CONFIG_LOG_PRINTK=y

    # Config logger
    CONFIG_LOG=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=n

    CONFIG_BT_LL_SOFTDEVICE_DEFAULT=y
    CONFIG_BT_CTLR=y

    #CONFIG_BT_SETTINGS=y
    CONFIG_SETTINGS=y

    CONFIG_BT_OBSERVER=y
    CONFIG_BT_BACKGROUND_SCAN_INTERVAL=2048
    CONFIG_BT_BACKGROUND_SCAN_WINDOW=18

    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_NVS=y
    CONFIG_NVS_LOG_LEVEL_DBG=y
    CONFIG_REBOOT=y
    CONFIG_MPU_ALLOW_FLASH_WRITE=y

    CONFIG_NRFX_CLOCK=n

    # Enable the UART driver
    #CONFIG_UART_ASYNC_API=y
    #CONFIG_NRFX_UARTE0=y
    #CONFIG_NRFX_UARTE1=y
    #CONFIG_SERIAL=y
    #CONFIG_CONSOLE=y

    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_NANO=n

    Thank you for your consideration.

Children
Related