assign value from a volatile variable

A

Anh-Tu Vo

Hi all,

I have a weird problem on a variable assignment. It seems that the
variable is only updated with the value at the second time the
function is called. My program looks like this:

myClass
{
typedef struct
{
union
{
struct _bit
{
UINT32 reservedBits0_15:16; // bits 0..15 Reserved
UINT32 phyStatus:16; // bits 16..32 PHY data
}bit;

UINT32 reg;
}u;
} miimstat_register_s;

volatile miimstat_register_s* ap_tsec1MiimstatReg;
}

myClass::myClass()
{
ap_tsec1MiimstatReg = (miimstat_register_s*)0x00024530;
}

void myClass::foo( unsigned int *p_data)
{
*p_data = (unsigned int)(ap_tsec1MiimstatReg->u.bit.phyStatus);
}

The problem is that *p_data is only updated with
ap_tsec1MiimstatReg->u.bit.phyStatus value when foo() is called the
second time.

Does anybody have an idea what the problem is?

thank you,
Anh Tu
 
R

Ryan Mack

UINT32 reservedBits0_15:16; // bits 0..15 Reserved
UINT32 phyStatus:16; // bits 16..32 PHY data

I would suggest avoiding C bitfields. The compiler may be ignoring your
volatile declaration when doing bitfield operations. Instead you should
just typedef miimstat_register_s as a UINT32 and use macros to get the
individual fields out of it. You could try inline functions instead of
macros but I'm not sure how they interact with volatile variables.

Ryan Mack
email: [first letter of first name][last name]@[last name]man.net
 
V

Victor Bazarov

Ryan said:
I would suggest avoiding C bitfields. The compiler may be ignoring your
volatile declaration when doing bitfield operations.

Do you know of any reason why it would/should? A passage from
the Standard, maybe?...
Instead you should
just typedef miimstat_register_s as a UINT32 and use macros to get the
individual fields out of it. You could try inline functions instead of
macros but I'm not sure how they interact with volatile variables.

I would think there is nothing particular about such "interaction".

V
 
R

Ryan Mack

Do you know of any reason why it would/should? A passage from
the Standard, maybe?...


I would think there is nothing particular about such "interaction".

I'm at home right now so no books available. The main reason I would
avoid them is because the field ordering is compiler-dependent (you don't
know if reservedBits is actually the high or low 16 bits). Looking into
it more, Ahn-To, you should declare the bit fields volatile too. That may
fix your problem.

As for the inline functions, I think the only safe way would be to declare
the inline function arguments volatile and then you end up propagating the
volatile declarations elsewhere in the code which becomes a pain to
maintain.

Ryan Mack
email: [first letter of first name][last name]@[last name]man.net
 
R

Ron Samuel Klatchko

I have a weird problem on a variable assignment. It seems that the
variable is only updated with the value at the second time the
function is called.

Looking at your code, I am assuming it is reading some sort of register
off a hardware device. Are you sure the data is available when you read
it the first time?

samuel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top