2016-06-07 22:46:27 +01:00
|
|
|
.. currentmodule:: machine
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
class SPI -- a Serial Peripheral Interface bus protocol
|
|
|
|
=======================================================
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
SPI is a serial protocol that is driven by a master. At the physical level,
|
|
|
|
bus consistens of 3 lines: SCK, MOSI, MISO. Multiple devices can share the
|
|
|
|
same bus. Each device should have a separate, 4th signal, SS (Slave Select),
|
|
|
|
to select a particualr device on a bus with which communication takes place.
|
|
|
|
Management of an SS signal should happen in user code (via machine.Pin class).
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. only:: port_wipy
|
|
|
|
|
|
|
|
See usage model of I2C; SPI is very similar. Main difference is
|
|
|
|
parameters to init the SPI bus::
|
|
|
|
|
|
|
|
from machine import SPI
|
|
|
|
spi = SPI(0, mode=SPI.MASTER, baudrate=1000000, polarity=0, phase=0, firstbit=SPI.MSB)
|
|
|
|
|
|
|
|
Only required parameter is mode, must be SPI.MASTER. 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.
|
|
|
|
|
|
|
|
Constructors
|
|
|
|
------------
|
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
.. class:: SPI(id, ...)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
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. Value -1 can be used for
|
|
|
|
bitbanging (software) implementation of SPI (if supported by a port).
|
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
|
|
|
|
|
|
|
Methods
|
|
|
|
-------
|
|
|
|
|
2016-10-17 16:05:16 +01:00
|
|
|
.. method:: SPI.init(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, pins=(CLK, MOSI, MISO), sck=None, mosi=None, miso=None)
|
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-17 16:05:16 +01:00
|
|
|
- ``bits`` is the width in bits of each transfer. Only 8 of is guaranteed to be supported by all hardware.
|
|
|
|
- ``firstbit`` can be ``SPI.MSB`` or ``SPI.LSB``.
|
|
|
|
- ``pins`` is an optional tuple with the pins to assign to the SPI bus (deprecated, only for WiPy).
|
|
|
|
- ``sck``, ``mosi``, ``miso`` are pins (machine.Pin) objects to use for bus signals. For most
|
|
|
|
hardware SPI blocks (as selected by ``id`` parameter to the constructore), 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
|
|
|
|
(``id``=-1).
|
2015-10-14 11:32:01 +01:00
|
|
|
|
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
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. data:: SPI.MASTER
|
|
|
|
|
2016-10-18 00:14:26 +01:00
|
|
|
for initialising the SPI bus to master; this is only used for the WiPy
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. data:: SPI.MSB
|
|
|
|
|
|
|
|
set the first bit to be the most significant bit
|
2016-10-17 16:05:16 +01:00
|
|
|
|
|
|
|
.. data:: SPI.LSB
|
|
|
|
|
|
|
|
set the first bit to be the least significant bit
|