473,480 Members | 2,157 Online
Bytes | Software Development & Data Engineering Community
Create 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_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
4 2488
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
19212
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...
17
2303
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...
22
2242
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...
14
7419
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: ...
19
7183
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,...
10
2874
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...
16
3856
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...
23
4053
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...
1
1198
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...
0
6920
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...
0
7106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5365
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,...
1
4799
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...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
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...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.