NewLib - sprintf part 2!

Hi all,

Before I start, yes, I did take a look at the other newLib thread…

I added the “PROVIDE(end = .);” to my cmd file, added libc.a to my linker script

$(LD) $(LFLAGS) -o main.out $(OBJECTS) libc.a libgcc.a

and it compiles! Yippeeeeee! :smiley:

NOW, here is the strange thing!

If I do the following command

            sprintf(buffer, "Count\r\n");

Everything works. Life is good.

If I do this

            sprintf(buffer, "Count[%d]\r\n", counter);

everything compiles, but when I run it, it crashes (the blinker flashes 3 times)

Looks like something with the variable args…

Thoughts?!

~Kam (^8*

Just a thought.

Got enough stack space?

–Dave

ummmm…not sure…

How would I check/update using arm-elf-gcc? I’m not too familier with the arm-elf-gcc compiler…

Here are my command line args from the makefile

CC      = arm-elf-gcc
LD      = arm-elf-ld -v
AR      = arm-elf-ar
AS      = arm-elf-as
CP      = arm-elf-objcopy
OD	= arm-elf-objdump

CFLAGS  = -I./ -c -fno-common -O0 -g
AFLAGS  = -ahls -mapcs-32 -o crt.o
LFLAGS  =  -Map main.map -Tlink_flash.cmd
CPFLAGS = --output-target=binary
ODFLAGS	= -x --syms

OBJECTS = crt.o	main.o timerisr.o timersetup.o isrsupport.o lowlevelinit.o blinker.o usart.o ARMTools.o adc.o

~Kam (^8*

I am not sure about syntactic, it should be

sprintf(buffer, “%s”, “Count\r\n”);

No, the syntax is good. This should’nt be the problem.

As far as I know, the printf family of function in NewLib use the heap space. Do you have implemented the sbrk system function ?

If not, I’m not sure of the resulting execution of your sprintf call…

try somthing a bit more simple to narrow the problem down.

try:

sprintf(buffer, “Count[%u]\r\n”, 0x01);

I bet for the stack problem :smiley:

I also have problems with sprintf. I use sprintf to create char buffers wich are then written on a SD card with efsl. The complete system runs with uC/os, each task have a 400 byte stack.

It works fine with the debugger, my buffer is right, but something goes wrong when I try to write the buffer with the file_write function. The system doesn’t crash, but the file is never created. Side effect ???

I will try with a bigger stack and post if it works.

Angelo

Whats the arg to increase the stack sizein gcc?

seulater:
try somthing a bit more simple to narrow the problem down.

try: sprintf(buffer, “Count[%u]\r\n”, 0x01);

Here are my results for a few different styles…

#1. //    sprintf(buffer, "Kam Was Here");                    //  works
#2. //    sprintf(buffer, "%s", "Count\r\n");                 //  works
#3. //    sprintf(buffer, "%s %s", "Count1", "Count2\r\n");   //  no work. LED Blinks 3 times - Data Abort Exception
#4. //    sprintf(buffer, "Count[%u]\r\n", 1);                //  no work. LED Blinks 3 times - Data Abort Exception

Now it’s interesting that #2 works, but #4 (2 args like #2) does not!

Thoughts?!

~Kam (^8*

KamPutty:
Whats the arg to increase the stack sizein gcc?

The stacks are set up in the assembly language startup file, not in GCC. Your build environment may have provisions for passing that information into the assembler at build time, or the size settings may be coded into the startup file directly. Whichever way your tools are set up to do it, you want to be sure that the stacks NEVER overflow. VERY strange things happen when a stack gets corrupted. I learned that from experience. :oops:

– Dave

:smiley: My problem was due to the stack size. I increased it to 2k and everything works properly now.

Angelo

Hi all, I think have the same problem with the sprintf as discuss previously in this forum. Using the development boar from ALIMEX, SAM-PXXX, ARM-USB-OCD jtag. At the last discussion, mentioned about changing the size of stack. How do I do so ?

At the moment I am using sample program from James P Lynch for the testing. I try to find the stack size definition in “crt.s” and found as bellow :

/* Stack Sizes */

.set UND_STACK_SIZE, 0x00000010 /* stack for “undefined instruction” interrupts is 16 bytes */

.set ABT_STACK_SIZE, 0x00000010 /* stack for “abort” interrupts is 16 bytes */

.set FIQ_STACK_SIZE, 0x00000080 /* stack for “FIQ” interrupts is 128 bytes */

.set IRQ_STACK_SIZE, 0X00000080 /* stack for “IRQ” normal interrupts is 128 bytes */

.set SVC_STACK_SIZE, 0x00000080 /* stack for “SVC” supervisor mode is 128 bytes */

My question is which stack location which the sprintf function use ?

How do I sure when changing the stack size, will not overflow ?

Thanks all

fayes

fayes:
Hi all, I think have the same problem with the sprintf as discuss previously in this forum. Using the development boar from ALIMEX, SAM-PXXX, ARM-USB-OCD jtag. At the last discussion, mentioned about changing the size of stack. How do I do so ?

At the moment I am using sample program from James P Lynch for the testing. I try to find the stack size definition in “crt.s” and found as bellow :

/* Stack Sizes */

.set UND_STACK_SIZE, 0x00000010 /* stack for “undefined instruction” interrupts is 16 bytes */

.set ABT_STACK_SIZE, 0x00000010 /* stack for “abort” interrupts is 16 bytes */

.set FIQ_STACK_SIZE, 0x00000080 /* stack for “FIQ” interrupts is 128 bytes */

.set IRQ_STACK_SIZE, 0X00000080 /* stack for “IRQ” normal interrupts is 128 bytes */

.set SVC_STACK_SIZE, 0x00000080 /* stack for “SVC” supervisor mode is 128 bytes */

My question is which stack location which the sprintf function use ?

How do I sure when changing the stack size, will not overflow ?

Thanks all

fayes

What you have shown only defines some of the stack sizes, but not their actual placement in memory. As for which stack sprintf is using, it depends on which mode you are actually running in, which you have not shown. When your code sets up the stack locations, that defines the starting point for each stack you set up, and then it will grow downwards from that point as your code uses it. I normally define the stack for the mode that I will run in last, so that it can grow downward toward the heap. That will give the running mode stack the largest size possible. That still leaves the possibility of stacks corrupting each other though.

You are probably running in USER mode, SYSTEM mode, or SUPERVISOR mode, but you have only shown the definition of SUPERVISOR mode. The mode that you are actually going to run in does not actually need to have a size definition if it is placed below the other stacks, since it will just grow down toward the heap. That sort of implies that you are actually running in USER mode or SYSTEM mode if your startup defines stacks with the running mode stack at the lowest position. That would be where sprintf is using stack space, unless you are calling it from an interrupt service routine, which is something that I would not want to do myself.

As for your last question, only you can know how much data you are placing on your stacks with local variables and calls, so you would need to calculate your stack sizes based on your knowledge of what your code is doing.

–Dave

Formating strings and integer with sprintf, it´s ok…

but no floating point (float)

I try up stack size and nothing happen…

Somebody help?

sprintf(str,"%5.3f",10000.001);

I have a similar problem that you KamPutty :

KamPutty:

seulater:
try somthing a bit more simple to narrow the problem down.

try: sprintf(buffer, “Count[%u]\r\n”, 0x01);

Here are my results for a few different styles…

#1. //    sprintf(buffer, "Kam Was Here");                    //  works

#2. // sprintf(buffer, “%s”, “Count\r\n”); // works
#3. // sprintf(buffer, “%s %s”, “Count1”, “Count2\r\n”); // no work. LED Blinks 3 times - Data Abort Exception
#4. // sprintf(buffer, “Count[%u]\r\n”, 1); // no work. LED Blinks 3 times - Data Abort Exception

But for me: when yout code works, mine also.

When you write no work : objcopy give me a very large binary : (400M as describe by someone else here http://www.embeddedrelated.com/usenet/e … 7990-1.php)

very strange behaviour !

Anyone gets some more info on this subject ?

Sorry, for bringing old topic to life, but I also have the same problem. Sprintf() doesn’t work with floats.

Anyone found a solution to that problem?