I'm trying to 32 bits data send and receive between Pi and FPGA using spi. So, I set the Pi as master and FPGA as slave. And use xfer2 function from spidev. But, I couldn't read data. How can I fix it?.
First 4 bytes for deciding read or write cycle. And last 4 bytes are the data to transfer to FPGA. For example, if the first 4 bytes are 0x00000000, then this is write cycle. And, last 4 bytes are transferred to FPGA. So, I want to send data by using xfer2. like this, xfer2([0x00000000, 0x000000ff]). But, it didn't work. So, I thought that xfer2 only transfer data 1-byte per. And, I changed it to xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff]).
And, whatever data I sent, this code just printed out [0, 0, 0, 0, 0, 0, 0, 0].
My simple spi test code is below.
import spidev
import time
spi=spidev.SpiDev()
spi.open(0,1)
spi.max_speed_hz= 190000000
while True:
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40]) # write
time.sleep(0.1)
resp = spi.xfer2([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]) # read
print(resp)
time.sleep(0.1)
resp1 = spi.xfer2([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]) # write
time.sleep(0.1)
This is the results
User contributions licensed under CC BY-SA 3.0