2016-06-07 22:46:27 +01:00
|
|
|
.. currentmodule:: machine
|
2017-04-18 06:27:37 +01:00
|
|
|
.. _machine.SPI:
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2021-06-12 05:51:05 +01:00
|
|
|
class SPI -- a Serial Peripheral Interface bus protocol (controller side)
|
|
|
|
=========================================================================
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2021-06-12 05:51:05 +01:00
|
|
|
SPI is a synchronous serial protocol that is driven by a controller. At the
|
2016-10-25 15:03:35 +01:00
|
|
|
physical level, a bus consists of 3 lines: SCK, MOSI, MISO. Multiple devices
|
|
|
|
can share the same bus. Each device should have a separate, 4th signal,
|
2021-06-12 05:51:05 +01:00
|
|
|
CS (Chip Select), to select a particular device on a bus with which
|
|
|
|
communication takes place. Management of a CS signal should happen in
|
2016-10-25 15:03:35 +01:00
|
|
|
user code (via machine.Pin class).
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2020-09-29 07:50:23 +01:00
|
|
|
Both hardware and software SPI implementations exist via the
|
|
|
|
:ref:`machine.SPI <machine.SPI>` and `machine.SoftSPI` classes. Hardware SPI uses underlying
|
|
|
|
hardware support of the system to perform the reads/writes and is usually
|
|
|
|
efficient and fast but may have restrictions on which pins can be used.
|
|
|
|
Software SPI is implemented by bit-banging and can be used on any pin but
|
|
|
|
is not as efficient. These classes have the same methods available and
|
|
|
|
differ primarily in the way they are constructed.
|
|
|
|
|
2020-08-31 16:38:20 +01:00
|
|
|
Example usage::
|
|
|
|
|
|
|
|
from machine import SPI, Pin
|
|
|
|
|
|
|
|
spi = SPI(0, baudrate=400000) # Create SPI peripheral 0 at frequency of 400kHz.
|
|
|
|
# Depending on the use case, extra parameters may be required
|
|
|
|
# to select the bus characteristics and/or pins to use.
|
|
|
|
cs = Pin(4, mode=Pin.OUT, value=1) # Create chip-select on pin 4.
|
|
|
|
|
|
|
|
try:
|
|
|
|
cs(0) # Select peripheral.
|
|
|
|
spi.write(b"12345678") # Write 8 bytes, and don't care about received data.
|
|
|
|
finally:
|
|
|
|
cs(1) # Deselect peripheral.
|
|
|
|
|
|
|
|
try:
|
|
|
|
cs(0) # Select peripheral.
|
|
|
|
rxdata = spi.read(8, 0x42) # Read 8 bytes while writing 0x42 for each byte.
|
|
|
|
finally:
|
|
|
|
cs(1) # Deselect peripheral.
|
|
|
|
|
|
|
|
rxdata = bytearray(8)
|
|
|
|
try:
|
|
|
|
cs(0) # Select peripheral.
|
|
|
|
spi.readinto(rxdata, 0x42) # Read 8 bytes inplace while writing 0x42 for each byte.
|
|
|
|
finally:
|
|
|
|
cs(1) # Deselect peripheral.
|
|
|
|
|
|
|
|
txdata = b"12345678"
|
|
|
|
rxdata = bytearray(len(txdata))
|
|
|
|
try:
|
|
|
|
cs(0) # Select peripheral.
|
|
|
|
spi.write_readinto(txdata, rxdata) # Simultaneously write and read bytes.
|
|
|
|
finally:
|
|
|
|
cs(1) # Deselect peripheral.
|
|
|
|
|
2015-10-14 11:32:01 +01:00
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
.. class:: SPI(id, ...)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2020-09-29 07:50:23 +01:00
|
|
|
Construct an SPI object on the given bus, *id*. Values of *id* depend
|
2016-10-17 16:05:16 +01:00
|
|
|
on a particular port and its hardware. Values 0, 1, etc. are commonly used
|
2020-09-29 07:50:23 +01:00
|
|
|
to select hardware SPI block #0, #1, etc.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
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.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2020-09-29 07:50:23 +01:00
|
|
|
.. _machine.SoftSPI:
|
|
|
|
.. class:: SoftSPI(baudrate=500000, *, polarity=0, phase=0, bits=8, firstbit=MSB, sck=None, mosi=None, miso=None)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-10-14 11:32:01 +01:00
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2020-07-11 07:53:26 +01:00
|
|
|
.. method:: SPI.init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO))
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
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.
|
2016-10-24 04:41:21 +01:00
|
|
|
- ``bits`` is the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.
|
2016-10-17 16:05:16 +01:00
|
|
|
- ``firstbit`` can be ``SPI.MSB`` or ``SPI.LSB``.
|
|
|
|
- ``sck``, ``mosi``, ``miso`` are pins (machine.Pin) objects to use for bus signals. For most
|
2016-12-21 02:48:20 +00:00
|
|
|
hardware SPI blocks (as selected by ``id`` parameter to the constructor), pins are fixed
|
2016-10-18 00:14:26 +01:00
|
|
|
and cannot be changed. In some cases, hardware blocks allow 2-3 alternative pin sets for
|
2016-10-17 16:05:16 +01:00
|
|
|
a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver
|
2016-10-25 15:03:35 +01:00
|
|
|
(``id`` = -1).
|
|
|
|
- ``pins`` - WiPy port doesn't ``sck``, ``mosi``, ``miso`` arguments, and instead allows to
|
2017-04-18 06:40:04 +01:00
|
|
|
specify them as a tuple of ``pins`` parameter.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2018-09-20 13:48:52 +01:00
|
|
|
In the case of hardware SPI the actual clock frequency may be lower than the
|
|
|
|
requested baudrate. This is dependant on the platform hardware. The actual
|
|
|
|
rate may be determined by printing the SPI object.
|
|
|
|
|
2016-06-07 23:33:49 +01:00
|
|
|
.. method:: SPI.deinit()
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
Turn off the SPI bus.
|
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
.. method:: SPI.read(nbytes, write=0x00)
|
|
|
|
|
|
|
|
Read a number of bytes specified by ``nbytes`` while continuously writing
|
|
|
|
the single byte given by ``write``.
|
|
|
|
Returns a ``bytes`` object with the data that was read.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
.. method:: SPI.readinto(buf, write=0x00)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
Read into the buffer specified by ``buf`` while continuously writing the
|
|
|
|
single byte given by ``write``.
|
|
|
|
Returns ``None``.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
Note: on WiPy this function returns the number of bytes read.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
.. method:: SPI.write(buf)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
Write the bytes contained in ``buf``.
|
|
|
|
Returns ``None``.
|
|
|
|
|
|
|
|
Note: on WiPy this function returns the number of bytes written.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-06-07 23:33:49 +01:00
|
|
|
.. method:: SPI.write_readinto(write_buf, read_buf)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
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
|
2015-10-14 11:32:01 +01:00
|
|
|
same length.
|
2016-10-18 00:14:26 +01:00
|
|
|
Returns ``None``.
|
|
|
|
|
|
|
|
Note: on WiPy this function returns the number of bytes written.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
2021-06-12 05:51:05 +01:00
|
|
|
.. data:: SPI.CONTROLLER
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2021-06-12 05:51:05 +01:00
|
|
|
for initialising the SPI bus to controller; this is only used for the WiPy
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. data:: SPI.MSB
|
2021-09-22 14:47:27 +01:00
|
|
|
SoftSPI.MSB
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
set the first bit to be the most significant bit
|
2016-10-17 16:05:16 +01:00
|
|
|
|
|
|
|
.. data:: SPI.LSB
|
2021-09-22 14:47:27 +01:00
|
|
|
SoftSPI.LSB
|
2016-10-17 16:05:16 +01:00
|
|
|
|
|
|
|
set the first bit to be the least significant bit
|