async_spi – SPI transfers for asyncio
async_spi.SPI is an SPI bus whose transfers are awaited. While a transfer runs, other
asyncio tasks run too.
import asyncio
import board
import async_spi
async def main():
spi = async_spi.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
data = bytearray(512)
async with spi.lock:
spi.configure(baudrate=8_000_000)
await spi.readinto(data)
asyncio.run(main())
Available on these boards
- class async_spi.Lock
A bus lock for asyncio code, used as
async with bus.lock:. Tasks waiting for it get it in the order they asked; a task cancelled while waiting leaves the line.
- class async_spi.SPI(clock: microcontroller.Pin, MOSI: microcontroller.Pin | None = None, MISO: microcontroller.Pin | None = None)
An SPI bus whose transfers are awaited
write,readintoandwrite_readintoreturn awaitables: the transfer runs while other asyncio tasks run. Short transfers, and transfers that cannot use DMA (for example from a buffer in flash), block other tasks until they finish. A transfer’s arguments are checked when the result is awaited. A transfer orconfigure()needs the bus’slockheld, so that a transaction of several transfers is not interleaved with another task’s.Construct an SPI object on the given pins.
- Parameters:
- __exit__() None
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
- configure(*, baudrate: int = 100000, polarity: int = 0, phase: int = 0, bits: int = 8) None
Configure the SPI bus, as
busio.SPI.configure. Needs thelock.
- async write(buffer: circuitpython_typing.ReadableBuffer, *, start: int = 0, end: int = sys.maxsize) None
Write the data in
buffer[start:end].- Parameters:
buffer (ReadableBuffer) – write out the data in this buffer
start (int) – beginning of buffer slice
end (int) – end of buffer slice; if not specified, use
len(buffer)
- async readinto(buffer: circuitpython_typing.WriteableBuffer, *, start: int = 0, end: int = sys.maxsize, write_value: int = 0) None
Read into
buffer[start:end]while writingwrite_valuefor each byte read.- Parameters:
buffer (WriteableBuffer) – read data into this buffer
start (int) – beginning of buffer slice
end (int) – end of buffer slice; if not specified, use
len(buffer)write_value (int) – value to write while reading
- async write_readinto(out_buffer: circuitpython_typing.ReadableBuffer, in_buffer: circuitpython_typing.WriteableBuffer, *, out_start: int = 0, out_end: int = sys.maxsize, in_start: int = 0, in_end: int = sys.maxsize) None
Write out the data in
out_bufferwhile simultaneously reading data intoin_buffer. The two slices must have the same length.- Parameters:
out_buffer (ReadableBuffer) – write out the data in this buffer
in_buffer (WriteableBuffer) – read data into this buffer
out_start (int) – beginning of
out_buffersliceout_end (int) – end of
out_bufferslice; if not specified, uselen(out_buffer)in_start (int) – beginning of
in_bufferslicein_end (int) – end of
in_bufferslice; if not specified, uselen(in_buffer)