assign value from a volatile variable

Discussion in 'C++' started by Anh-Tu Vo, May 25, 2004.

  1. Anh-Tu Vo

    Anh-Tu Vo Guest

    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
    Anh-Tu Vo, May 25, 2004
    #1
    1. Advertising

  2. Anh-Tu Vo

    Ryan Mack Guest

    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
    Ryan Mack, May 25, 2004
    #2
    1. Advertising

  3. 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
    Victor Bazarov, May 25, 2004
    #3
  4. Anh-Tu Vo

    Ryan Mack Guest

    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
    Ryan Mack, May 25, 2004
    #4
  5. (Anh-Tu Vo) wrote in message news:<>...
    > 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
    Ron Samuel Klatchko, May 27, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    8
    Views:
    53,545
    saravanavsk
    Jan 25, 2010
  2. Nmishra
    Replies:
    3
    Views:
    364
    Default User
    May 24, 2004
  3. ben
    Replies:
    5
    Views:
    590
    Ulrich Eckhardt
    Jan 11, 2005
  4. Replies:
    18
    Views:
    7,065
    Karl Heinz Buchegger
    Jul 22, 2005
  5. Matt
    Replies:
    2
    Views:
    841
    Anthony Judd
    Sep 25, 2004
Loading...

Share This Page