This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/66AK2G12: Printing Floating point numbers using UART_printf()

Part Number: 66AK2G12
Other Parts Discussed in Thread: 66AK2H14

Tool/software: Code Composer Studio

Hi Team,

I am using the 66AK2G12 processor and running TI-RTOS in the ARM core.

As a part of requirement I am using float variable to hold a floating point numeral.

The floating point operations are working good on the processor without having any issues. I tried to compare two fractional numbers in an IF condition and no issue was reported. 
But when I try to print the value using UART_printf(), I could find "ERROR" getting displayed in place of the fractional number.

I even tried to use sprintf() to store the floating point number number as a string and then tried to print that string using UART_printf(). Still the number is not getting displayed.

I even tried to pass the hard-coded floating value into sprintf as shown below.

char val[20];
sprintf(val,"\nTEMP VALUE = %f",(1.8765));
UART_printf("\n%s",val);

But I am getting the following output.

TEMP VALUE = 

The numbers are not getting displayed.

So kindly let me know how I can print the fractional numbers via UART in CCS for the 66AK2G12 processor. 
Thank You.

With Regards,
Krishna.

  • This is currently not supported by default by UART_printf. You can convert float or integer to ascii value and print the value. Example of this is provided in AudioEq.c file located in pdk_k2g_1_0_15\packages\ti\drv\mcasp\example\src

    Look for itoa and atoi support functions used with UART_printf

    Hope this helps

    Regards,

    Rahul

  • itoa() won't help much since it takes an integer, not a float. Unless you are suggesting they write their own "ftoa()" based on the itoa() code.

  • Krishna Boopathy J said:
    I even tried to pass the hard-coded floating value into sprintf as shown below.
    1
    2
    3
    char val[20];
    sprintf(val,"\nTEMP VALUE = %f",(1.8765));
    UART_printf("\n%s",val);

    The val string only has 20 bytes, and the %f format specifier writes 6 decimal places. I.e. the val string isn't long enough leading to sprintf writing off the end of the val array possibly corrupting other variables on the stack.

    I tried that code with the gcc-arm-none-eabi-7-2018-q2-update compiler, and noticed that compiler actually produces a warning about this:

    ../main.c:10:33: warning: '%f' directive writing 8 bytes into a region of size 6 [-Wformat-overflow=]
         sprintf(val,"\nTEMP VALUE = %f",(1.8765));
                                     ^~
    ../main.c:10:5: note: 'sprintf' output 23 bytes into a destination of size 20
         sprintf(val,"\nTEMP VALUE = %f",(1.8765));
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

    Trying increasing the length of the val array to allow sufficient space.

  • Hi Chester,

    Chester Gillon said:

    The val string only has 20 bytes, and the %f format specifier writes 6 decimal places. I.e. the val string isn't long enough leading to sprintf writing off the end of the val array possibly corrupting other variables on the stack.

    Trying increasing the length of the val array to allow sufficient space.

    While testing, I just printed one word followed by numbers. So [20] was sufficient. 

    I just added "TEMP VALUE" just for the aesthetics while posting the code in forum here. So I overlooked to update the array length value. my bad. 

    So even with an array length of 100, I can print only the text and the numbers are not getting printed.


    And I checked the code suggested by Rahul. And the itoa() and i2a() functions are useful in case of integer numbers. But I need to print fractional number with a decimal point. 

    So will attempt to write an ftoa() function as suggested by Keith. Meanwhile do let me know of any other workarounds. I am open for suggestions.

    With Thanks,

    Krishna

  • Krishna Boopathy J said:
    So even with an array length of 100, I can print only the text and the numbers are not getting printed.

    Thanks for the explanation.

    Which compiler are you using?

    You might need to enable support for floating point in the printf family of functions.

  • Hi,

    I am currently using the "GNU v7.2.1 (Linaro)" compiler for the ARM core of the K2G. 

    Using TI-RTOS[ti-processor-sdk-rtos-k2g-evm-06.01.00.08]  on CCS 9.3.

    I looked at project config files and UART_printf libraries. Couldn't find any option to enable floating point support.

    But floating point calculations and working fine without any issues in the code. So issue seems to be prevailing only during print scenarios. So need to check if there is any option to enable floating point prints in sprintf(). 


    Because for UART_printf() it was explicitly mentioned in the libraries that it wont support %f.

    Regards,

    Krishna

  • Krishna Boopathy J said:
    am currently using the "GNU v7.2.1 (Linaro)" compiler for the ARM core of the K2G. 

    Using TI-RTOS[ti-processor-sdk-rtos-k2g-evm-06.01.00.08]  on CCS 9.3.

    I don't have a 66AK2G12, but investigated using a 66AK2H14 using ti-processor-sdk-rtos-k2hk-evm-06.01.00.08.

    Imported the UART_BasicExample_K2H_armExampleProject example and added the following code:

        char val[40];
        sprintf(val,"\nTEMP VALUE = %f",(1.8765));
        UART_printf("\n%s",val);

    When the code was run, sprintf() generated an empty string for the "%f" floating point format.

    Krishna Boopathy J said:
    I looked at project config files and UART_printf libraries. Couldn't find any option to enable floating point support.

    TI-RTOS links the Newlib-nano library, by using "--specs=nano.specs" in the linker options.

    README.nano explains that programs that require formatted floating-point input/output must explicitly reference the relevant support function during linking. For the printf() family of routines the support function is "_printf_float".

    For a TI-RTOS project this can be done by adding _printf_float as an undefined symbol in the linker options for the project:

    With this change sprintf then generated a float point output:

  • Hi Chester,

    Sorry for the delay in reply. 

    I have tested your suggestion and it works good. 

    Thank you.

    -Krishna.