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
  • 0xCB Gemini
  • 0xCB Helios
  • 42. Keebs Frood
  • 8086 RP2040 Interfacer
  • 8086 USB Interposer
  • Adafruit CLUE nRF52840 Express (Zephyr)
  • Adafruit Circuit Playground Bluefruit (Zephyr)
  • Adafruit Feather Bluefruit Sense (Zephyr)
  • Adafruit Feather RP2040
  • Adafruit Feather RP2040 (Zephyr)
  • Adafruit Feather RP2040 Adalogger
  • Adafruit Feather RP2040 CAN
  • Adafruit Feather RP2040 DVI
  • Adafruit Feather RP2040 Prop-Maker
  • Adafruit Feather RP2040 RFM
  • Adafruit Feather RP2040 Scorpio
  • Adafruit Feather RP2040 ThinkInk
  • Adafruit Feather RP2040 USB Host
  • Adafruit Feather RP2350
  • Adafruit Feather RP2350 Adalogger
  • Adafruit Feather nRF52840 Express (Zephyr)
  • Adafruit Floppsy RP2040
  • Adafruit Fruit Jam
  • Adafruit Industries LLC Metro CH32V407 (Zephyr)
  • Adafruit Industries LLC Metro CH32V467 (Zephyr)
  • Adafruit Industries LLC Metro M7 iMX RT1011 AirLift (Zephyr)
  • Adafruit Industries LLC Metro M7 iMX RT1011 SD (Zephyr)
  • Adafruit ItsyBitsy RP2040
  • Adafruit ItsyBitsy nRF52840 Express (Zephyr)
  • Adafruit KB2040
  • Adafruit MacroPad RP2040 (Zephyr)
  • Adafruit Macropad RP2040
  • Adafruit Metro RP2040
  • Adafruit Metro RP2350
  • Adafruit QT Py RP2040
  • Adafruit QT2040 Trinkey
  • Adafruit Tinychad RP2350
  • Archi RP2040
  • Arduino Nano RP2040 Connect
  • BBQ20KBD
  • BLOK
  • Bradán Lane STUDIO Explorer Badge
  • COSMO-Pico
  • Challenger NB RP2040 WiFi
  • Challenger RP2040 LTE
  • Challenger RP2040 LoRa
  • Challenger RP2040 NFC
  • Challenger RP2040 SD/RTC
  • Challenger RP2040 SubGHz
  • Challenger RP2040 WiFi
  • Challenger RP2040 WiFi/BLE
  • Challenger+ RP2350 BConnect
  • Challenger+ RP2350 WiFi6/BLE5
  • Cytron EDU PICO 2
  • Cytron EDU PICO W
  • Cytron IRIV IO Controller
  • Cytron MOTION 2350 Pro
  • Cytron Maker Nano RP2040
  • Cytron Maker Pi RP2040
  • Cytron Maker Sumo RP2350
  • Cytron Maker Uno RP2040
  • Datanoise PicoADK
  • Datanoise PicoADK V2
  • E-Fidget
  • ELECFREAKS PICO:ED
  • Electrolama minik
  • EncoderPad RP2040
  • Fig Pi
  • HEIA-FR Picomo V2
  • HEIA-FR Picomo V3
  • HXR.DK SAO Digital Multimeter
  • Hack Club Sprig
  • IDE-TUDelft RealPicoExpander
  • LILYGO T-DISPLAY
  • Maple Computing Elite-Pi
  • Melopero Shake RP2040
  • MoonYard Equinox
  • Music Thing Modular Workshop Computer
  • NXP Semiconductors FRDM-MCXN947 (Zephyr)
  • NXP Semiconductors FRDM-RW612 (Zephyr)
  • NXP Semiconductors IMXRT1010-EVK (Zephyr)
  • NXP Semiconductors MIMXRT1170-EVK (Zephyr)
  • Noise Nugget 2040
  • Nordic Semiconductor nRF5340 DK (Zephyr)
  • Nordic Semiconductor nRF54H20 DK (Zephyr)
  • Nordic Semiconductor nRF54L15 DK (Zephyr)
  • Nordic Semiconductor nRF54L15 TAG (Zephyr)
  • Nordic Semiconductor nRF54LM20 DK (Zephyr)
  • Oak Dev Tech BREAD2040
  • Oak Dev Tech Cast-Away RP2040
  • Oak Dev Tech RPGA Feather
  • Orpheus Pico
  • PCBCupid Glyph Mini 2040
  • PROVES Kit v4
  • Pajenicko PicoPad
  • Pimoroni Badger 2040
  • Pimoroni Badger 2040 W
  • Pimoroni Badger 2350
  • Pimoroni Explorer
  • Pimoroni Inky Frame 5.7
  • Pimoroni Inky Frame 7.3
  • Pimoroni Interstate 75
  • Pimoroni Keybow 2040
  • Pimoroni Motor 2040
  • Pimoroni PGA2040
  • Pimoroni PGA2350
  • Pimoroni Pico DV Base W
  • Pimoroni Pico LiPo (16MB)
  • Pimoroni Pico LiPo (4MB)
  • Pimoroni Pico Plus 2
  • Pimoroni Pico Plus 2 W
  • Pimoroni Pico dv Base
  • Pimoroni PicoSystem
  • Pimoroni Plasma 2040
  • Pimoroni Plasma 2040W
  • Pimoroni Plasma 2350
  • Pimoroni Plasma 2350W
  • Pimoroni Servo 2040
  • Pimoroni Tiny 2040 (2MB)
  • Pimoroni Tiny 2040 (8MB)
  • Pimoroni Tiny 2350
  • Pimoroni Tiny FX
  • PyKey 18 Numpad
  • PyKey 44 Ergo
  • PyKey 60
  • PyKey 87 TKL
  • RF.Guru RP2040
  • RP2.65-F
  • RP2040 Stamp
  • RP2350 Stamp
  • RP2350 Stamp XL
  • Raspberry Breadstick
  • Raspberry Pi Pico
  • Raspberry Pi Pico (Zephyr)
  • Raspberry Pi Pico 2
  • Raspberry Pi Pico 2 W
  • Raspberry Pi Pico 2 W (Zephyr)
  • Raspberry Pi Pico W
  • Raspberry Pi Pico W (Zephyr)
  • Renesas Electronics Corporation RA6M5 Evaluation Kit (Zephyr)
  • Renesas Electronics Corporation RA8D1 Evaluation Kit (Zephyr)
  • STMicroelectronics Nucleo N657X0-Q (Zephyr)
  • STMicroelectronics Nucleo U575ZI Q (Zephyr)
  • STMicroelectronics STM32H7B3I Discovery kit (Zephyr)
  • STMicroelectronics STM32WBA65I Discovery kit (Zephyr)
  • SecuringHardware.com Xiaomao
  • Seeeduino XIAO RP2040
  • Seeeduino XIAO RP2350
  • Silicognition LLC RP2040-Shim
  • Silicon Labs SiWx917 Wi-Fi 6 and Bluetooth LE SoC Dev Kit (BRD2605A) (Zephyr)
  • SparkFun MicroMod RP2040 Processor
  • SparkFun Pro Micro RP2040
  • SparkFun Pro Micro RP2350
  • SparkFun Thing Plus - RP2040
  • SparkFun Thing Plus RP2350
  • Teenage Engineering ting fx EP-2350
  • TinyCircuits Thumby
  • TinyCircuits Thumby Color
  • VCC-GND Studio YD RP2040
  • W5100S-EVB-Pico
  • W5100S-EVB-Pico2
  • W5500-EVB-Pico
  • W5500-EVB-Pico2
  • W55RP20-EVB-Pico
  • WK-50 Trackball Keyboard
  • Waveshare RP2040-GEEK
  • Waveshare RP2040-LCD-0.96
  • Waveshare RP2040-LCD-1.28
  • Waveshare RP2040-One
  • Waveshare RP2040-PiZero
  • Waveshare RP2040-Plus (16MB)
  • Waveshare RP2040-Plus (4MB)
  • Waveshare RP2040-TOUCH-LCD-1.28
  • Waveshare RP2040-Tiny
  • Waveshare RP2040-Zero
  • Waveshare RP2350-GEEK
  • Waveshare RP2350-LCD-0.96
  • Waveshare RP2350-LCD-1.28
  • Waveshare RP2350-One
  • Waveshare RP2350-Plus
  • Waveshare RP2350-TOUCH-LCD-1.28
  • Waveshare RP2350-Tiny
  • Waveshare RP2350-Zero
  • WeAct Studio Pico
  • WeAct Studio Pico 16MB
  • WeAct Studio RP2350B Core
  • WisdPi Ardu2040M
  • WisdPi Tiny RP2040
  • nullbits Bit-C PRO
  • splitkb.com Liatris
  • takayoshiotake Octave RP2040
  • uGame22
  • zephyr Native simulator - LVGL + ASan (Zephyr)
  • zephyr Native simulator - LVGL displayio compositor (Zephyr)
  • zephyr Native simulator - littlefs filesystem (Zephyr)
  • zephyr Native simulator - native_sim (Zephyr)

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.

async __aenter__() → Lock

Waits for the lock and takes it.

__aexit__() → None

Hands the lock to the next task waiting, or releases it.

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, readinto and write_readinto return 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 or configure() needs the bus’s lock held, so that a transaction of several transfers is not interleaved with another task’s.

Construct an SPI object on the given pins.

Parameters:
  • clock (Pin) – the pin to use for the clock.

  • MOSI (Pin) – the Main Out Selected In pin.

  • MISO (Pin) – the Main In Selected Out pin.

deinit() → None

Turn off the SPI bus. A transfer still running is stopped.

__enter__() → SPI

No-op used by Context Managers.

__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 the lock.

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 writing write_value for 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_buffer while simultaneously reading data into in_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_buffer slice

  • out_end (int) – end of out_buffer slice; if not specified, use len(out_buffer)

  • in_start (int) – beginning of in_buffer slice

  • in_end (int) – end of in_buffer slice; if not specified, use len(in_buffer)

frequency: int

The actual SPI bus frequency. This may not match the frequency requested due to internal limitations.

lock: Lock

The bus’s lock. Hold it around a transaction.

For example:

async with spi.lock:
    spi.configure(baudrate=8_000_000)
    await spi.write(command)
    await spi.readinto(reply)