EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Static int declaration and initialization

Started by Mirko Viviani September 2, 2008
Using IAR on a MSP430F1611 (MSP430 family) this piece of code does not work
(printf will print something else than 23)
I cannot split the inizialization from declaration (source code cannot be
modified): any idea?

static int i#;
void main()
{
printf("%d",i);
}

Obviously splitting initialization from declaration works.

--
-- Mirko Viviani --

Beginning Microcontrollers with the MSP430

I suspect that printf("%d",23); will also print something else than
23. Can you try that?
it prints correctly 23 ;)

2008/9/2 old_cow_yellow
I cannot try printf(). But "static int i#;" works for me.

Specifically, the compiler did the following:

a) Assigned a word in RAM for the variable "i"
b) Assigned a word in FLASH and stored a constant "23" (0x0017) in it.
c) In the c startup code, call "__data16_memcpy()" to copied the
constant "23" to the variable "i".
Hi!

Just to eliminate the obvious -- did you include the header file "stdio.h"?

If not, then you should have listened to the compiler warnings (they are
there for a reason). On the other hand, if you did include it you should
have included it in the example you posted here.

If we assume that that is not the issue, what is the value of the
variable "i" if you debug the application?

What settings do you use? (Optimization levels, data models etc.)

-- Anders Lindgren, IAR Systems
the problem was in the custom cstartup code.
By replacing it with the default one, no more problems occurred.

Thanks a lot for the tips!
>>> the problem was in the custom cstartup code. By replacing it with the
>>> default one, no more problems occurred.

... but ... but ... but ... you didn't tell us that you had changed the
startup code!

If you change a very major part of the system such as the startup code and
then basic C code stops working, then maybe just maybe you might have broken
something!

If you had provided FULL information then maybe you would have had an
instant & correct reply.
I though at cstartup code only after the old_cow_yellow response.
Anyway, thanks to everybody!
My experience has been that custom startup code causes more problems
than it's worth. To the original poster, would you please give us
some insight as to why you needed to customize it?

My (limited) experience has been that any optimizations are
overwhelmed by the complications and potential for problems (as shown
by the poster). If you need RAM or some hardware initialized in some
special way, why didn't you just do it at the top of main()? On an
MSP430, there is so little RAM, and I'm sure very little of it get
pre-initialized.

The only time I've seen a custom startup routine make sense (and I
actually don't agree that it was the best way to do it) was in an
avionics application where a restart was implicitly due to a power
blip and certain hardware had to be initialized quickly.

This is not a criticism, but I really do want to know what drove that
design decision.

Thanks.

Stuart
All, I do want to clarify that on the projects which used custom
startup routines were NOT MSP430-based systems. They happen to have
been 68HC12 and ARM9 boards, but I still maintain that the
customizations had questionable utility at best.

Stuart

Memfault Beyond the Launch