+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Junior Member level 1
    Points: 124, Level: 1

    Join Date
    Jul 2019
    Posts
    19
    Helped
    0 / 0
    Points
    124
    Level
    1

    SPI communication in SDK

    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!

    •   AltAdvertisement

        
       

  2. #2
    Newbie level 6
    Points: 611, Level: 5

    Join Date
    Nov 2016
    Location
    ShengXiaLu
    Posts
    12
    Helped
    0 / 0
    Points
    611
    Level
    5

    Re: SPI communication in SDK

    The sdk has example code, you can reference it.



  3. #3
    Junior Member level 1
    Points: 124, Level: 1

    Join Date
    Jul 2019
    Posts
    19
    Helped
    0 / 0
    Points
    124
    Level
    1

    Re: SPI communication in SDK

    @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.



  4. #4
    Newbie level 6
    Points: 611, Level: 5

    Join Date
    Nov 2016
    Location
    ShengXiaLu
    Posts
    12
    Helped
    0 / 0
    Points
    611
    Level
    5

    Re: SPI communication in SDK

    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.



    •   AltAdvertisement

        
       

  5. #5
    Junior Member level 1
    Points: 124, Level: 1

    Join Date
    Jul 2019
    Posts
    19
    Helped
    0 / 0
    Points
    124
    Level
    1

    Re: SPI communication in SDK

    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.



  6. #6
    Newbie level 4
    Points: 44, Level: 1

    Join Date
    Sep 2019
    Location
    Germany
    Posts
    7
    Helped
    0 / 0
    Points
    44
    Level
    1

    Re: SPI communication in SDK

    Quote Originally Posted by Roronoa137 View Post
    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.



  7. #7
    Junior Member level 1
    Points: 124, Level: 1

    Join Date
    Jul 2019
    Posts
    19
    Helped
    0 / 0
    Points
    124
    Level
    1

    Re: SPI communication in SDK

    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.



    •   AltAdvertisement

        
       

  8. #8
    Newbie level 6
    Points: 611, Level: 5

    Join Date
    Nov 2016
    Location
    ShengXiaLu
    Posts
    12
    Helped
    0 / 0
    Points
    611
    Level
    5

    Re: SPI communication in SDK

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



  9. #9
    Junior Member level 1
    Points: 124, Level: 1

    Join Date
    Jul 2019
    Posts
    19
    Helped
    0 / 0
    Points
    124
    Level
    1

    Re: SPI communication in SDK

    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.



  10. #10
    Newbie level 6
    Points: 611, Level: 5

    Join Date
    Nov 2016
    Location
    ShengXiaLu
    Posts
    12
    Helped
    0 / 0
    Points
    611
    Level
    5

    Re: SPI communication in SDK

    Quote Originally Posted by Roronoa137 View Post
    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\XilinxProc essorIPLib\drivers\spi_v4_2\examples\xspi_atmel_fl ash_example.c' examle to check it. By the way, the hardware need an interrupt IP, too.



--[[ ]]--