EmbeddedRelated.com
Forums

IAR v4.11 msp430

Started by franciscocantero1 July 20, 2009
Hello,
I'm experiencing some problems with printf(), sprintf().
It seems that
va_start(ap,format);
where va_list ap
char* format
Is not populating the ap with a valid address, it keeps the same address that had before running that line.
while debugging if i set the ap varaiable to the address that i want it prints ok. but at runtime it doesnt print the string, it prints whatever contains that va_list.

I dont know why is doing this, I use IAR 3.42 and the code works properly.
Any help will be welcome.

Beginning Microcontrollers with the MSP430

I forgot to say that the problem with va_start is in iar 4.11

franciscocantero1 wrote:
> I'm experiencing some problems with printf(), sprintf().
> It seems that
> va_start(ap,format);
> where va_list ap
> char* format
> Is not populating the ap with a valid address, it keeps the same address
> that had before running that line.
> while debugging if i set the ap varaiable to the address that i want it
> prints ok. but at runtime it doesnt print the string, it prints whatever
> contains that va_list.
>
> I dont know why is doing this, I use IAR 3.42 and the code works properly.
> Any help will be welcome.

Hi!

I could take a look at it, but I would need a small but complete example
that demonstrates the problem.

Also, I would need to know if you are using DLib or the legacy CLib library.

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

>
> Hi!
>
> I could take a look at it, but I would need a small but complete example
> that demonstrates the problem.
>
> Also, I would need to know if you are using DLib or the legacy CLib library.
>
> -- Anders Lindgren, IAR Systems
> --
> Disclaimer: Opinions expressed in this posting are strictly my own and
> not necessarily those of my employer.
>

Hello;

I used CLib but for trying to get it to work I used both of them with the same result.
I modified one of the printf functions to uprintf, by the way thanks to your help.
an example of the code is:

uprintf(serial_out,"\r\n time %d:%d:%d\r\n",hours,minutes,seconds);

where serial_out is a function that send bytes to the serial.

this uprintf when is ran we can see nothing on the console just:
time (various strange carachters corresponding to the portion of memory that va_list has):(same as previous place):(same)
> I could take a look at it, but I would need a small but complete
example
> > that demonstrates the problem.
> >
> > Also, I would need to know if you are using DLib or the legacy CLib
> library.

>
> I used CLib but for trying to get it to work I used both of them with
> the same result.
> I modified one of the printf functions to uprintf, by the way thanks to
> your help.
> an example of the code is:
>
> uprintf(serial_out,"\r\n time %d:%d:%d\r\n",hours,minutes,seconds);
>
> where serial_out is a function that send bytes to the serial.
>
> this uprintf when is ran we can see nothing on the console just:
> time (various strange carachters corresponding to the portion of memory
> that va_list has):(same as previous place):(same)

Hi!

Unfortunately, it's still not enough information for me to investigate.

I would need a small complete example that I could compile.

By "complete", I mean that I should be able to place a piece of code in
a source file, compile, link, and run without having to modify the
program in any way. By "small" means that I do not want a large
application (most issues can be distilled into a handful of lines).

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

hello:

this is a test that im doing:

void main()
{
char buffer[18];
int hours,minutes,seconds;
hours;
minutes ;
seconds";

sprintf(buffer,"hello time is: %d:%d:%d\r\n",hours,minutes,seconds);
}

buffers has: hello time is: 10:20:22\r\n

Sprintf is working in my code, but the function that you help me with uprintf (message 37845) (that was working in 3.41) is not working in this new version.

my uprintf is as follows

#include "stdarg.h"
#include "stdio.h"
#include

//printing function
extern void (*serial_out)(char c);

int _formatted_write(const char *,
void (*)(char, void *),
void *,
va_list);

int uprintf(void (*func)(char c), const char *format, ...)
{
va_list ap;
int nr_of_chars;

va_start(ap, format); /* Variable argument begin */
nr_of_chars = _formatted_write(format, (void(*)(char,void*))func, (void *) &format, ap);
va_end(ap); /* Variable argument end */
return nr_of_chars; /* According to ANSI */
}

What i notice is that inside formatted write (frmwri.c) at line 477
if( !(buff_pointer= VAPTR(char))) this buff_pointer is not pointing to the string that i want to print. So I believe this is happening because va_start(ap,format) is not starting the ap properlly.

franciscocantero1 wrote:
> this is a test that im doing:
>
> [...]
>
> What i notice is that inside formatted write (frmwri.c) at line 477
> if( !(buff_pointer= VAPTR(char))) this buff_pointer is not pointing to
> the string that i want to print. So I believe this is happening because
> va_start(ap,format) is not starting the ap properlly.

It looks as though it should work -- even though there are some strange
details in your code. (It is not kosher cast between function pointer
types, instead you should ensure that your serial out function should
take two parameters. Also, the third parameter to _formatted_write
should should be '0', unless you plan to use it in your output function.)

If you put together something I can compile and run (with some kind of
dummy variant for 'serial_out', e.g. something that puts the characters
in an array), I could take a look at it.

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

#include "msp430x16x.h"
#include "stdlib.h"
#include "string.h"
#include "icclbutl.h"
#include "stdarg.h"
#include "stdio.h"

const char * const printout = "Francisco";

char *buffer;
void PrintTo_Buf(char ,void*);

int uprintf(void (*func)(char ,void*), const char *format, ...);

int main(void)
{
int i;
char *temp;
buffer = (char *) malloc(70);
temp=buffer;
i=0;

while(i<50)
{
i++;
uprintf(PrintTo_Buf,"hello %s, times %d\r\n","francisco",i);
memset(temp, 0x00, 70);
buffer=temp;
WDTCTL = WDT_ARST_1000;
}
return 0;
}
void PrintTo_Buf(char c,void *dummy)
{
*buffer = c;
buffer++;
(void)dummy; /* Warning on this line OK (Optimized Away) */
}
int uprintf(void (*func)(char ,void*), const char *format, ...)
{
va_list ap;
int nr_of_chars;

va_start(ap, format); /* Variable argument begin */
nr_of_chars = _formatted_write(format, (void(*)(char,void*))func, (void *) &format, ap);
va_end(ap); /* Variable argument end */
return nr_of_chars; /* According to ANSI */
}

This code should put hello Francisco, times 1\r\n
but the buffer has "hello 0P!R>@F, times 4430
"

Thanks for your help,
Francisco

franciscocantero1 wrote:
> This code should put hello Francisco, times 1\r\n
> but the buffer has "hello 0P!R>@F, times 4430

Strange, I get:

"hello francisco, times 1

hello francisco, times 2

hello francisco, times 3
..."

I made two minor changes, just so that I would be able to see the result:

1) I moved the "memset" call to before the call to uprintf (so that the
resulting string would be properly zero-terminated).

2) Added a "puts(temp)" after the call to uprintf.

What compiler settings do you use? (If possible, please send me the
entire command line.)

Just one final thing, what HEAP size do you use? (The default is 50
bytes, if I recall correctly, so malloc(70) might return 0).

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

> What compiler settings do you use? (If possible, please send me the
> entire command line.)

I just set up the compiler from the project options.
Language C
Allow IAR extensions
char is unsigned
R4 normal use
R5 normal use
reduce stack usage
No optimizations

> Just one final thing, what HEAP size do you use? (The default is 50
> bytes, if I recall correctly, so malloc(70) might return 0).

My stack was 350 and heap 1200. When I reduced the them both to 100, then i got this print
hello hello , times 1
insted of "francisco" i got "hello".
And if i modify:
uprintf(PrintTo_Buf,"how u doing %s, times %d\r\n","francisco",i);
i get:
how u doing how u doing , times 1

but at least the integer is printing properly