By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

assign value from a volatile variable

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Tue, 25 May 2004 07:46:44 -0700, Anh-Tu Vo wrote:
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
Jul 22 '05 #2

P: n/a
Ryan Mack wrote:
On Tue, 25 May 2004 07:46:44 -0700, Anh-Tu Vo wrote:

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.


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
Jul 22 '05 #3

P: n/a
On Tue, 25 May 2004 10:58:46 -0400, Victor Bazarov wrote:
Ryan Mack wrote:
On Tue, 25 May 2004 07:46:44 -0700, Anh-Tu Vo wrote:

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.


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".


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

Jul 22 '05 #4

P: n/a
ha***@hotmail.com (Anh-Tu Vo) wrote in message news:<ff**************************@posting.google. com>...
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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.