Printing Floating Values using FX3 SDK – KBA231244

Version 4

    Version: **

    Translation - Japanese: FX3 SDKを使用した浮動小数点値のprint出力 – KBA231244 - Community Translated (JA)

    Question 1: Is it possible to print float or double variables using FX3 SDK?

    Answer: Yes, it is possible to print float variable using FX3 SDK. You can print using the standard C library function sprintf().

    The standard sprintf() function is used to store the formatted string into a buffer. The GCC linker script provided with the FX3 SDK (firmware/common/fx3.ld) does not initialize a runtime heap that is required for standard C library functions such as sprintf().

    If you are using the standard C library functions, implementation for standard system calls needs to be provided and the fx3.ld file needs to be modified to create a heap.

    See the BulkLoopAutoCpp example of the SDK (Path: SDK Installation path \Cypress\EZ-USB FX3 SDK\1.3\firmware\basic_examples) for a sample implementation of the required system calls, and the fx3cpp.ld file for a script sample with heap initialization.                             

    Note: C++ linker file (fx3cpp.ld) in the SDK can be used only for the C++ based projects like BulkLoopAutoCpp example of the SDK

    Question 2: Is it possible to print float or double variables in C based projects of FX3 SDK?

    Answer: Yes, it is possible to print float variable in C based projects of FX3 SDK. The float values can be printed using standard C library sprintf() function and the CyU3PDebugPrint API of the SDK. However, firmware needs to be modified before using the sprintf() function as runtime heap needs to be initialized.

    Do the following modifications:

    1. Add #include<sys/unistd.h> and #include<stdio.h> to the main firmware file
    2. The Sprintf() function internally calls the _sbrk system call. Add the definition of _sbrk, from the cyfxcppsyscall.cpp file (available in the BulkLoopAutoCpp project in the FX3 SDK), to the firmware.
    3. Modify fx3.ld and the cyfxtx.c file to allot the memory to the heap and initialize the heap. The modified fx3.ld and the cyfxtx.c files, for 512KB SRAM, are attached with this knowledge base article (KBA). The attached fx3.ld and cyfxtx.x files are modified to allot the 32 KB of SRAM, which is reserved for 2-stage boot operation in the default memory map, to the runtime heap required by the sprintf() function.

     

    Note:

     

    Figure 1. Modifications to Project Settings

      

     

    4. Now, use the sprintf() function in the firmware, and print floating numbers to UART using the CyU3PDebugPrint API.

     

    Here is a sample code to print floating values using the CyU3PDebugPrint API.

     

    Question 3: Is there any example code for printing floating values?

     

    Answer: The USBBukSourceSink example of the SDK, modified as per the steps mentioned in Question 2, is attached with this KBA.