machine.SPI.rst 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. currentmodule:: machine
  2. .. _machine.SPI:
  3. class SPI -- a Serial Peripheral Interface bus protocol (master side)
  4. =====================================================================
  5. SPI is a synchronous serial protocol that is driven by a master. At the
  6. physical level, a bus consists of 3 lines: SCK, MOSI, MISO. Multiple devices
  7. can share the same bus. Each device should have a separate, 4th signal,
  8. SS (Slave Select), to select a particular device on a bus with which
  9. communication takes place. Management of an SS signal should happen in
  10. user code (via machine.Pin class).
  11. Constructors
  12. ------------
  13. .. class:: SPI(id, ...)
  14. Construct an SPI object on the given bus, ``id``. Values of ``id`` depend
  15. on a particular port and its hardware. Values 0, 1, etc. are commonly used
  16. to select hardware SPI block #0, #1, etc. Value -1 can be used for
  17. bitbanging (software) implementation of SPI (if supported by a port).
  18. With no additional parameters, the SPI object is created but not
  19. initialised (it has the settings from the last initialisation of
  20. the bus, if any). If extra arguments are given, the bus is initialised.
  21. See ``init`` for parameters of initialisation.
  22. Methods
  23. -------
  24. .. method:: SPI.init(baudrate=1000000, \*, polarity=0, phase=0, bits=8, firstbit=SPI.MSB, sck=None, mosi=None, miso=None, pins=(SCK, MOSI, MISO))
  25. Initialise the SPI bus with the given parameters:
  26. - ``baudrate`` is the SCK clock rate.
  27. - ``polarity`` can be 0 or 1, and is the level the idle clock line sits at.
  28. - ``phase`` can be 0 or 1 to sample data on the first or second clock edge
  29. respectively.
  30. - ``bits`` is the width in bits of each transfer. Only 8 is guaranteed to be supported by all hardware.
  31. - ``firstbit`` can be ``SPI.MSB`` or ``SPI.LSB``.
  32. - ``sck``, ``mosi``, ``miso`` are pins (machine.Pin) objects to use for bus signals. For most
  33. hardware SPI blocks (as selected by ``id`` parameter to the constructor), pins are fixed
  34. and cannot be changed. In some cases, hardware blocks allow 2-3 alternative pin sets for
  35. a hardware SPI block. Arbitrary pin assignments are possible only for a bitbanging SPI driver
  36. (``id`` = -1).
  37. - ``pins`` - WiPy port doesn't ``sck``, ``mosi``, ``miso`` arguments, and instead allows to
  38. specify them as a tuple of ``pins`` parameter.
  39. .. method:: SPI.deinit()
  40. Turn off the SPI bus.
  41. .. method:: SPI.read(nbytes, write=0x00)
  42. Read a number of bytes specified by ``nbytes`` while continuously writing
  43. the single byte given by ``write``.
  44. Returns a ``bytes`` object with the data that was read.
  45. .. method:: SPI.readinto(buf, write=0x00)
  46. Read into the buffer specified by ``buf`` while continuously writing the
  47. single byte given by ``write``.
  48. Returns ``None``.
  49. Note: on WiPy this function returns the number of bytes read.
  50. .. method:: SPI.write(buf)
  51. Write the bytes contained in ``buf``.
  52. Returns ``None``.
  53. Note: on WiPy this function returns the number of bytes written.
  54. .. method:: SPI.write_readinto(write_buf, read_buf)
  55. Write the bytes from ``write_buf`` while reading into ``read_buf``. The
  56. buffers can be the same or different, but both buffers must have the
  57. same length.
  58. Returns ``None``.
  59. Note: on WiPy this function returns the number of bytes written.
  60. Constants
  61. ---------
  62. .. data:: SPI.MASTER
  63. for initialising the SPI bus to master; this is only used for the WiPy
  64. .. data:: SPI.MSB
  65. set the first bit to be the most significant bit
  66. .. data:: SPI.LSB
  67. set the first bit to be the least significant bit