SAMC21 - printf not printing float values

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SAMC21 Xplained Pro.  AS6.2  AFS3.27

Here is the code:

 

	c = 15.3;
	/*main oop*/
	while(1)
	{
	
		//c = 15.3;
		d = c/3;
		cntr = 10;
		printf("Integer cntr= %d\r\n",cntr);
		printf("Float      d= %f\r\n",d);  //PRINTF statement that doesn't print.
		delay_s(2);
		c=c+1.0;
		cntr++;
	}

here is the output:

 

This topic has a solution.
Last Edited: Sat. Oct 8, 2016 - 10:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does Arm have selectable printf library (like AVR)? For AVR at least there are 3 printf() libraries available, "minimal", "normal" and "float support". Only the latter shows %f output. Perhaps ARM is the same?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting.

How can I select the float support library? As a linker -lprintf_flt

 

Now how can I find the libprintf_flt.a for the M0 core?

 

In the Atmel files so far I have only found the library for AVR8.

 

 

Last Edited: Fri. Sep 18, 2015 - 11:18 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I was looking for this answer, so I thought I'd post an answer in case you are using current tools (AtmelStudio 7).

 

In the project properties, under Toolchain, open the ARM/GNU  Linker-->General options and DESELECT (uncheck)

the option "Use size optimized library". This will then increase your program size but will allow you to print

floats. If you are doing this for debug purposes, you can then check the option to once you don't need it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the answer.

 

but size jumps from 21K to 65K.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Found this on Atmel FAQ:

 

http://atmel.force.com/support/a...

 

Question

How to use printf with software floating point in the Atmel ARM GNU Toolchain in Atmel Studio project?

Answer

To add the software floating point support to the printf library in Atmel Studio 6 project, do the following:

  1. Open the project properties in Atmel Studio 6
  2. Go to Miscellaneous in Toolchain -> ARM/GNU Linker
  3. Add --specs=nano.specs -lc -u _printf_float in the linker flags
  4. Rebuild the project 

 

The software floating point support for printf library was not available with the earlier versions of Atmel ARM GNU Toolchain. Atmel ARM GNU Toolchain version 4.7.3.158 or later is required for using printf with software floating point support. To update the Toolchain, please download ARM Toolchain update from Extension Manager in Atmel Studio. To update the Atmel Studio (ARM GNU Toolchain update is a part of it) to the latest version, please download from www.atmel.com/tools/atmelstudio....

 

 

Only increase program size from 20K to 30K.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jstampfl wrote:
but size jumps from 21K to 65K.

Which should come as no surprise at all!

 

On a processor with no hardware floating point (such as a Cortex-M0) it does take a "lot" of code to do floating point - which is why it's often avoided in small micros.

 

So - do you really need floating point ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In this thread I was talking about the overhead of using "printf" for floats, not using floats.

 

Here are some tests on a D21 Xplained pro, AS 1188, ASF 3.32.

 

The code size difference is:

 

Integers only:

Program Memory Usage : 2264 bytes 0.9 % Full

Data Memory Usage : 9304 bytes 28.4 % Full

 

With float, but not using float printf.

Program Memory Usage : 2076 bytes 0.8 % Full

Data Memory Usage : 9304 bytes 28.4 % Full

 

With float, but not using float printf.

 

so the overhead of using software float is not great.

 


//test code for D21 Xplained using integers ASF 3.32
#include "sam.h"
#include "stdio.h"
#define SHIFT 32
volatile uint32_t ival,knt;
static uint64_t long_division(uint64_t n, uint64_t d)
{
	int32_t i;
	uint64_t q = 0, r = 0, bit_shift;
	for (i = 63; i >= 0; i--) {
		bit_shift = (uint64_t)1 << i;

		r = r << 1;

		if (n & bit_shift) {
			r |= 0x01;
		}

		if (r >= d) {
			r = r - d;
			q |= bit_shift;
		}
	}

	return q;
}

uint32_t bval(uint32_t peripheral_clock,uint32_t baudrate)
{
	/* Temporary variables  */
	uint64_t ratio = 0;
	uint64_t scale = 0;
	uint64_t baud_calculated = 0;
	uint64_t temp1;
	uint32_t sample_num = 16;

		/* Calculate the BAUD value */
	temp1 = ((sample_num * (uint64_t)baudrate) << SHIFT);
	ratio = long_division(temp1, peripheral_clock);
	scale = ((uint64_t)1 << SHIFT) - ratio;
	baud_calculated = (65536 * scale) >> SHIFT;
	return ((uint32_t) baud_calculated);

}

int main(void)
{
	uint32_t tval = 48000000UL;
	uint32_t tbaud = 9600UL;
    /* Initialize the SAM system */
    SystemInit();

    /* Replace with your application code */
    while (1)
    {

		ival = bval(tval,tbaud);
		knt++;
    }
}
	Task "RunOutputFileVerifyTask"
				Program Memory Usage 	:	2264 bytes   0.9 % Full
				Data Memory Usage 		:	9304 bytes   28.4 % Full
	Done executing task "RunOutputFileVerifyTask".

//test code #2 for D21 Xplained using Floats ASF 3.32
#include "sam.h"
#include "stdio.h"
#define SHIFT 32
volatile uint32_t ival,knt;

uint32_t bval(uint32_t peripheral_clock,uint32_t baudrate)
{
	/* Temporary variables  */

	uint32_t baud_calculated = 0;
	float temp1;
	uint32_t sample_num = 16;

		/* Calculate the BAUD value */
	temp1 = (((sample_num)*(float)baudrate)) / ((float)peripheral_clock);
	baud_calculated = (uint32_t) (65536*(1 - temp1));

	return ((uint32_t) baud_calculated);

}

int main(void)
{
	uint32_t tval = 48000000UL;
	uint32_t tbaud = 9600UL;
    /* Initialize the SAM system */
    SystemInit();

    /* Replace with your application code */
    while (1)
    {

		ival = bval(tval,tbaud);
		knt++;
    }
}

	Task "RunOutputFileVerifyTask"
			Program Memory Usage 	:	2076 bytes   0.8 % Full
			Data Memory Usage 		:	9304 bytes   28.4 % Full
	Done executing task "RunOutputFileVerifyTask".

 

Last Edited: Mon. Oct 10, 2016 - 02:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jstampfl wrote:
In this thread I was talking about the overhead of using "printf" for floats, not using floats.

Obviously, for printf to support floats - it has to include all the code for floats!

 

And that's on top of the code for rest of the printf functionality - which is quite a lot!

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, but my point is that using floats is independent of using printf for floats.

 

Using floats does not add much overhead.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

HI,

Am using Atmel Studio 7 & also am unable to use floating point in my program.

Silly beans are always silly
:)