Spark Logic

[SOLVED] Sloeber and float sprintf: doesn’t work

Hi everybody.
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);
}

Replies15

Thu Dec 07, 2017 9:39 pm

what is ftoa? That isn’t standard. Where did you get that?

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);
}

Thu Dec 07, 2017 9:49 pm

I thought there were general issues with float to cstring etc, because only part of the full printf / sprintf lib gets implemented otherwise it is too big to fit on most small MCU’s

Or perhaps I’m thinking of problems I had on AVRAVRMicrocontrollers and processors…Arduino\libraries\U8glib\U8glib.cpp, C:\Program Files (x86)\Arduino\hardware\arduino\ avr \cores\arduino\HardwareSerial.h, C:\Program Files (x86)\Arduino/Blink3.ino, D:\Users\me\A…View in hardware catalog

Thu Dec 07, 2017 10:05 pm

You are right Roger. I looked at the string conversion but I didn’t look at the dtostrf() function it calls which in turns calls sprintf… sorry nm

Also it isn’t cstring it is actually String .. which is an arduino specific class. In the AVRAVRMicrocontrollers and processors…Arduino\libraries\U8glib\U8glib.cpp, C:\Program Files (x86)\Arduino\hardware\arduino\ avr \cores\arduino\HardwareSerial.h, C:\Program Files (x86)\Arduino/Blink3.ino, D:\Users\me\A…View in hardware catalog versions they have their own float conversion code which doesn’t use sprintf .. it uses a function in AVRAVRMicrocontrollers and processors…Arduino\libraries\U8glib\U8glib.cpp, C:\Program Files (x86)\Arduino\hardware\arduino\ avr \cores\arduino\HardwareSerial.h, C:\Program Files (x86)\Arduino/Blink3.ino, D:\Users\me\A…View in hardware catalog libc

http://www.atmel.com/webdoc/avrlibcrefe … 71d42.html

… which eventually calls:
https://github.com/vancegroup-mirrors/a … dtoa_prf.c

Thu Dec 07, 2017 11:41 pm

I normally end up printing fixed precision floats using a function e.g. take the integer part and then the float part and multiply the float part by the 10 to the number of digits required etc etc

Its probably faster for the processor than calling sprintf etc if you just want to print a float at fixed precision.

Thu Dec 07, 2017 11:56 pm

Not sure what you are trying to achieve in the end, since the sample code looks like an example of something wrong with ftoa not sprintf, but here are my 2 cents:
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));

Fri Dec 08, 2017 12:06 am

Works fine under Sloeber and Roger’s core (BluePill):
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.056450

Fri Dec 08, 2017 6:37 am

The code I tested is without ftoa. ftoa was a function I implemented to try to make the sketch work.
The 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

Fri Dec 08, 2017 8:28 am

@Pito: have you added some personal directives in Sloeber that doesn’t come with standard distribution?
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
..

Fri Dec 08, 2017 9:01 am

@Pito.
Hi Pito. Please, be patient vith me: I’m not very good with eclipse :oops: How can I correctly insert this directives in Sloeber? I modified platform.txt under /home/luca/arduino/hardware/Arduino_STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog -master/STM32F1STM32F1Microcontrollers and processors…w I am not able to compile. Error message: “C:\arduino-1.6.12\hardware\Arduino_STM32\ STM32F1 \cores\maple/boards.h:39:25: fatal error: board/board.h: No such file or directory” I…View in hardware catalog /platform.txt, restarted sloeber, cleaned project, rebuilted project but I can see the added flags in output console, and the sletch still not work.
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

Fri Dec 08, 2017 12:15 pm

Combiner recipe in platform.txt (it adds the {compiler.combine.flags} into the build):
## 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-group

Fri Dec 08, 2017 7:25 pm

Hi Pito.
Thanks for suggestions.
In file platform.txt that is under /home/luca/arduino/hardware/Arduino_STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog -master/STM32F1STM32F1Microcontrollers and processors…w I am not able to compile. Error message: “C:\arduino-1.6.12\hardware\Arduino_STM32\ STM32F1 \cores\maple/boards.h:39:25: fatal error: board/board.h: No such file or directory” I…View in hardware catalog /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

Fri Dec 08, 2017 8:21 pm

In the project properties, change the board to something else, apply, and then back to the one you were using. That will reload all flags from platform.txt

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.

Fri Dec 08, 2017 8:46 pm

luca, which upload method do you use?
I think the linker file is not the right one. Instead of jtag_c8.ld it should be bootloader_20.ld.

Sat Dec 09, 2017 9:58 am

@Victor
I followed your instructions and now added directives are loaded correclty and sketch works as aspected also under Sloeber :D

@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 :D

@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 STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog core works correctly and core STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog 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 STM32STM32Microcontrollers and processors…Map magic glue code, ethernet compiles, just WARNING: library LwIP claims to run on [ stm32 ] architecture(s) and may be incompatible with your current board which runs on [STM32]…View in hardware catalog 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.

Sat Dec 09, 2017 10:44 am

Not sure if it reloads them by just closing and opening, I believe it doesn’t.
In Sloeber, in order to load new properties and settings, you have to go:

Project -> Properties -> Arduino -> Apply -> OK.

Post a reply

Optional · up to 10 MiB