473,548 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

assign value from a volatile variable

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_1 5:16; // bits 0..15 Reserved
UINT32 phyStatus:16; // bits 16..32 PHY data
}bit;

UINT32 reg;
}u;
} miimstat_regist er_s;

volatile miimstat_regist er_s* ap_tsec1Miimsta tReg;
}

myClass::myClas s()
{
ap_tsec1Miimsta tReg = (miimstat_regis ter_s*)0x000245 30;
}

void myClass::foo( unsigned int *p_data)
{
*p_data = (unsigned int)(ap_tsec1Mi imstatReg->u.bit.phyStatu s);
}

The problem is that *p_data is only updated with
ap_tsec1Miimsta tReg->u.bit.phyStatu s 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
4 2493
On Tue, 25 May 2004 07:46:44 -0700, Anh-Tu Vo wrote:
UINT32 reservedBits0_1 5: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_regist er_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
Ryan Mack wrote:
On Tue, 25 May 2004 07:46:44 -0700, Anh-Tu Vo wrote:

UINT32 reservedBits0_1 5: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_regist er_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 "interactio n".

V
Jul 22 '05 #3
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_1 5: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_regist er_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 "interactio n".


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
ha***@hotmail.c om (Anh-Tu Vo) wrote in message news:<ff******* *************** ****@posting.go ogle.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
19267
by: google-newsgroups | last post by:
Hello, even (or because?) reading the standard (ISO/IEC 9899/1999) I do not understand some issues with volatile. The background is embedded programming where data is exchanged between main program flow and interrupts. At the organisation I work, instead of declaring a variable volatile, it is casted to volatile when necessary:
17
2319
by: dingoatemydonut | last post by:
The C99 standard states: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)."...
22
2264
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable is this the proper use of volatile?
14
7431
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: Interlocked.Increment(ref count); The error says that a volatile field cannot be used as ref or out, but if I don't use the volatile field, the value may be cached away...
19
7193
by: steve | last post by:
// What I want to do Use enumerated types with the Interlocked.Exchange methods Suggestions please // My estimation of problem Seems like Interlocked.Exchange only works with ints, referencable objects, and floats
10
2882
by: Lau Lei Cheong | last post by:
Hello, I really need to use volatile System.Int64 for a .NET v1.1 program in C#. But the compiler complains "a volatile field can not be of type long". How to work around it? Or is there any other way to get similar effect for Int64 type? Another question less urging question is, why long variables can't be used as volatile? I understand...
16
3864
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether it is even possible. Here is what my code looks like: // data is a variable 1 byte in size while bitVariable is 1 bit in size. i is a byte. ...
23
4068
by: Anders Borum | last post by:
Hi! I am implementing a threaded producer / consumer pattern just for fun. I am using an internal counter to keep track of the produced / consumed items and am logging that information. I am using the Interlocked class to increment a set of counter. Without placing an explicit lock around calls to the Interlocked class, how do you get...
1
1206
by: lovecreatesbea... | last post by:
Can type qualifiers be added through declarations For example, I have one variable defined in file scope without type qualifier. In another file, say f1.c, this variable is declared with two qualifiers added, and in the third file f2.c, declared with only one qualifier added. Will the variable act just as what they are declared in each...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6036
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.