Note: this is a "howto" response rather than a genuine question, given because it may be helpful to others.
Disclaimer: The information here has been cobbled together from several sources and experimentation. It works for my environment (KDS 3.2.0 running under Mac OSX 10.12.2, Cross ARM C compiler), but details may be different for your environment.
Description of the Problem: printf("%f", 2.0) doesn't print anything.
Reason: The Cross ARM C compiler (and linker) do not provide support for printing and scanning floating point numbers by default.
Solution: You can add support to the Cross ARM C compiler for printing and scanning floating point numbers (and doubles) as follows.
- In KDS, under the Project menu item, click on Properties
- Expand the C/C++ Build item in the left hand column
- Click on C/C++ Build Settings in the left hand column
- Click on [Tool Settings] tab in the right hand panel
- Expand the Cross Arm C Compiler item
- Click on Cross Arm C Compiler / Preprocessor
- In the Defined Symbols (-D) panel, click on the + icon
- In the dialog panel that appears, type "PRINTF_FLOAT_ENABLE=1" and [OKAY]
- Click on the + icon again
- In the dialog panel that appears, type "SCANF_FLOAT_ENABLE=1" and [OKAY]
- Click [Apply]
- Expand the Cross Arm C++ Linker item
- Click on Cross Arm C++ Linker / Miscellaneous
- In the right hand panel, enable the two check boxes labeled "Use float with nano printf (-u _printf_float)" and "Use float with nano scanf (-u _scanf_float)".
- Click [Apply]
- Click [OK]
You can test that printf() works with floats by creating a project based on the "Hello World" demo application and modifying it as follows:
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"
int main(void) {
char ch;
double val;
BOARD_InitPins();
BOARD_BootClockRUN();
BOARD_InitDebugConsole();
val = 0.01;
for (int i=0; i<5; i++) {
PRINTF("val = %f\n", val);
val *= 10.0;
}
while (1) {
ch = GETCHAR();
PUTCHAR(ch);
}
}
If all goes well, you should see this printed on the console:
val = 0.010000
val = 0.100000
val = 1.000000
val = 10.000000
val = 100.000000