aboutsummaryrefslogtreecommitdiff
path: root/docs/library/machine.SPI.rst
blob: 7c6627614a60001243eaa7b4fbc920a5d0f77bb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
.. currentmodule:: machine

class SPI -- a Serial Peripheral Interface bus protocol
=======================================================

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).

.. 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
------------

.. class:: SPI(id, ...)

   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).

   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.

Methods
-------

.. method:: SPI.init(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, pins=(CLK, MOSI, MISO), sck=None, mosi=None, miso=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 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
       and cannot be changed. In some cases, hardware blocks allow 2-3 alterbative pin sets for
       a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver
       (``id``=-1).

.. method:: SPI.deinit()

   Turn off the SPI bus.

.. method:: SPI.write(buf)

    Write the data contained in ``buf``. 
    Returns the number of bytes written.

.. method:: SPI.read(nbytes, *, write=0x00)

    Read the ``nbytes`` while writing the data specified by ``write``.
    Return the number of bytes read.

.. method:: SPI.readinto(buf, *, write=0x00)

    Read into the buffer specified by ``buf`` while writing the data specified by
    ``write``.
    Return the number of bytes read.

.. method:: SPI.write_readinto(write_buf, read_buf)

    Write from ``write_buf`` and read into ``read_buf``. Both buffers must have the
    same length.
    Returns the number of bytes written.

Constants
---------

.. data:: SPI.MASTER

   for initialising the SPI bus to master

.. data:: SPI.MSB

   set the first bit to be the most significant bit

.. data:: SPI.LSB

   set the first bit to be the least significant bit