Printing Floating Values using FX3 SDK – KBA231244
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:
- Add #include<sys/unistd.h> and #include<stdio.h> to the main firmware file
- 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.
- 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.
- Use the attached files only if 2-stage boot is not used by the application. For custom modification to the linker script file and the cyfxtx.cModifying the Default Memory Map in FX3, CX3, FX3S, SD3, FX2G2, and SD2 - KBA229114.
- After including the cyfxtx.c file, attached with this KBA, in the project, make sure that the project settings are modified, that is, the path to the new linker script file is added.
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.