Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SPI communication in SDK

Status
Not open for further replies.

Roronoa137

Junior Member level 1
Joined
Jul 26, 2019
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
165
Hi, I'm trying to write the code for SPI communication in SDK.

So I was following these steps:
a) Initialize the SPI driver
b) Perform a self-test
c) Set the SPI device options
d) Select the device on the SPI bus (SPI slave)
e) Start the SPI driver
f) Write data
g) Read the data

I reached step e), but I don't exactly how to proceed for the last two steps.

I am using an SPI QUAD AXI block in the PL connected to an external device and I would like to implement in the PS code the code to send/receive data from/to the external device through the SPI protocol.

I would like to implement two separate functions:
1) send data to the external device via SPI protocol
2) read the data from the external device via SPI protocol.

I discovered that the Spi_Transfer function provides data transfers on the SPI bus, and in particular:
1) XSpi_Transfer (InstancePtr, SendBuf, NULL, ByteCount) should be used if I only want to send data
2) XSpi_Transfer (InstancePtr, RecvBuf, RecvBuf, ByteCount) should be used if I just want to read the data

So I wrote my functions:

1)
Code:
int WriteData (XSpi * SpiInstancePtr, u8 * SpiWriteBuffer, u8 * SpiReadBuffer, u16 ByteCount) {
   int Status;
   u32 Count;
   u8 Test = 0x07;

   for (Count = 0; Count <SPI_BUFFER_SIZE; Count ++) {
      SpiWriteBuffer [Count] = (u8) (Count + Test);
      xil_printf ("WriteBuffer [% d] =% d r", Count, SpiWriteBuffer [Count]);
      xil_printf ("RdBuff% d =% x r", Count, ReadBuffer [Count]);
   }

   Status = XSpi_Transfer (SpiInstancePtr, SpiWriteBuffer, NULL, SPI_BUFFER_SIZE);
   if (Status! = XST_SUCCESS) {
      xil_printf ("Transfer_failure n");
      return XST_FAILURE;
   }
   return XST_SUCCESS;
}


2)
Code:
int ReadTransfer (XSpi * SpiInstancePtr, u8 * SpiWriteBuffer, u8 * SpiReadBuffer, u16 ByteCount) {

   int Status;

   Status = XSpi_Transfer (SpiInstancePtr, SpiReadBuffer, SpiReadBuffer, SPI_BUFFER_SIZE);
      if (Status! = XST_SUCCESS) {
      return XST_FAILURE;
   }

   return XST_SUCCESS;
}

Is this the correct way to proceed?
Also, is there a way to check if the transmission was completed?

If anyone could clarify my doubts, I would be really grateful! Thanks!
 

@luoyanghero thanks for your replay, but I have already took a look to the SDK examples, but the only formt in which they use the Spi_Transfer function is: XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE), but from what I have seen there are no examples related to the other syntax I posted in the above post.
 

which version vivado do you use?
In the vivado install directory, sdk/xxx/processorlib/driver/spi_xx
there are spi read write example functions. The dirrcory name is not completely right, you need search keyword.
 

I'm using vivado 2018.3. I took a look to the example spi_v4.4, which is the interrupt example, but as I wrote above, it's using XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE); but what I wanted to do is to implement two different functions, one for the read and the other one for the write transfer.
 

Also, is there a way to check if the transmission was completed?

I'm not sure what you mean. Doesn't the function provided by SDK check for completion of the transfer? If you're trying to check if the data was correctly written to the destination, then you'll have to read it back and compare.
 

Thanks for your replay. I just wanted to check if all the data were transmitted, and actually the SDK example provides it. So this is fine, but I'm still checking for the write and read separate functions.
 

you need a flash device,and connect to your fpga board.
An oscilloscope maybe also need.
 

I don't want to use the flash, I just want to store them in the fifo inside the AXI Quad SPI block and actually if you go through the xspi.h library on GitHub it says that the function Spi_Transfer can be used for read and write, only read and only write. It's just that they don't provide examples for separate read and write.
 

Hi, I'm trying to write the code for SPI communication in SDK.

So I was following these steps:
a) Initialize the SPI driver
b) Perform a self-test
c) Set the SPI device options
d) Select the device on the SPI bus (SPI slave)
e) Start the SPI driver
f) Write data
g) Read the data

I reached step e), but I don't exactly how to proceed for the last two steps.

I am using an SPI QUAD AXI block in the PL connected to an external device and I would like to implement in the PS code the code to send/receive data from/to the external device through the SPI protocol.

I would like to implement two separate functions:
1) send data to the external device via SPI protocol
2) read the data from the external device via SPI protocol.

I discovered that the Spi_Transfer function provides data transfers on the SPI bus, and in particular:
1) XSpi_Transfer (InstancePtr, SendBuf, NULL, ByteCount) should be used if I only want to send data
2) XSpi_Transfer (InstancePtr, RecvBuf, RecvBuf, ByteCount) should be used if I just want to read the data

So I wrote my functions:

1)
Code:
int WriteData (XSpi * SpiInstancePtr, u8 * SpiWriteBuffer, u8 * SpiReadBuffer, u16 ByteCount) {
   int Status;
   u32 Count;
   u8 Test = 0x07;

   for (Count = 0; Count <SPI_BUFFER_SIZE; Count ++) {
      SpiWriteBuffer [Count] = (u8) (Count + Test);
      xil_printf ("WriteBuffer [% d] =% d r", Count, SpiWriteBuffer [Count]);
      xil_printf ("RdBuff% d =% x r", Count, ReadBuffer [Count]);
   }

   Status = XSpi_Transfer (SpiInstancePtr, SpiWriteBuffer, NULL, SPI_BUFFER_SIZE);
   if (Status! = XST_SUCCESS) {
      xil_printf ("Transfer_failure n");
      return XST_FAILURE;
   }
   return XST_SUCCESS;
}


2)
Code:
int ReadTransfer (XSpi * SpiInstancePtr, u8 * SpiWriteBuffer, u8 * SpiReadBuffer, u16 ByteCount) {

   int Status;

   Status = XSpi_Transfer (SpiInstancePtr, SpiReadBuffer, SpiReadBuffer, SPI_BUFFER_SIZE);
      if (Status! = XST_SUCCESS) {
      return XST_FAILURE;
   }

   return XST_SUCCESS;
}

If anyone could clarify my doubts, I would be really grateful! Thanks!
Is this the correct way to proceed?
It is the right way to rpoceed.
Also, is there a way to check if the transmission was completed?
spi flash is device relative, different flash have different config and read/write command. So if you want to use a fifo to simulate a flash, it is difficult.
I suggest that you bought a flash, such as 'Atmel Serial Flash Device (AT45XX series)'. Fllow the '$YourXilinx\SDK\2016.3\data\embeddedsw\XilinxProcessorIPLib\drivers\spi_v4_2\examples\xspi_atmel_flash_example.c' examle to check it. By the way, the hardware need an interrupt IP, too.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top