cancel
Showing results for 
Search instead for 
Did you mean: 

The question on the stm3210b_eval_lcd.c

lxnisme
Associate II
Posted on June 14, 2011 at 03:30

In

 

stm3210b_eval_lcd.c

 

Why use

 

static __IO

 

in

 

this definition

 

TextColor and LCDType ? 

Put another way

I understand the

 

static

 

definition of

 

the

 

prefix stands for

 

a static

 

variable?

but 

static sFONT

 

and

 

static __IO

 

should be

 

how to understand.

Who can

 

tell me?

#stm32 #stm32-lcd-eval
7 REPLIES 7
Posted on June 14, 2011 at 06:03

No doubt to localize it to that specific file/object, and not pollute the global name space.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist III
Posted on June 14, 2011 at 11:00

Did you have a particular reason for using such a large font?

''

static sFONT

 

and

 

static __IO

 

should be

 

how to understand

''

I would guess that sFont is a typedef - use the facilites in your tools to find its definition.

Anything that begins with underscores should be to do with the compiler - so check your tool Manuals for details of __IO;

Or, again, use the facilites in your tools to find its definition.

Any decent tools will have a ''Go To Definition'' feature.

You haven't stated what tools you're using, so it's impossible to say how, precisely, you will need to do it - you will have to check the Manuals yourself.

lxnisme
Associate II
Posted on June 14, 2011 at 11:30

lxnisme
Associate II
Posted on June 14, 2011 at 11:31

what is the advantage of this form of the Definition than others;

what is the difference between this two forms?

1.static __IO

uint_16_t  TextColor

;

2.static 

uint_16_t  TextColor

;

3.

uint_16_t  TextColor

;

Andrew Neil
Evangelist III
Posted on June 14, 2011 at 13:58

''what is the advantage of this form of the Definition''

 

Which form?

''than others''

 

What others?

''what is the difference between this two forms?''

How about you show a full & complete example of each (the definition and its use) - so that we know what you're actually talking about?!

Posted on June 14, 2011 at 22:16

what is the advantage of this form of the Definition than others;

 

what is the difference between this two forms?

 

1.static __IO uint_16_t  TextColor;

 

2.static  uint_16_t  TextColor;

 

3.uint_16_t  TextColor;

 

The benefit of the first two is that you get a ''global'' variable with a potentially common naming, but it's symbol doesn't pollute the global namespace. ie you can have a different TextColor defined in other files and they won't interfere with each other.

/**

 * IO definitions

 *

 * define access restrictions to peripheral registers

 */

#define     __I     volatile const            /*!< defines 'read only' permissions      */

#define     __O     volatile                  /*!< defines 'write only' permissions     */

#define     __IO    volatile                  /*!< defines 'read / write' permissions   */

The value of __IO in this context is a little more murky, but as a volatile it will force the compiler to re-read a value each time it is needed rather than optimizing off in a register, or out of a loop. This might be helpful if the value is apt to change under interrupt, or by another thread/task.

As to why it was done this way, I'm not sure you're going to have the originating engineer come here and explain the design decision. Probably to prevent some compiler warnings, or strict type checking. You should analyze the code for yourself, and draw your own conclusions. Try setting different warning levels, or type compliance from the compiler. Try LINT or something.

Bloody asinine forum software, had to edit this several times to get rid of the ''quoted message'' hiding content.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist III
Posted on June 15, 2011 at 10:49

''The benefit of the first two is that you get a ''global'' variable with a potentially common naming, but it's symbol doesn't pollute the global namespace.''

 

aka ''file scope''

Other potential advantages include:

  • Because the name is not visible outside of the file, other modules that shouldn't fiddle with it can't fiddle with it.
  • Restricting it to file-scope means that the compiler knows that it is not used elsewhere - so it can warn you if you don't use it at all.

''ie you can have a different TextColor defined in other files and they won't interfere with each other.''

That may or may not be an advantage in practice!

Having identically-typed and identically-named but distinct variables could lead to confusion...

''The value of __IO in this context is a little more murky''

In programming, there are always 2 things to consider:
  1. Getting the language syntax right;
  2. Making the programmer's intention clear.

In this case, the 'C' syntax is identical for both __O and __IO, but having different names should help to make the programmer's intention clearer.

It may also be that this is intended to be ''generic'', and could be used to encapsulate some compiler-specific (or target-specific) features related to IO...

However, as already noted, names beginning with underscores should be reserved for use by the compiler - user code should not  define names beginning with underscores!

''As to why it was done this way, I'm not sure you're going to have the originating engineer come here and explain the design decision.''

 

 

When writing example  code, this is the kind of thing that really should  be clearly explained!

See rant here: 

http://www.8052.com/forumchat/read/181034