How to use time.h and math.h in nrf connect sdk?

1)I want to use "mktime" and "localtime" in my nrf52840 code ,how do i include time.h file?


2)I am using math.h file , in prj.conf i define "CONFIG_NEWLIB_LIBC=y"  but I continuously  get error as below ,how do i resolve it?

error:

/usr/include/math.h:415:33: error: '_Float128' is not supported on this target
415 | # define _Mdouble_ _Float128
  • Hello Krumita,

    Thank you for contacting DevZone at NordicSemi.

    I am not sure if we are on the same page or not, but here is what I did:

    I started with hello-world sample for the nrf52840dk in the ncs v2.4.0.

    I simply added:

    #include <time.h>
    #include <math.h>
    

    and then I was able to use the functions defined within those headers.

    To print the floating point numbers, I had to add following in the prj.config:

    CONFIG_SFLOAT=y
    CONFIG_CBPRINTF_FP_SUPPORT=y

    I can also see there are other Kconfig options available related to the DSP and Floating points

    (for example: CONFIG_FPU to enable and use the floating point unit)

    Below you can see the code I have used and the output on 52840dk

    BR,

    Naeem

  • 1)I want to use "mktime" and "localtime" in my nrf52840 code ,how do i include time.h file?

    and i get 

    error:

    /usr/include/math.h:415:33: error: '_Float128' is not supported on this target
    415 | # define _Mdouble_ _Float128
    error after adding math.h , what is reason for it.
  • This is exact same query you have started in your original post.

    I have responded to that that I can add time.h and math.h without any issue and use their functions. 

    I have explained all steps in detail that how I did that starting from the name of the project and noting smaller changes like the project configs etc.

    1) Can you replicate the steps which I have mentioned and see the output?

    2) Kindly let let me know all the steps in detail to replicate the problem you are having

    BR

  • yes i have did it, but i found time.h from nrf connect sdk which does not have 'mktime' and 'localtime' function.I want to use time.h from "usr/include/time.h" which has this function.

    I have attache my cmakefile and prj.conf, 

    and after adding math.h i am getting error as above i mention

    #
    # Copyright (c) 2021 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
    #
    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    
    
    # add_subdirectory(common)
    # add_subdirectory_ifdef(CONFIG_BT_HCI host)
    # add_subdirectory_ifdef(CONFIG_BT_SHELL shell)
    # add_subdirectory_ifdef(CONFIG_BT_CONN services)
    # add_subdirectory_ifdef(CONFIG_BT_MESH mesh)
    # add_subdirectory_ifdef(CONFIG_BT_AUDIO audio)
    # add_subdirectory_ifdef(CONFIG_BT_CRYPTO crypto)
    
    # if(CONFIG_BT_CTLR AND CONFIG_BT_LL_SW_SPLIT)
    #   add_subdirectory(controller)
    # endif()
    
    zephyr_include_directories(${ZEPHYR_BASE}/subsys/bluetooth)
    # zephyr_include_directories(${ZEPHYR_BASE}
    
    # Target name
    project(repeater)
    
    set(APP_FIRMWARE_VERSION 0x1BD)
    set(CLIENT CLIENT_ILUMI)
    set(BOOTLOADER_VERSION  0x106)
    
    # Project root directory
    set(PROJ_DIR ../..)
    
    set(THIRD_PARTY ../../third_party)
    
    # set(CMAKE_C_STANDARD 11)
    # set(CMAKE_CXX_STANDARD 11)
    
    set(RTC_SYNC_V2 0)
    
    set(C_SRC
    
        ${PROJ_DIR}/app/src/main.c
        ${PROJ_DIR}/app/src/ilumi_pa_lna_assist.c
        ${PROJ_DIR}/app/src/mock_placeholder.c
        ${PROJ_DIR}/app/src/RTC.c
        ${PROJ_DIR}/app/src/i2c_nrf52.c
        ${PROJ_DIR}/app/src/timeout.c
        ${PROJ_DIR}/app/src/connect_notify.c
        ${PROJ_DIR}/app/src/rtc_func.c
        ${PROJ_DIR}/app/src/adv_timer.c
        ${PROJ_DIR}/app/src/internal_flash.c
        ${PROJ_DIR}/app/src/ilumi_app_timer.c
        ${PROJ_DIR}/app/src/ilumi_ble_app.c
        ${PROJ_DIR}/app/src/hal_uart.c
        ${PROJ_DIR}/app/src/ilumi_softdevice_handler_sdkv13.c
        ${PROJ_DIR}/app/src/uart.c
        ${PROJ_DIR}/app/src/uart_logf.c
        ${PROJ_DIR}/app/src/ilumi_api_type_define.c
        ${PROJ_DIR}/app/src/scene.c
        ${PROJ_DIR}/app/src/scene_predefine.c
        ${PROJ_DIR}/app/src/log.c
        ${PROJ_DIR}/app/src/OSAL_Nv.c
        ${PROJ_DIR}/app/src/ilumi_nv_data.c
        ${PROJ_DIR}/app/src/ilumi_user_data.c
        ${PROJ_DIR}/app/src/ilumi_recv_msg.c
        ${PROJ_DIR}/app/src/ilumi_common_msg.c
        ${PROJ_DIR}/app/src/ilumi_temp_compensation.c
        ${PROJ_DIR}/app/src/ilumi_circadian.c
        ${PROJ_DIR}/app/src/ilumi_api.c
        ${PROJ_DIR}/app/src/ble_stack_util.c
        ${PROJ_DIR}/app/src/user_interface.c
        ${PROJ_DIR}/app/src/ilumi_utility.c
        ${PROJ_DIR}/app/src/ilumi_timer_def.c
        ${PROJ_DIR}/app/src/ilumi_api_service.c
        ${PROJ_DIR}/app/src/ilumi_task_dispatch.c
        ${PROJ_DIR}/app/src/ilumi_user_action.c
        ${PROJ_DIR}/app/src/ilumi_uart_cmd.c
        ${PROJ_DIR}/app/src/ilumi_properties.c
        ${PROJ_DIR}/app/src/ilumi_mesh.c
        ${PROJ_DIR}/app/src/ilumi_mesh_config.c
        ${PROJ_DIR}/app/src/ilumi_special_effect.c
        ${PROJ_DIR}/app/src/ilumi_app_button_sdk13.c
        ${PROJ_DIR}/app/src/alarm_list.c
        ${PROJ_DIR}/app/src/alarm_manager.c
        ${PROJ_DIR}/app/src/color_util.c
        ${PROJ_DIR}/app/src/rtc_util.c
        ${PROJ_DIR}/app/src/time_util.c
        ${PROJ_DIR}/app/src/company.c
        ${PROJ_DIR}/app/src/ilumi_dfu_func.c
        ${PROJ_DIR}/app/src/ilumi_dfu_service.c
        # ${PROJ_DIR}/app/src/ilumi_queue.c
        # Source files from booloader section
        ${PROJ_DIR}/bootloader/src/bootloader_util_gcc.c
    
        ${THIRD_PARTY}/nrf_drv_gpiote.c
        ${THIRD_PARTY}/crc16.c
        ${THIRD_PARTY}/app_error.c
        ${THIRD_PARTY}/nrf_assert.c
        ${THIRD_PARTY}/app_util_platform.c
        ${THIRD_PARTY}/app_scheduler.c
        ${THIRD_PARTY}/nrf_drv_ppi.c
        ${THIRD_PARTY}/nrf_drv_pwm.c
        ${THIRD_PARTY}/nrf_drv_clock.c
        ${THIRD_PARTY}/nrf_drv_common.c
        ${THIRD_PARTY}/ble_flash.c
        # ${THIRD_PARTY}/nrf_drv_twis.c
        ${THIRD_PARTY}/nrf_drv_twi.c
        ${THIRD_PARTY}/nrf_drv_spi.c
        ${THIRD_PARTY}/ble_srv_common.c
        ${THIRD_PARTY}/ble_dis.c
        ${THIRD_PARTY}/ble_conn_params.c
        ${THIRD_PARTY}/softdevice_handler_appsh.c
        # ${THIRD_PARTY}system_nrf52.c
        
    )
    
    # C++ source files
    set(CPP_SRC
        ${PROJ_DIR}/app/src/RouteCore.cpp
        ${PROJ_DIR}/app/src/routing_api.cpp
    )
    set(INCLUDE_DIR
        # /usr/include/x86_64-linux-gnu/bits
        ${PROJ_DIR}/app/repeater
        ${PROJ_DIR}/app/inc
        ${PROJ_DIR}/bootloader/repeater
        ${PROJ_DIR}/bootloader/inc
        ${THIRD_PARTY}
        /usr/include
    )
    
    # Header files from NRF Connect SDK
    
    # Add source files to the target
    target_sources(app PRIVATE
        ${C_SRC}
        ${CPP_SRC}
    )
    
    target_include_directories(app PRIVATE
        ${INCLUDE_DIR}
    )
    
    # target_compile_options(app PRIVATE
    #     -Wno-unused-function
    #     -Wno-unused-variable
    #     -Wno-unused-but-set-variable
    #     -mfloat-abi=hard
    #     -mfpu=fpv4-sp-d16
    #     -mcpu=cortex-m4
    #     -mthumb
    #     -mabi=aapcs
    #     -DNRF52
    #     -Wall
    #     # -Werror
    #     -fno-exceptions
    #     -fdata-sections
    #     -ffunction-sections
    #     -g
    #     -Os
    #     -std=gnu99
    # )
    
    
    # target_link_options(app PRIVATE
    #     -mcpu=cortex-m4
    #     -mthumb
    #     -mabi=aapcs
    #     -specs=nano.specs
    #     -specs=nano.specs
    #     -Xlinker --cref
    #     -Xlinker --gc-sections
    #     -Xlinker --undefined=m_boot_settings
    # )
    
    add_definitions(-DREPEATER)
    add_definitions(-DSDK_V13)
    add_definitions(-DSDK_V11)
    add_definitions(-DBLE_STACK_SUPPORT_REQD)
    add_definitions(-D${CLIENT})
    add_definitions(-DAPP_FIRMWARE_VERSION=${APP_FIRMWARE_VERSION})
    add_definitions(-DENABLE_RTC)
    add_definitions(-DUSER_DEFINED_COLOR)
    add_definitions(-DDEBUG)
    add_definitions(-DFDIN_SUPPORT)
    add_definitions(-DNRF52)
    add_definitions(-DNORDIC_PLATFORM)
    add_definitions(-DNRF52840_XXAA)
    # Set the ASM flags globally for all targets
    add_compile_options(-DNRF_SD_BLE_API_VERSION=5)
    
    2117.prj.conf

  • Hello,

    I am not sure whether we are on the same page or not.

    I do not find any such folder /usr/include in the complete NCS folder.

    If that is custom implementation, then it makes sense.

    For such a case, I have created a custom library, it has a header file and a source file, very simple one just to show the functionality.

    The library I called neolib is present at c:/ncs/third_party/

    neolib has neolib.h and neolib.c files

    Below you can see I am using the header file (neolib.h) in my project

    I am using the function defined in it, and I can see everything is working good.

    BR, 
    Naeem

Related