Easy-Python  1.00.00Change to: 中文
SPI

Functions

 SPI (id, mode, baudrate, polarity, phase)
 Construct an SPI object on the given bus, id. More...
 
 init (mode, baudrate, polarity, phase)
 Initialise the SPI bus with the given parameters: More...
 
 deinit ()
 Turn off the SPI bus. More...
 
 read (nbytes)
 Read a number of bytes specified by nbytes.(for slave mode) More...
 
 readinto (buf)
 Read into the buffer specified by buf.(for slave mode) More...
 
 write (buf)
 Write the bytes contained in buf. More...
 
 write_readinto (write_buf, read_buf)
 Write the bytes from write_buf while reading into read_buf. The buffers can be the same or different, but both buffers must have the same length.(for master mode) More...
 

Detailed Description

SPI_SSP.png
SPI Connection Diagram

The Synchronous Serial Port (SSP) controller can operate on a SPI and 4-wire SSI bus. It interacts with multiple masters and slaves on the bus. Only a single master and a single slave can communicate on the bus during a given data transfer. Data transfers are in principle full duplex, with frames of 4 to 16 bits of data flowing from the master to the slave and from the slave to the master. In practice it is often the case that only one of these data flows carries meaningful data.

Features of the SSP include:

Function Documentation

SPI ( id  ,
mode  ,
baudrate  ,
polarity  ,
phase   
)

Construct an SPI object on the given bus, id.

Parameters
[in]idis the SPI number
  • 0 : select SPI0
  • 1 : select SPI1
[in]modeis the SPI mode
  • SPI.MASTER : act as master
  • SPI.SLAVE : act as slave
[in]baudrateis the SCK clock rate, default is 20000000. Recommendation rate is 20000000, because receive data have distortion in 40000000Hz.
  • 1250000 : baudrate 1250000
  • 2500000 : baudrate 2500000
  • 5000000 : baudrate 5000000
  • 10000000 : baudrate 10000000
  • 20000000 : baudrate 20000000
  • 40000000 : baudrate 40000000
[in]polaritycan be 0 or 1, and is the level the idle clock line sits at, default is 0.
[in]phasecan be 0 or 1 to sample data on the first or second clock edge respectively, default is 0.
Returns
a SPI object
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER,baudrate=20000000,polarity=0,phase=0)
spi1=SPI(1,mode=SPI.SLAVE,baudrate=20000000,polarity=0,phase=0)
init ( mode  ,
baudrate  ,
polarity  ,
phase   
)

Initialise the SPI bus with the given parameters:

Parameters
[in]modeis the SPI mode
  • SPI.MASTER : act as master
  • SPI.SLAVE : act as slave
[in]baudrateis the SCK clock rate, default is 20000000. Recommendation rate is 20000000, because receive data have distortion in 40000000Hz.
  • 1250000 : baudrate 1250000
  • 2500000 : baudrate 2500000
  • 5000000 : baudrate 5000000
  • 10000000 : baudrate 10000000
  • 20000000 : baudrate 20000000
  • 40000000 : baudrate 40000000
[in]polaritycan be 0 or 1, and is the level the idle clock line sits at, default is 0.
[in]phasecan be 0 or 1 to sample data on the first or second clock edge respectively, default is 0.
Returns
(no)
Example of usage:
from machine import SPI
spi.init(0,mode=SPI.MASTER,baudrate=20000000,polarity=0,phase=0) # set SPI as master, rate=20MHz, polarity=0,phase=0
spi.init(1,mode=SPI.SLAVE,baudrate=10000000,polarity=1,phase=1) # set SPI as slave, rate=10MHz, polarity=1,phase=1
deinit ( )

Turn off the SPI bus.

Parameters
no
Returns
(no)
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER) # creat a spi as master
spi0.deinit() # deinit spi
read ( nbytes  )

Read a number of bytes specified by nbytes.(for slave mode)

SPI.read([nbytes])

Parameters
[in]nbytes: slave receive nbytes data from master, nbytes can't be over 4096 bytes.
Returns
a bytes object
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER)
spi1=SPI(1,mode=SPI.SLAVE)
spi0.write('hello')
spi1.read(5) # receive 5 bytes on the bus
readinto ( buf  )

Read into the buffer specified by buf.(for slave mode)

SPI.readinto(buf)

Parameters
[in]buf: slave receive data buf from master, buf size can't be over 4096 bytes.
Returns
the number of bytes read.
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER)
spi1=SPI(1,mode=SPI.SLAVE)
spi0.write('hello')
rbuf = bytearray(5)
spi1.readinto(rbuf) # receive 5 bytes buffer
write ( buf  )

Write the bytes contained in buf.

SPI.write(buf)

Parameters
[in]buf: SPI sends data buf, buf size can't be over 4096 bytes.
Returns
the number of bytes written.
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER)
spi0.write('hello') # send 5 bytes
write_readinto ( write_buf  ,
read_buf   
)

Write the bytes from write_buf while reading into read_buf. The buffers can be the same or different, but both buffers must have the same length.(for master mode)

SPI.write_readinto(write_buf, read_buf)

Parameters
[in]write_buf: master sends data write_buf to slave, buf size can't be over 4096 bytes.
[in]read_buf: master receives data read_buf from slave, buf size can't be over 4096 bytes.
Returns
the number of bytes written.
Example of usage:
from machine import SPI
spi0=SPI(0,mode=SPI.MASTER)
spi1=SPI(1,mode=SPI.SLAVE)
spi1.write('hello') # slave sends 5 bytes
rbuf = bytearray(5)
spi0.write_readinto('00000',rbuf) # master receives 5 bytes