Represents a single SPI device and manages locking the bus and the device address. :param ~busio.SPI spi: The SPI bus the device is on :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the DigitalInOut API. :param bool cs_acti
| 13 | """ |
| 14 | |
| 15 | class SPIDevice: |
| 16 | """ |
| 17 | Represents a single SPI device and manages locking the bus and the device |
| 18 | address. |
| 19 | |
| 20 | :param ~busio.SPI spi: The SPI bus the device is on |
| 21 | :param ~digitalio.DigitalInOut chip_select: The chip select pin object that implements the |
| 22 | DigitalInOut API. |
| 23 | :param bool cs_active_value: Set to true if your device requires CS to be active high. |
| 24 | Defaults to false. |
| 25 | :param int baudrate: The SPI baudrate |
| 26 | :param int polarity: The SPI polarity |
| 27 | :param int phase: The SPI phase |
| 28 | :param int extra_clocks: The minimum number of clock cycles to cycle the bus after CS is high. |
| 29 | (Used for SD cards.) |
| 30 | |
| 31 | .. note:: This class is **NOT** built into CircuitPython. See |
| 32 | :ref:`here for install instructions <bus_device_installation>`. |
| 33 | |
| 34 | Example: |
| 35 | |
| 36 | .. code-block:: python |
| 37 | |
| 38 | import busio |
| 39 | import digitalio |
| 40 | from board import * |
| 41 | from adafruit_bus_device.spi_device import SPIDevice |
| 42 | |
| 43 | with busio.SPI(SCK, MOSI, MISO) as spi_bus: |
| 44 | cs = digitalio.DigitalInOut(D10) |
| 45 | device = SPIDevice(spi_bus, cs) |
| 46 | bytes_read = bytearray(4) |
| 47 | # The object assigned to spi in the with statements below |
| 48 | # is the original spi_bus object. We are using the busio.SPI |
| 49 | # operations busio.SPI.readinto() and busio.SPI.write(). |
| 50 | with device as spi: |
| 51 | spi.readinto(bytes_read) |
| 52 | # A second transaction |
| 53 | with device as spi: |
| 54 | spi.write(bytes_read) |
| 55 | """ |
| 56 | |
| 57 | def __init__( |
| 58 | self, |
| 59 | spi: SPI, |
| 60 | chip_select: Optional[DigitalInOut] = None, |
| 61 | *, |
| 62 | cs_active_value: bool = False, |
| 63 | baudrate: int = 100000, |
| 64 | polarity: int = 0, |
| 65 | phase: int = 0, |
| 66 | extra_clocks: int = 0 |
| 67 | ) -> None: |
| 68 | self.spi = spi |
| 69 | self.baudrate = baudrate |
| 70 | self.polarity = polarity |
| 71 | self.phase = phase |
| 72 | self.extra_clocks = extra_clocks |