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: 
Explorer
Explorer
11,458 Views
Registered: ‎01-18-2014

Read and write DDR memory on Zedboard using C code

Jump to solution

Hi..

 

  I am trying to write and read the entire DDR memory available with the zed board.

  I have used the functions 'Xil_Out16' and 'Xil_In16' for reading and writing to the memory.

 

  The base address used is "XPAR_DDR_MEM_BASEADDR". Since, I am using a 16 bit read and write,

   i am writing to the alternate locations of memory so that both the operations are proper.

 

  I hope that the method is correct.

 

 Now, my problem is, i have written code to write to 100000 locations of memory and then read them. After the 2*98304th  location, the values read are displayed as '0' even though i have written some other values to this locations.

  

 Why is this happening? Is there any problem with the read and write??

 

 Please somebody do reply at the earliest

0 Kudos
1 Solution

Accepted Solutions
Contributor
Contributor
17,510 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I tried to run the program, and it stops working while it iswriting to the ddr. I found that changing the starting address of the memory buffer from XPAR_DDR_MEM_BASEADDR to something like XPAR_DDR_MEM_BASEADDR+0x00300000 solves the problem. After that the program works, writing and reading.

What i understand is that the code of the program is also writen in the DDR, so when you have as a starting address the XPAR_DDR_MEM_BASEADDR, you overwrite it.

 

Moreover, I think you want to change

if (Valuer==65537)

   Value = 0;

to

if (Value==65536)

  Valuer = 0;

since the range of the 16bit integers is 0 to 2^16-1 = 65535

 

or change the type of Value and Valuer to u16 from u32 and omit the comparisons. The variables will rollover on their own when increasing:

...->65534->65535->0->1...

 

 

 

12 Replies
Contributor
Contributor
11,434 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

Hi gayathri90,

 

I think you don't need to use Xil_In16 and Xil_Out16 functions for DDR. You can for example do:

 

u16 *BufferPtr;   //pointer to memory

BufferPtr= (u16 *)BUFFER_BASE_ADDR; 

 

Then you can access to write as an array

BufferPtr[i] = 1234;

or as pointer

*(BufferPtr+i)  = 1234;

 

or to read:

temp = BufferPtr[i];

 

Unfortunatelly I don't have any idea about your main question. Maybe you should without the Xil_In and Xil_out functions, or post the part of your code that does the reading and writing

 

Nikos

 

 

Explorer
Explorer
11,423 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

Thank you Nikos for your reply..

 

When i started with this problem, I have searched so much in xilinx forums and found this method of reading and writing.

 

I have tried the way you mentioned and it worked..

 

Previously, when i was using those functions for proper read and write of 16 bits i was writing and reading from alternate locations. Now, i am able to read and write consecutive locations.

 

But still my main problem mentioned in the previous post exist..

 

Thanks again

 

Gayathri

0 Kudos
Contributor
Contributor
11,411 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

hi Gayathri,

 

maybe you could share with us the code that has the problem, for further ideas

 

Nikos

0 Kudos
Explorer
Explorer
11,407 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I have attached my code with this..

0 Kudos
Contributor
Contributor
17,511 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I tried to run the program, and it stops working while it iswriting to the ddr. I found that changing the starting address of the memory buffer from XPAR_DDR_MEM_BASEADDR to something like XPAR_DDR_MEM_BASEADDR+0x00300000 solves the problem. After that the program works, writing and reading.

What i understand is that the code of the program is also writen in the DDR, so when you have as a starting address the XPAR_DDR_MEM_BASEADDR, you overwrite it.

 

Moreover, I think you want to change

if (Valuer==65537)

   Value = 0;

to

if (Value==65536)

  Valuer = 0;

since the range of the 16bit integers is 0 to 2^16-1 = 65535

 

or change the type of Value and Valuer to u16 from u32 and omit the comparisons. The variables will rollover on their own when increasing:

...->65534->65535->0->1...

 

 

 

Explorer
Explorer
11,389 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

Thanks alot Nikos...

 

a few more questions...

 

1) how did you figure out the value XPAR_DDR_MEM_BASEADDR+0x00300000??

 2) Also, how did you know that the code would also be written to the DDR?? 

3) Would it take so  much of time to write and read the entire DDR memory??

 

I am really sorry if the questions are very silly..

Since i am using a DDR memory for the first time i am ignorant of many things..

 

Where can i get a detailed material on these topics??

 

Thanks alot...

 

Gayathri

0 Kudos
Contributor
Contributor
11,369 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I did a small mistake, the program is not stored on the DDR but on the On Chip Memory (OCM).

This memory is normally mapped to the addresses 0x0000_0000 to 0x0003_FFFF.

The constant XPAR_DDR_MEM_BASEADDR is 0x0000_0000.

So when you are writing to this address you are problably writing on OCM.

 

If you look at page 112 of ug585 you will see that. Moreover you will see that the address that is "Accessible to all interconnect masters"  is starting at address 0x0010_0000. I think this is the memory you want and have to use.

 

The address XPAR_DDR_MEM_BASEADDR+0x00300000 was just an example but as stated above, it must be something above XPAR_DDR_MEM_BASEADDR+0x00100000 to access the DDR.

 

 

You didn't tell me, did the code work?

 

ps. I am not an expert on this, these are just thinks I figured out. I may be a bit wrong on some :)

0 Kudos
Explorer
Explorer
11,366 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution
I have kept the code for running..
How much time did it take for you to write to the entire memory??
0 Kudos
Contributor
Contributor
11,356 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I didn't try to write to the entire memory

0 Kudos
Explorer
Explorer
6,686 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I tried writing from 0x00100000 to 0x03FFFFFF.

 

After writing to a few locations my for loop gets stuck.

 

I am printing the value of 'i' and its stops printing after 835.

What would be the reason??

 

#include "stdio.h"
#include "math.h"
#include "xparameters.h"
#include "xgpio.h"
#include "xil_io.h"

int main(void)
{
//u16 *base_address;
//base_address = (u16 *) XPAR_PS7_DDR_0_S_AXI_BASEADDR;
u32 write_value = 255;
u32 read_value ;

int i,j;
u32 base_address = XPAR_DDR_MEM_BASEADDR+0x00100000;

//u32 high_address =(XPAR_DDR_MEM_HIGHADDR+1)/4;
u32 high_address = base_address + 100000;

xil_printf("High address = %d\n", high_address);
// To write to the DDR memory
xil_printf("Writing to memory.................................................\n");
for(i=0;i<high_address;i++)
{
Xil_Out32(base_address+(4*i),write_value);
xil_printf("%d\n", i);
}
//To read from the DDR memory
xil_printf("Reading from memory................................................\n");
for(j=0;j<high_address;j++)
{
read_value = Xil_In32(base_address+(4*j));
//xil_printf("Value read from location %d = %d\n",base_address+j, read_value);
}
xil_printf("Read completed..........................................\n");
return(0);
}

 

0 Kudos
Contributor
Contributor
6,673 Views
Registered: ‎03-11-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

I run the program you posted and it got stuck for me too.

 

Try to run with  
u32 base_address = XPAR_DDR_MEM_BASEADDR+0x00200000;

instead of +0x00100000

and it runs ok

 

What I think that is happening is that you are overwriting some of the variables of the program.

 

I added the following line after the declaration of the variables:

 

xil_printf("%x %x %x %x\n", &write_value, &read_value, &base_address, &high_address);

j

ust to see the addresses of these variables. What I got was:

 

10C04C 10C048 10C044 10C040 

 

As you can see these addresses are inside the buffer you are trying to write.

 

If you  change the starting address of the buffer, you will hopefully have no problem.

 

 

Explorer
Explorer
6,664 Views
Registered: ‎01-18-2014

Re: Read and write DDR memory on Zedboard using C code

Jump to solution

Thank you nmarinos..

 

:)

0 Kudos