machine.SPI
Classes
Construct an SPI object on the given bus, id. Values of id depend |
|
Construct a new software SPI object. Additional parameters must be |
Module Contents
- class machine.SPI.SPI(id, *args, **kwargs)
Construct an SPI object on the given bus, id. Values of id depend on a particular port and its hardware. Values 0, 1, etc. are commonly used to select hardware SPI block #0, #1, etc.
With no additional parameters, the SPI object is created but not initialised (it has the settings from the last initialisation of the bus, if any). If extra arguments are given, the bus is initialised. See
init
for parameters of initialisation.- init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=MSB, sck=None, mosi=None, miso=None, pins: Tuple | None) None
Initialise the SPI bus with the given parameters:
baudrate
is the SCK clock rate.polarity
can be 0 or 1, and is the level the idle clock line sits at.phase
can be 0 or 1 to sample data on the first or second clock edge respectively.bits
is the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.firstbit
can beSPI.MSB
orSPI.LSB
.sck
,mosi
,miso
are pins (machine.Pin) objects to use for bus signals. For most hardware SPI blocks (as selected byid
parameter to the constructor), pins are fixed and cannot be changed. In some cases, hardware blocks allow 2-3 alternative pin sets for a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver (id
= -1).pins
- WiPy port doesn’tsck
,mosi
,miso
arguments, and instead allows to specify them as a tuple ofpins
parameter.
In the case of hardware SPI the actual clock frequency may be lower than the requested baudrate. This is dependent on the platform hardware. The actual rate may be determined by printing the SPI object.
- read(nbytes, write=0) bytes
Read a number of bytes specified by
nbytes
while continuously writing the single byte given bywrite
. Returns abytes
object with the data that was read.
- readinto(buf, write=0) int
Read into the buffer specified by
buf
while continuously writing the single byte given bywrite
. ReturnsNone
.Note: on WiPy this function returns the number of bytes read.
- write(buf) int
Write the bytes contained in
buf
. ReturnsNone
.Note: on WiPy this function returns the number of bytes written.
- write_readinto(write_buf, read_buf) int
Write the bytes from
write_buf
while reading intoread_buf
. The buffers can be the same or different, but both buffers must have the same length. ReturnsNone
.Note: on WiPy this function returns the number of bytes written.
- CONTROLLER: Incomplete
for initialising the SPI bus to controller; this is only used for the WiPy
- LSB: Incomplete
set the first bit to be the least significant bit
- MSB: Incomplete
set the first bit to be the most significant bit
- class machine.SPI.SoftSPI(baudrate=500000, *, polarity=0, phase=0, bits=8, firstbit=MSB, sck=None, mosi=None, miso=None)
Bases:
SPI
Construct a new software SPI object. Additional parameters must be given, usually at least sck, mosi and miso, and these are used to initialise the bus. See
SPI.init
for a description of the parameters.- LSB: Incomplete
set the first bit to be the least significant bit
- MSB: Incomplete
set the first bit to be the most significant bit