sprintf problem

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

Im having trouble with adding any variable type to sprintf.

 

sprintf(TXdataBuffer,"Counter = \r\n");  

 

works as I expect, but:

 

unsigned int counter=0;     

 

 

unsigned int counter=0;     
sprintf(TXdataBuffer,"Counter = %u \r\n",counter);

 

gives a compiller error: undefined reference to -sbrk

 

I have only used sprintf with AVR before, now im trying to use with 32bit and am having these troubles.

 

thanks for any ideas.

Last Edited: Fri. Apr 17, 2020 - 11:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How about telling us what tools you're using?

 

ClaytonLL wrote:
trying to use with 32bit

really tells us nothing!

 

Except that it's off-topic for this[1] forum ?

 

ClaytonLL wrote:
gives a compiller error: undefined reference to -sbrk

Have you tried just putting that message into your favourite internet search engine? Maybe adding the name of whatever tools you're using?

 

I think it has something to do with dynamic memory allocation ...

 

EDIT

 

And I strongly suspect that's a Linker error - not a compiler error

 

 

[1] was originally posted in the AVR forum.

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...
Last Edited: Fri. Apr 17, 2020 - 10:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Im using Atmel studio 7, and yes ive tried searching web and this forum..

 

Ahh.. im not too familiar with playing with the linker, also, I thought i was in the smart arm community.. must have made a mistake

Last Edited: Fri. Apr 17, 2020 - 07:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ClaytonLL wrote:
Im using Atmel studio 7

With what toolchain ?

 

 

, and yes ive tried searching web and this forum..

Go on:  https://www.google.com/search?q=undefined+reference+to+%22-sbrk%22

 

https://www.google.com/search?q=undefined+reference+to+%22-sbrk%22+GCC

I thought i was in the smart arm community.. must have made a mistake

Indeed!

 

https://community.atmel.com/forums/atmel-studio-arm-related

 

 

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...
Last Edited: Fri. Apr 17, 2020 - 08:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ARM/GNU

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

If you have an AS7.0 project,  ZIP up the project and attach the ZIP.   30 seconds from your life.

 

Describe your problem clearly.   10-20 minutes from your life.

 

You will probably get sensible replies.   If you have "common" SAM hardware,   readers might even run on real hardware.    But many problems can be spotted from the Simulator.

 

David. 

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

My problem is I cannot get sprintf to work with any variables. I get build errors, "undefined reference to sbrk"

the issue is no doubt related to the linker and memroy allocation, but these are areas I dont know much about. Im happy to read up on it, but having a steer in the right direction would be helpful.

device is SAME70, and im running it on the SAME70 xplained board. solution is attached.

 

adding any variable type to sprintf causes the build problem:


volatile unsigned int counter=0;
int main(void)
{

	setup();

    while (1)
    {
		if (statusPIOA&PIO_PA11)                //push button interrupt
		{
			ledControl(LEDoN);
			delaymS(15);
			ledControl(LEDoFF);
			sprintf(TXdataBuffer,"Button pushed %u times \r\n",counter);
			uartSendData();
			counter++;
			statusPIOA=0;
		}
	}
}

and if I remove it I have no build problem: 


int main(void)
{

	setup();

    while (1)
    {
		if (statusPIOA&PIO_PA11)
		{
			ledControl(LEDoN);
			delaymS(15);
			ledControl(LEDoFF);
			sprintf(TXdataBuffer,"Button pushed\r\n");   //
			uartSendData();
			counter++;
			statusPIOA=0;
		}	

	}
}

 

Attachment(s): 

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

It means that you haven't got (all of) the correct runtime library support.

 

SAME70, and im running it on the SAME70 xplained board

So have you used one of the ready-to-go examples for that chip on that board?

 

First hit from  https://www.google.com/search?q=undefined+reference+to+%22-sbrk%22+GCC+ARM is:

 

https://stackoverflow.com/questions/5764414/undefined-reference-to-sbrk

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...
Last Edited: Fri. Apr 17, 2020 - 08:30 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I have written my own code, not using ASF.  Im trying to learn to program the part at the register level.

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

This is a 'C' runtime issue - nothing to do with ASF or registers.

 

You should at least start with the provided example - to see how a proper setup should look.

 

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

david.prentice wrote:
many problems can be spotted from the Simulator

This is a build issue - so nothing to run in the simulator.

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

I have run may example projects to get me this far, but I find ASF very long winded and confusing. I want to understand what is happening at register level. I have used sprintf since I am familiar with that function, and I understand it may not be the best option, but would be nice to understand why its not working.

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

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

stdio requires a few runtime functions that are platform specific. Most of these functions can just be stub functions (they do nothing apart from exist). I had the same issue many years ago with a SAML21 - you need to look carefully at the example code that gives you printf - the answer will be there. It is also a common problem - Google should help - it's the same issue on other platforms.

 

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

Kartman wrote:
Google should help 

Indeed - see #13 and #8

 

 

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

ClaytonLL wrote:
I want to understand what is happening

Take a look at #3 and #7 in this thread:

 

https://www.avrfreaks.net/commen...

 

to understand how the build process works - and the separate roles of compiler, linker, etc ...

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

@ClaytonLL,

 

Seriously,  you should probably use START and put up with the gobbledygook.

 

Creating a trivial project in AS7.0 requires a bit of fiddling.   The defaults don't work even for SAMD21, ...

You get exactly the same _sbrk errors.

 

It is easier in Keil IDE:

*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Rebuild target 'SAME70'
assembling startup_SAME70.s...
compiling system_same70q21.c...
compiling SAME70_interrupt.c...
linking...
Program Size: Code=2248 RO-data=352 RW-data=12 ZI-data=2636  
".\Objects\SAMD21_interrupt.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed:  00:00:16

Note that you need the Atmel.SAME70_DFP.2.4.166 to understand your defines.   Keil will accept the .atpack file if you rename as .pack

 

The whole Atmel SAM folder structure is horrible.  i.e. component/ and instance/

But the worst feature is the change of symbol names with the current Pack Release.

 

David.