UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Contributor
Contributor
6,128 Views
Registered: ‎09-14-2008

Need some help in C-code

Hello everybody

 

I need some help.

I have an MicroBlaze with some IO-Ports to communicate with VHDL-Components. MB is a component. MB should read some data (at *Baseadress_Data_in_ptr)

 

Here is my C-code:

 

TempStateOut = (TempStateOut | 0x00000001);

*Baseadress_State_out_ptr = TempStateOut; // ReadClk

 

do

{

}

while ((*Baseadress_State_in_ptr)& 0x00000040 != 0x00000040); //DataValid

TempStateOut = (TempStateOut & 0xFFFFFFFE);

*Baseadress_State_out_ptr = TempStateOut;
// /ReadClk

 xil_printf(" "); // Important. But why????                      LINE XX

ucVar = (unsigned char)*Baseadress_Data_in_ptr;

 

If i add LINE XX the program is working properly. But if i comment that line i got some false Data. Can anybody tell me why?

 

 

Sincerely and wish u merry merry Chrismas

 

M.Rapp

0 Kudos
5 Replies
Historian
Historian
6,111 Views
Registered: ‎02-25-2008

Re: Need some help in C-code


m.rapp wrote:

Hello everybody

 

I need some help.

I have an MicroBlaze with some IO-Ports to communicate with VHDL-Components. MB is a component. MB should read some data (at *Baseadress_Data_in_ptr)

 

Here is my C-code:

 

TempStateOut = (TempStateOut | 0x00000001);

*Baseadress_State_out_ptr = TempStateOut; // ReadClk

 

do

{

}

while ((*Baseadress_State_in_ptr)& 0x00000040 != 0x00000040); //DataValid

TempStateOut = (TempStateOut & 0xFFFFFFFE);

*Baseadress_State_out_ptr = TempStateOut;
// /ReadClk

 xil_printf(" "); // Important. But why????                      LINE XX

ucVar = (unsigned char)*Baseadress_Data_in_ptr;

 

If i add LINE XX the program is working properly. But if i comment that line i got some false Data. Can anybody tell me why?

 

 

Sincerely and wish u merry merry Chrismas

 

M.Rapp


 

Perhaps when you put in the xil_printf(), you also add a delay that's long enough for  whatever data you're trying to read to become valid.

 

-a

----------------------------Yes, I do this for a living.
0 Kudos
Explorer
Explorer
6,106 Views
Registered: ‎08-14-2007

Re: Need some help in C-code

I second that, and would also mention that maybe you're changing the state of the system by performing your operation.

 

In other words, the call to xil_printf() takes: (a) time, and (b) resources. So by using time you could be introducing a delay (as previously mentioned) that allows the expected result to occur. By using resources, you could be doing something where a value is 're-read' instead of used off the stack.

 

Let's look at the second one in more depth:

 

Let's say you have the following function:

int b = *(int*)(0xsome_address); int* c = (int*)(0xdifferent_address); *c = b; b = *(int*)(0xsome_address); /* address is the same as before */

 

In this example, B and C could have been allocated on the stack (or registers) and the compiler could assume that the address has not changed, therefore the state at that address hasn't changed and it won't re-read from memory for that value. Which is not what we expected. We wanted to write a value to C, then read the new state from B.

 

Now let's take a look at this example:

int b = *(int*)(0xsome_address); int* c = (int*)(0xdifferent_address); *c = b; do_func(); b = *(int*)(0xsome_address); /* address is the same as before */

 

Once again b and c should be allocated via registers or the stack. With the call to the function in there, now the compiler has to handle getting the value for b again. In this case, it could throw away the stack storage for b and just re-read it into a register after the call to do_func() finishes. This is what we expected to happen.

 

If you want to force the values to always be read, use the 'volatile' keyword in your declaration of a pointer:

 

volatile int *b;

 

This will force the compiler to always check the value at the address upon usage.

 

Professor
Professor
6,099 Views
Registered: ‎08-14-2007

Re: Need some help in C-code

Another point.  The volatile declaration indicates that the object could change value due to some

process not visible to the compiler at the time of build.  So the declaration:

 

volatile int *b;

 

 tells the compiler to read the value pointed to by "b" each time you need to use it.

 

Now let's look at another case where the value of b (not *b but the pointer itself)

can change when the compiler doesn't know about it.  This could happen if

an interrupt routine updates a pointer to point to a new buffer location, for example.

 

Then you need to say that the pointer itself, and not just the data it points to,

is volatile like:

 

int * volatile b;

-- Gabor
0 Kudos
Contributor
Contributor
6,057 Views
Registered: ‎09-14-2008

Re: Need some help in C-code

Hi

Thanks for the answers

 

I tried to declare the pointer to volatile. But it did not work.

I dont know why.

 

It does not matter... I paste the line and OK.

 

 

Merry Christmas

 

M.Rapp

0 Kudos
Professor
Professor
6,044 Views
Registered: ‎08-14-2007

Re: Need some help in C-code

So you have two possibilities:

 

1)  The time that xil_printf takes makes a difference.  If this is the case you need to

make sure that the data is really available when your data valid bit is asserted.

 

2) The compiler has chosen to use an existing copy of the data rather than reading

it fresh.  This could happen if you have written recently to the data in question, or

if you have data cache in your system.  How does the data get to the area you

are reading?  Is it written by external hardware?  Is it updated in an interrupt routine?

Does the value of Baseadress_Data_in_ptr change during normal operation?  Can

you show us the declaration of Baseadress_Data_in_ptr?  If you only declared the

data pointed to by Baseadress_Data_in_ptr volatile, and not the pointer itself, you

may need to change the line:

 

ucVar = (unsigned char)*Baseadress_Data_in_ptr;

 

to:

 

ucVar = (volatile unsigned char)*Baseadress_Data_in_ptr;

 

Regards,

Gabor

-- Gabor
0 Kudos