EmbeddedRelated.com
Forums

printf behavior

Started by Unknown May 19, 2006
Hi,

I'm having trouble using the printf function to send a string of chars
using
a custom __putchar function. That is, this works:

__putchar('h');

..while this doesn't:

printf("hello");

The printf function even seems to halt the execution of the program (it
hangs somewhere in the following code, hard to say where. This is a bit
puzzling, especially as it worked a few days ago, and I cannot see any
particular modifications I have done that should cause it to malfunction.
I'm using CrossStudio 1.3.

I hope someone can help me out here.. if the putchar function hadn't been
working I'd known where to look, but as it is, I'm quite at a loss..


best regards,
-hard





Beginning Microcontrollers with the MSP430

Harvard,

What does your __putchar function look like?  Prototypes and
implementation would be helpful.

--
Paul Curtis, Rowley Associates Ltd   http://www.rowley.co.uk 
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors

 

> -----Original Message-----
> From: Hard Vst [mailto:hvagstol@hvag...] 
> Sent: 19 May 2006 12:00
> To: MSP430 Yahoo Group
> Subject: [msp430] printf behavior
> 
> Hi,
> 
> I'm having trouble using the printf function to send a string 
> of chars using a custom __putchar function. That is, this works:
> 
> __putchar('h');
> 
> ..while this doesn't:
> 
> printf("hello");
> 
> The printf function even seems to halt the execution of the 
> program (it hangs somewhere in the following code, hard to 
> say where. This is a bit puzzling, especially as it worked a 
> few days ago, and I cannot see any particular modifications I 
> have done that should cause it to malfunction.
> I'm using CrossStudio 1.3.
> 
> I hope someone can help me out here.. if the putchar function 
> hadn't been working I'd known where to look, but as it is, 
> I'm quite at a loss..
> 
> 
> best regards,
> -hard
> 
> 
> 
> 
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> --------------------~--> Protect your PC from spy ware with 
> award winning anti spy technology. It's free.
> http://us.click.yahoo.com/97bhrC/LGxNAA/yQLSAA/CFFolB/TM
> --------------------------
> ------~-> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 

On 5/19/06, Paul Curtis <plc@plc@...> wrote:

> What does your __putchar function look like? 
Prototypes and
> implementation would be helpful.
>

My putchar function looks like this.. splits the incoming data into three
symbols and sends them in sequence. As I said it works fine when called with
for instance __putchar('A') but not with printf.

int __putchar(int data){
  int sym1, sym2, sym3;
  char mask1 = 0x07;
  char mask2 = 0x38;
  char mask3 = 0xC0;

  sym1 = (data & mask1);
  sym2 = (data & mask2) >> 3;
  sym3 = (data & mask3) >> 6;

  transmit_pulse(sym1);
  transmit_pulse(sym2);
  transmit_pulse(sym3);

  return 0;
}


-hard





Are you sure that your stack is big enough?

Are you using debug or release mode?

--
Paul Curtis, Rowley Associates Ltd   http://www.rowley.co.uk 
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors

 

> -----Original Message-----
> From: Hard Vst [mailto:hvagstol@hvag...] 
> Sent: 19 May 2006 12:34
> To: msp430@msp4...
> Subject: Re: [msp430] printf behavior
> 
> On 5/19/06, Paul Curtis <plc@plc@...> wrote:
> 
> > What does your __putchar function look like?  Prototypes and 
> > implementation would be helpful.
> >
> 
> My putchar function looks like this.. splits the incoming 
> data into three symbols and sends them in sequence. As I said 
> it works fine when called with for instance __putchar('A') 
> but not with printf.
> 
> int __putchar(int data){
>   int sym1, sym2, sym3;
>   char mask1 = 0x07;
>   char mask2 = 0x38;
>   char mask3 = 0xC0;
> 
>   sym1 = (data & mask1);
>   sym2 = (data & mask2) >> 3;
>   sym3 = (data & mask3) >> 6;
> 
>   transmit_pulse(sym1);
>   transmit_pulse(sym2);
>   transmit_pulse(sym3);
> 
>   return 0;
> }
> 
> 
> -hard
> 
> 
> 
> 
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> --------------------~--> Everything you need is oneclick 
> away. Make Yahoo! your home pagenow.
> http://us.click.yahoo.com/AHchtC/4FxNAA/yQLSAA/CFFolB/TM
> --------------------------
> ------~-> 
> 
> .
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 

Hard Vst wrote:
> On 5/19/06, Paul Curtis <plc@plc@...> wrote:
> 
>> What does your __putchar function look like?  Prototypes and
>> implementation would be helpful.
>>
> 
> My putchar function looks like this.. splits the incoming data into three
> symbols and sends them in sequence. As I said it works fine when called
with
> for instance __putchar('A') but not with printf.
> 
> int __putchar(int data){
>   int sym1, sym2, sym3;
>   char mask1 = 0x07;
>   char mask2 = 0x38;
>   char mask3 = 0xC0;
> 
>   sym1 = (data & mask1);
>   sym2 = (data & mask2) >> 3;
>   sym3 = (data & mask3) >> 6;
> 
>   transmit_pulse(sym1);
>   transmit_pulse(sym2);
>   transmit_pulse(sym3);
> 
>   return 0;
> }
> 
> -hard

Try returning 'data' on success or EOF (-1) on error

Regards
-Bill Knight
R O SoftWare

On 5/19/06, Paul Curtis <plc@plc@...> wrote:
>
> Are you sure that your stack is big enough?
>
> Are you using debug or release mode?
>

I'm not sure it is. I take it you mean that the printf function may be
"overloading" the stack in some way? How may this be prevented?

I might add that I tried some of the other tips that people gave (putting in
some delay in the putchar, returning a non-zero value), though without
getting any closer to solving the problem.

Usually I run the program in debug mode, though I've also tried running in
release mode, with equal results..

-hard





Harvard, 

> On 5/19/06, Paul Curtis <plc@plc@...> wrote:
> >
> > Are you sure that your stack is big enough?
> >
> > Are you using debug or release mode?
> >
> 
> I'm not sure it is. I take it you mean that the printf 
> function may be "overloading" the stack in some way? How may 
> this be prevented?

Printf has a small stack requirement, it isn't huge.  It does not use an
internal buffer for conversion in RAM-limited micros.

> I might add that I tried some of the other tips
that people 
> gave (putting in some delay in the putchar, returning a 
> non-zero value), though without getting any closer to solving 
> the problem.

Well, I've just tried it and it works for me.

> Usually I run the program in debug mode, though
I've also 
> tried running in release mode, with equal results..

Then I would suggest that your stack setting is too small.  I'm not sure
what else to suggest.  I'll give it a bit more thought.

--
Paul Curtis, Rowley Associates Ltd   http://www.rowley.co.uk 
CrossWorks for ARM, MSP430, AVR, MAXQ, and now Cortex-M3 processors



Never had any problems either with __putchar() - printf() interface.
One remote possibility is that you have declared a __putchar() somewhere else
with
a different prototype ? (or a putchar() )
Best is to step _into_ a simple printf() call, go through vsprintf() and see if
you indeed
land
in your __putchar() function. (you'll have to step in ASM since it's
LIB code).
If that doesn't work out, there's something really haywire.

Perhaps start with a small, separate project with printf() and __putchar(), and
re-build
up your
new project until it either works properly, or you identify where your current
project is
dropping the ball.

-- Kris

>-----Original Message-----
>From: msp430@msp4... [mailto:msp430@msp4...] On Behalf Of Hard
>Vst
>Sent: Monday, 22 May 2006 8:22 PM
>To: msp430@msp4...
>Subject: Re: [msp430] printf behavior
>
>On 5/19/06, Paul Curtis <plc@plc@...> wrote:
>>
>> Are you sure that your stack is big enough?
>>
>> Are you using debug or release mode?
>>
>
>I'm not sure it is. I take it you mean that the printf function may be
>"overloading" the stack in some way? How may this be prevented?
>
>I might add that I tried some of the other tips that people gave (putting in
>some delay in the putchar, returning a non-zero value), though without
>getting any closer to solving the problem.
>
>Usually I run the program in debug mode, though I've also tried running
in
>release mode, with equal results..
>
>-hard
>
>
>
>
>
>
>
>.
>
>
>Yahoo! Groups Links
>
>
>
>
>



On 5/22/06, microbit <microbit@micr...> wrote:

> Perhaps start with a small, separate project with
printf() and
> __putchar(), and re-build
> up your
> new project until it either works properly, or you identify where your
> current project is
> dropping the ball.
>

Though I guess it should have been  obvious to try this right away, it did
lead me to some sort of solution. It appears that when I trimmed the main
function of the project, moving some code to another function, things looked
up. Am I correct in guessing that somehow a bloated main function may strain
the controller enough to halt execution? If someone could be bothered to
shed some light on why this seems to be the case, I'd be glad..

However, things seem to be working fine now, so thanks to all who suggested
solutions!

-hard