adafruit_bus_device
– Hardware accelerated external bus access¶
The I2CDevice and SPIDevice helper classes make managing transaction state on a bus easy. For example, they manage locking the bus to prevent other concurrent access. For SPI devices, it manages the chip select and protocol changes such as mode. For I2C, it manages the device address.
Available on these boards
- class adafruit_bus_device.I2CDevice(i2c: busio.I2C, device_address: int, probe: bool = True)¶
I2C Device Manager
Represents a single I2C device and manages locking the bus and the device address.
- Paramètres:
Example:
import busio from board import * from adafruit_bus_device.i2c_device import I2CDevice with busio.I2C(SCL, SDA) as i2c: device = I2CDevice(i2c, 0x70) bytes_read = bytearray(4) with device: device.readinto(bytes_read) # A second transaction with device: device.write(bytes_read)
- readinto(buffer: _typing.WriteableBuffer, *, start: int = 0, end: int = sys.maxsize) None ¶
Read into
buffer
from the device.If
start
orend
is provided, then the buffer will be sliced as ifbuffer[start:end]
were passed. The number of bytes read will be the length ofbuffer[start:end]
.
- write(buffer: _typing.ReadableBuffer, *, start: int = 0, end: int = sys.maxsize) None ¶
Write the bytes from
buffer
to the device, then transmit a stop bit.If
start
orend
is provided, then the buffer will be sliced as ifbuffer[start:end]
were passed, but without copying the data. The number of bytes written will be the length ofbuffer[start:end]
.
- write_then_readinto(out_buffer: _typing.ReadableBuffer, in_buffer: _typing.WriteableBuffer, *, out_start: int = 0, out_end: int = sys.maxsize, in_start: int = 0, in_end: int = sys.maxsize) None ¶
Write the bytes from
out_buffer
to the device, then immediately reads intoin_buffer
from the device.If
out_start
orout_end
is provided, then the buffer will be sliced as ifout_buffer[out_start:out_end]
were passed, but without copying the data. The number of bytes written will be the length ofout_buffer[out_start:out_end]
.If
in_start
orin_end
is provided, then the input buffer will be sliced as ifin_buffer[in_start:in_end]
were passed, The number of bytes read will be the length ofout_buffer[in_start:in_end]
.- Paramètres:
out_buffer (ReadableBuffer) – write out bytes from this buffer
in_buffer (WriteableBuffer) – read bytes into this buffer
out_start (int) – beginning of
out_buffer
sliceout_end (int) – end of
out_buffer
slice; if not specified, uselen(out_buffer)
in_start (int) – beginning of
in_buffer
slicein_end (int) – end of
in_buffer slice
; if not specified, uselen(in_buffer)
- class adafruit_bus_device.SPIDevice(spi: busio.SPI, chip_select: microcontroller.Pin, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, extra_clocks: int = 0)¶
SPI Device Manager
Represents a single SPI device and manages locking the bus and the device address.
- Paramètres:
spi (SPI) – The SPI bus the device is on
chip_select (DigitalInOut) – The chip select pin object that implements the DigitalInOut API.
cs_active_value (bool) – Set to true if your device requires CS to be active high. Defaults to false.
extra_clocks (int) – The minimum number of clock cycles to cycle the bus after CS is high. (Used for SD cards.)
Example:
import busio import digitalio from board import * from adafruit_bus_device.spi_device import SPIDevice with busio.SPI(SCK, MOSI, MISO) as spi_bus: cs = digitalio.DigitalInOut(D10) device = SPIDevice(spi_bus, cs) bytes_read = bytearray(4) # The object assigned to spi in the with statements below # is the original spi_bus object. We are using the busio.SPI # operations busio.SPI.readinto() and busio.SPI.write(). with device as spi: spi.readinto(bytes_read) # A second transaction with device as spi: spi.write(bytes_read)
- __exit__() None ¶
Ends a SPI transaction by deasserting chip select. See Lifetime and ContextManagers for more info.