I’m trying ti make sprintf work in Sloeber with float.
I wrote the following sketch to test it:
#include "Arduino.h"
void setup()
{
Serial.begin(115200);
delay(5000);
}
void loop()
{
static float temp = 10000;
uint8_t i;
char buffer[100];
for (i=1;i<13;i++) {
Serial.print("Before ");
Serial.println(i);
//sprintf (buffer,"Value = %f",temp);
ftoa(temp, buffer);
Serial.print("After ");
temp=temp/3;
Serial.println(buffer);
}
while(1);
}
Why not use the String’s conversion functions:
#include "Arduino.h"
void setup()
{
Serial.begin(115200);
delay(5000);
}
void loop()
{
static float temp = 10000;
uint8_t i;
String buffer;
for (i=1;i<13;i++) {
Serial.print("Before ");
Serial.println(i);
buffer = temp; // convert a float using the String's conversion routines
Serial.print("After ");
temp=temp/3;
Serial.println(buffer);
}
while(1);
}
Or perhaps I’m thinking of problems I had on AVR
Also it isn’t cstring it is actually String .. which is an arduino specific class. In the avr versions they have their own float conversion code which doesn’t use sprintf .. it uses a function in avr libc
http://www.atmel.com/webdoc/avrlibcrefe … 71d42.html
… which eventually calls:
https://github.com/vancegroup-mirrors/a … dtoa_prf.c
Its probably faster for the processor than calling sprintf etc if you just want to print a float at fixed precision.
1. If you want to print a float, just use the print class, it includes a funtion to print floats:
Serial.print(float, num_digits));Before 1
After Value = 10000.000000
Before 2
After Value = 3333.333252
Before 3
After Value = 1111.111084
Before 4
After Value = 370.370361
Before 5
After Value = 123.456787
Before 6
After Value = 41.152264
Before 7
After Value = 13.717422
Before 8
After Value = 4.572474
Before 9
After Value = 1.524158
Before 10
After Value = 0.508053
Before 11
After Value = 0.169351
Before 12
After Value = 0.056450The code I’m using is what Pito tested, but in my case it doesn’t work.
I’m using Rogers core (bluepill).
If I use Arduino IDE, the skecth works correctly, but under Sloeber it doesn’t work.
@victor_pv:
2) You are right: static declaration is not necessary.
3) I made a little bit of confusion. As I said before, the code I’m using is exactly what Pito tested.
4) I found sprintf light implementation, but that implementation doesn’t support float.
@Pito: have you added some personal directives in Sloeber that doesn’t come with standard distribution? For example, I noticed that you have modified platform.txt to make new() function work viewtopic.php?f=41&t=2434&start=80
Luca
name=STM32 Boards (STM32duino.com)
version=0.1.2
compiler.warning_flags=-w -DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.none=-w -DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.default=-DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.more=-Wall -DDEBUG_LEVEL=DEBUG_FAULT
compiler.warning_flags.all=-Wall -Wextra -DDEBUG_LEVEL=DEBUG_ALL
compiler.combine.flags=-u _sbrk -u link -u _close -u _fstat -u _isatty -u _lseek -u _read -u _write -u _exit -u kill -u _getpid
# compiler variables
..
Hi Pito. Please, be patient vith me: I’m not very good with eclipse
Starting combiner
"/home/luca/sloeber//arduinoPlugin/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -Og -Wl,--gc-sections -mcpu=cortex-m3 "-T/home/luca/arduino/hardware/Arduino_STM32-master/STM32F1/variants/generic_stm32f103c/ld/jtag_c8.ld" "-Wl,-Map,/home/luca/Documenti/sloeber-workspace/Sprintf_Test/Release/Sprintf_Test.map" "-L/home/luca/arduino/hardware/Arduino_STM32-master/STM32F1/variants/generic_stm32f103c/ld" -o "/home/luca/Documenti/sloeber-workspace/Sprintf_Test/Release/Sprintf_Test.elf" "-L/home/luca/Documenti/sloeber-workspace/Sprintf_Test/Release" -lm -lgcc -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group ./sloeber.ino.cpp.o /home/luca/Documenti/sloeber-workspace/Sprintf_Test/Release/arduino.ar -Wl,--end-group
/home/luca/sloeber/arduinoPlugin/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/../lib/gcc/arm-none-eabi/4.8.3/../../../../arm-none-eabi/lib/armv7-m/libnosys.a(sbrk.o): In function `_sbrk':
sbrk.c:(.text._sbrk+0x30): warning: undefined reference to `end'
Finished building: Sprintf_Test.elf
## Combine gc-sections, archives, and objects
recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mcpu={build.mcu} "-T{build.variant.path}/{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" "-L{build.path}" -lm -lgcc -mthumb -Wl,--cref -Wl,--check-sections -Wl,--gc-sections -Wl,--unresolved-symbols=report-all -Wl,--warn-common -Wl,--warn-section-align -Wl,--warn-unresolved-symbols -Wl,--start-group {compiler.combine.flags} {object_files} "{build.path}/{archive_file}" -Wl,--end-groupThanks for suggestions.
In file platform.txt that is under /home/luca/arduino/hardware/Arduino_STM32-master/STM32F1/platform.txt I added:
name=STM32 Boards (STM32duino.com)
version=0.1.2
compiler.warning_flags=-w -DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.none=-w -DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.default=-DDEBUG_LEVEL=DEBUG_NONE
compiler.warning_flags.more=-Wall -DDEBUG_LEVEL=DEBUG_FAULT
compiler.warning_flags.all=-Wall -Wextra -DDEBUG_LEVEL=DEBUG_ALL
compiler.combine.flags=-u _sbrk -u link -u _close -u _fstat -u _isatty -u _lseek -u _read -u _write -u _exit -u kill -u _getpid
Not sure if it reloads them by just closing and opening, I believe it doesn’t.
I use sloeber 4.1 in a windows 10 and 4.0 in Win 8.1, both work fine.
I think the linker file is not the right one. Instead of jtag_c8.ld it should be bootloader_20.ld.
I followed your instructions and now added directives are loaded correclty and sketch works as aspected also under Sloeber
@Steve
I use STLink programmer. I think jtag_c8.ld in this case is correct.
@Victor, Steve, Pito and Roger
Thanks again for your support
@Roger
Could be possible adding this directives in platform.txt by default? I tested this sketch with modified platform.txt and Arduino IDE and works, but I don’t know if the added directives have some sort of collateral effect.
I belive I’m not the first one asking you this thing. I also know that under Arduino IDE STM32 core works correctly and core STM32 is intended for Arduino IDE.
Otherwise, is there another method to make it be “compatible” with eclipse IDE?
What I’m trying to tell is that if someone want to use STM32 core from repository and use Eclipse, functions like new(), sprintf… doesn’t work and they don’t know why.
Thanks again and compliments again for your work.
Regards.
Luca.
In Sloeber, in order to load new properties and settings, you have to go:
Project -> Properties -> Arduino -> Apply -> OK.


