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

Development environment specific, Arduino, Eclipse, VS2013,Em::Blocks etc
luca_stm32
Posts: 23
Joined: Wed Apr 26, 2017 12:39 pm

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

Post by luca_stm32 » Thu Dec 07, 2017 8:23 pm

Hi everybody.
I'm trying ti make sprintf work in Sloeber with float.
I wrote the following sketch to test it:

Code: Select all

#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);
}
The output on terminal is: Before 1
and then nothing.
I tried to debug project, but code crashes (loop in __stm32reservedexception9:
08000c20: b.n 0x8000c20 <__stm32reservedexception9>)

I tied the same code on Arduino IDE and it is OK!

Can you help me?

I'm using Sloeber ver 4.2.

Thanks.
Regards.
Luca
Last edited by luca_stm32 on Sat Dec 09, 2017 6:05 pm, edited 1 time in total.

User avatar
Rick Kimball
Posts: 1078
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Sloeber and float sprintf: doesn't work

Post by Rick Kimball » 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:

Code: Select all

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

User avatar
RogerClark
Posts: 7691
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Sloeber and float sprintf: doesn't work

Post by RogerClark » 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 AVR

User avatar
Rick Kimball
Posts: 1078
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Sloeber and float sprintf: doesn't work

Post by Rick Kimball » 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 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
-rick

User avatar
RogerClark
Posts: 7691
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Sloeber and float sprintf: doesn't work

Post by RogerClark » 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.

victor_pv
Posts: 1869
Joined: Mon Apr 27, 2015 12:12 pm

Re: Sloeber and float sprintf: doesn't work

Post by victor_pv » 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:

Code: Select all

Serial.print(float, num_digits));
2. Why are you declaring temp as static, is there any specific reason for that?

3. If you want to use ftoa, I believe it needs 3 arguments, at least the implementation I see in my helpers.h file, and I was using it successfully in a sketch last week:

Code: Select all

void ftoa(char *buf, float val, int places)
Since I dont see ftoa in the core, you must be using your own version, hard to say why it fails without know what's in the function.

4. If you want to use sprintf, I don't know if it has been added to the core now, but I remember it wasn't a while back. Back then I used the version someone posted in a thread in the forum and worked pretty well. If nothing has been added to the core and you need to pull sprintf from somewhere, I recommend that version, was light in flash and ram usage, and did work.

User avatar
Pito
Posts: 1743
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Sloeber and float sprintf: doesn't work

Post by Pito » Fri Dec 08, 2017 12:06 am

Works fine under Sloeber and Roger's core (BluePill):

Code: Select all

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

Code: Select all

#include "Arduino.h"
void setup()
{
  Serial.begin(115200);
  delay(5000);
}
void loop()
{
  static float temp = 10000.0;
  uint8_t i;
  char buffer[100];
  for (i=1;i<13;i++) {
    Serial.print("Before ");
    Serial.println(i);
    sprintf (buffer,"Value = %f",temp);
    Serial.print("After ");
    temp=temp/3.0;
    Serial.println(buffer);
  }
  while(1);
}
With Daniel's core under Sloeber:

Code: Select all

Before 1
After Value =
Before 2
After Value =
Before 3
After Value =
Before 4
After Value =
Before 5
After Value =
Before 6
After Value =
Before 7
After Value =
Before 8
After Value =
Before 9
After Value =
Before 10
After Value =
Before 11
After Value =
Before 12
After Value =
Pukao Hats Cleaning Services Ltd.

luca_stm32
Posts: 23
Joined: Wed Apr 26, 2017 12:39 pm

Re: Sloeber and float sprintf: doesn't work

Post by luca_stm32 » 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

User avatar
Pito
Posts: 1743
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: Sloeber and float sprintf: doesn't work

Post by Pito » Fri Dec 08, 2017 8:28 am

@Pito: have you added some personal directives in Sloeber that doesn't come with standard distribution?

Code: Select all

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
..
Pukao Hats Cleaning Services Ltd.

luca_stm32
Posts: 23
Joined: Wed Apr 26, 2017 12:39 pm

Re: Sloeber and float sprintf: doesn't work

Post by luca_stm32 » 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_STM32-master/STM32F1/platform.txt, restarted sloeber, cleaned project, rebuilted project but I can see the added flags in output console, and the sletch still not work.

Code: Select all

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
Thanks again.
Regards.
Luca

Post Reply