sprintf floating point support NCS

I cannot seem to get floating point supoort for printf/scranf 

It works for LOG_INF

    sprintf(str,"nopad: %f pad %2.1f\r\n", f,f );
    printk(str);

    printf("nopad: %f pad %2.1f\r\n", f,f);

    LOG_INF("Testing padded float %2.1f",f);

nopad: 0.000000 pad -nan

nopad: 0.000000 pad -nan

[00:00:01.373,535] <inf> display: Testing padded float 3.4

I have tried the following .conf settings:

CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_CBPRINTF_COMPLETE=y
Parents
  • Hi,

     

    Your printk should be printk("%s", str).

     

    I took hello_world, added these to prj.conf:

    CONFIG_CBPRINTF_COMPLETE=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_CBPRINTF_FP_SUPPORT=y
    

     

    Added your functions:

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <math.h>
    #include <stdio.h>
    
    void main(void)
    {
    	printk("Hello World! %s\n", CONFIG_BOARD);
    	float f = 0.1234f;
    	char str[100];
    	sprintf(str,"nopad: %f pad %2.1f\r\n", f,f );
    	printk("sprintf: %s\n",str);
    
    	printf("nopad: %f pad %2.1f\r\n", f,f);
    
    }

     

    And here's the output:

    *** Booting Zephyr OS build v3.1.99-ncs1  ***
    Hello World! nrf5340dk_nrf5340_cpuapp
    sprintf: nopad: 0.123400 pad 0.1
    
    nopad: 0.123400 pad 0.1
    

     

    Which ncs version are you running on? I am currently on ncs v2.1.0

     

    Kind regards,

    Håkon

  • It must have been some build issue or overlay conflict because it is working now.

Reply Children
No Data
Related