You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.9 KiB

  1. # The MIT License (MIT)
  2. #
  3. # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. # THE SOFTWARE.
  22. # pylint: disable=too-few-public-methods
  23. """
  24. `adafruit_bus_device.spi_device` - SPI Bus Device
  25. ====================================================
  26. """
  27. __version__ = "2.2.2"
  28. __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BusDevice.git"
  29. class SPIDevice:
  30. """
  31. Represents a single SPI device and manages locking the bus and the device
  32. address.
  33. :param ~busio.SPI spi: The SPI bus the device is on
  34. :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the
  35. DigitalInOut API.
  36. :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high.
  37. (Used for SD cards.)
  38. .. note:: This class is **NOT** built into CircuitPython. See
  39. :ref:`here for install instructions <bus_device_installation>`.
  40. Example:
  41. .. code-block:: python
  42. import busio
  43. import digitalio
  44. from board import *
  45. from adafruit_bus_device.spi_device import SPIDevice
  46. with busio.SPI(SCK, MOSI, MISO) as spi_bus:
  47. cs = digitalio.DigitalInOut(D10)
  48. device = SPIDevice(spi_bus, cs)
  49. bytes_read = bytearray(4)
  50. # The object assigned to spi in the with statements below
  51. # is the original spi_bus object. We are using the busio.SPI
  52. # operations busio.SPI.readinto() and busio.SPI.write().
  53. with device as spi:
  54. spi.readinto(bytes_read)
  55. # A second transaction
  56. with device as spi:
  57. spi.write(bytes_read)
  58. """
  59. def __init__(self, spi, chip_select=None, *,
  60. baudrate=100000, polarity=0, phase=0, extra_clocks=0):
  61. self.spi = spi
  62. self.baudrate = baudrate
  63. self.polarity = polarity
  64. self.phase = phase
  65. self.extra_clocks = extra_clocks
  66. self.chip_select = chip_select
  67. if self.chip_select:
  68. self.chip_select.switch_to_output(value=True)
  69. def __enter__(self):
  70. while not self.spi.try_lock():
  71. pass
  72. self.spi.configure(baudrate=self.baudrate, polarity=self.polarity,
  73. phase=self.phase)
  74. if self.chip_select:
  75. self.chip_select.value = False
  76. return self.spi
  77. def __exit__(self, *exc):
  78. if self.chip_select:
  79. self.chip_select.value = True
  80. if self.extra_clocks > 0:
  81. buf = bytearray(1)
  82. buf[0] = 0xff
  83. clocks = self.extra_clocks // 8
  84. if self.extra_clocks % 8 != 0:
  85. clocks += 1
  86. for _ in range(clocks):
  87. self.spi.write(buf)
  88. self.spi.unlock()
  89. return False