pyb.SPI.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. .. currentmodule:: pyb
  2. .. _pyb.SPI:
  3. class SPI -- a master-driven serial protocol
  4. ============================================
  5. SPI is a serial protocol that is driven by a master. At the physical level
  6. there are 3 lines: SCK, MOSI, MISO.
  7. See usage model of I2C; SPI is very similar. Main difference is
  8. parameters to init the SPI bus::
  9. from pyb import SPI
  10. spi = SPI(1, SPI.MASTER, baudrate=600000, polarity=1, phase=0, crc=0x7)
  11. Only required parameter is mode, SPI.MASTER or SPI.SLAVE. Polarity can be
  12. 0 or 1, and is the level the idle clock line sits at. Phase can be 0 or 1
  13. to sample data on the first or second clock edge respectively. Crc can be
  14. None for no CRC, or a polynomial specifier.
  15. Additional methods for SPI::
  16. data = spi.send_recv(b'1234') # send 4 bytes and receive 4 bytes
  17. buf = bytearray(4)
  18. spi.send_recv(b'1234', buf) # send 4 bytes and receive 4 into buf
  19. spi.send_recv(buf, buf) # send/recv 4 bytes from/to buf
  20. Constructors
  21. ------------
  22. .. class:: pyb.SPI(bus, ...)
  23. Construct an SPI object on the given bus. ``bus`` can be 1 or 2, or
  24. 'X' or 'Y'. With no additional parameters, the SPI object is created but
  25. not initialised (it has the settings from the last initialisation of
  26. the bus, if any). If extra arguments are given, the bus is initialised.
  27. See ``init`` for parameters of initialisation.
  28. The physical pins of the SPI busses are:
  29. - ``SPI(1)`` is on the X position: ``(NSS, SCK, MISO, MOSI) = (X5, X6, X7, X8) = (PA4, PA5, PA6, PA7)``
  30. - ``SPI(2)`` is on the Y position: ``(NSS, SCK, MISO, MOSI) = (Y5, Y6, Y7, Y8) = (PB12, PB13, PB14, PB15)``
  31. At the moment, the NSS pin is not used by the SPI driver and is free
  32. for other use.
  33. Methods
  34. -------
  35. .. method:: SPI.deinit()
  36. Turn off the SPI bus.
  37. .. method:: SPI.init(mode, baudrate=328125, \*, prescaler, polarity=1, phase=0, bits=8, firstbit=SPI.MSB, ti=False, crc=None)
  38. Initialise the SPI bus with the given parameters:
  39. - ``mode`` must be either ``SPI.MASTER`` or ``SPI.SLAVE``.
  40. - ``baudrate`` is the SCK clock rate (only sensible for a master).
  41. - ``prescaler`` is the prescaler to use to derive SCK from the APB bus frequency;
  42. use of ``prescaler`` overrides ``baudrate``.
  43. - ``polarity`` can be 0 or 1, and is the level the idle clock line sits at.
  44. - ``phase`` can be 0 or 1 to sample data on the first or second clock edge
  45. respectively.
  46. - ``bits`` can be 8 or 16, and is the number of bits in each transferred word.
  47. - ``firstbit`` can be ``SPI.MSB`` or ``SPI.LSB``.
  48. - ``crc`` can be None for no CRC, or a polynomial specifier.
  49. Note that the SPI clock frequency will not always be the requested baudrate.
  50. The hardware only supports baudrates that are the APB bus frequency
  51. (see :meth:`pyb.freq`) divided by a prescaler, which can be 2, 4, 8, 16, 32,
  52. 64, 128 or 256. SPI(1) is on AHB2, and SPI(2) is on AHB1. For precise
  53. control over the SPI clock frequency, specify ``prescaler`` instead of
  54. ``baudrate``.
  55. Printing the SPI object will show you the computed baudrate and the chosen
  56. prescaler.
  57. .. method:: SPI.recv(recv, \*, timeout=5000)
  58. Receive data on the bus:
  59. - ``recv`` can be an integer, which is the number of bytes to receive,
  60. or a mutable buffer, which will be filled with received bytes.
  61. - ``timeout`` is the timeout in milliseconds to wait for the receive.
  62. Return value: if ``recv`` is an integer then a new buffer of the bytes received,
  63. otherwise the same buffer that was passed in to ``recv``.
  64. .. method:: SPI.send(send, \*, timeout=5000)
  65. Send data on the bus:
  66. - ``send`` is the data to send (an integer to send, or a buffer object).
  67. - ``timeout`` is the timeout in milliseconds to wait for the send.
  68. Return value: ``None``.
  69. .. method:: SPI.send_recv(send, recv=None, \*, timeout=5000)
  70. Send and receive data on the bus at the same time:
  71. - ``send`` is the data to send (an integer to send, or a buffer object).
  72. - ``recv`` is a mutable buffer which will be filled with received bytes.
  73. It can be the same as ``send``, or omitted. If omitted, a new buffer will
  74. be created.
  75. - ``timeout`` is the timeout in milliseconds to wait for the receive.
  76. Return value: the buffer with the received bytes.
  77. Constants
  78. ---------
  79. .. data:: SPI.MASTER
  80. .. data:: SPI.SLAVE
  81. for initialising the SPI bus to master or slave mode
  82. .. data:: SPI.LSB
  83. .. data:: SPI.MSB
  84. set the first bit to be the least or most significant bit