User Guide

175
The xfer() method accepts one argument, and that is an array of
bytes. When you call this method, the slave select pin is brought
low and then it sends the values in the byte array to the SPI
device. The clock signal is generated for you.
To write a byte to the 23K640, you send the WRITE command
(2), followed by two bytes that form a 16-bit address, then the
value to be stored (8):
[2,0,0,8]
You also use the xfer() method to read from an SPI device. The
slave device expects the master to generate the clock pulses that
it needs to transmit data. So you may need to include extra bytes
in the call to xfer().
For example, if an SPI device expects the master to send the
sequence 3,0,0 before it sends back a byte then you would pass
an extra 0 into xfer(). This generates the additional clock signals
for the value that the device is sending.
spi.xfer([3,0,0,0])
The code below is a short, but complete, example of how to use
SPI to read and write to a 23K640 SRAM.
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
def Read23K640(addr1, addr2):
vals = spi.xfer([3, addr1, addr2, 0])
return vals[3]
def Write23K640(addr1, addr2, value):
spi.xfer([2, addr1, addr2, value])
Write23K640(0, 0, 8)
print(Read23K640(0, 0))