busio¶
Hardware accelerated external bus access
The busio module contains classes to support a variety of serial
protocols.
When the microcontroller does not support the behavior in a hardware
accelerated fashion it may internally use a bitbang routine. However, if
hardware support is available on a subset of pins but not those provided,
then a RuntimeError will be raised. Use the bitbangio module to explicitly
bitbang a serial protocol on any general purpose pins.
All classes change hardware state and should be deinitialized when they
are no longer needed if the program continues after use. To do so, either
call deinit() or use a context manager. See
Lifetime and ContextManagers for more info.
For example:
import busio
from board import *
i2c = busio.I2C(SCL, SDA)
print(i2c.scan())
i2c.deinit()
This example will initialize the the device, run
scan() and then deinit() the
hardware. The last step is optional because CircuitPython automatically
resets hardware after a program finishes.
-
class
busio.I2C(scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 400000, timeout: int = 255)¶ Two wire serial protocol
-
deinit(self)¶ Releases control of the underlying hardware so other classes can use it.
-
__enter__(self)¶ No-op used in Context Managers.
-
__exit__(self)¶ Automatically deinitializes the hardware on context exit. See Lifetime and ContextManagers for more info.
-
scan(self)¶ Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond.
- Returns
List of device ids on the I2C bus
- Return type
-
try_lock(self)¶ Attempts to grab the I2C lock. Returns True on success.
- Returns
True when lock has been grabbed
- Return type
-
unlock(self)¶ Releases the I2C lock.
-
readfrom_into(self, address: int, buffer: bytearray, *, start: int = 0, end: int = None)¶ Read into
bufferfrom the device selected byaddress. The number of bytes read will be the length ofbuffer. At least one byte must be read.If
startorendis provided, then the buffer will be sliced as ifbuffer[start:end]. This will not cause an allocation likebuf[start:end]will so it saves memory.
-
writeto(self, address: int, buffer: bytearray, *, start: int = 0, end: int = None, stop: bool = True)¶ - Write the bytes from
bufferto the device selected byaddress. Transmits a stop bit when stop is True. Setting stop=False is deprecated and stop will be removed in CircuitPython 6.x. Use
writeto_then_readfromwhen needing a write, no stop and repeated start before a read.If
startorendis provided, then the buffer will be sliced as ifbuffer[start:end]. This will not cause an allocation likebuffer[start:end]will so it saves memory.- Writing a buffer or slice of length zero is permitted, as it can be used
to poll for the existence of a device.
- param int address
7-bit device address
- param bytearray buffer
buffer containing the bytes to write
- param int start
Index to start writing from
- param int end
Index to read up to but not include. Defaults to
len(buffer)- param bool stop
If true, output an I2C stop condition after the buffer is written. Deprecated. Will be removed in 6.x and act as stop=True.
- Write the bytes from
-
writeto_then_readfrom(self, address: int, out_buffer: bytearray, in_buffer: bytearray, *, out_start: int = 0, out_end: int = None, in_start: int = 0, in_end: int = None)¶ - Write the bytes from
out_bufferto the device selected byaddress, generate no stop bit, generate a repeated start and read into
in_buffer.out_bufferandin_buffercan be the same buffer because they are used sequentially.- If
startorendis provided, then the corresponding buffer will be sliced as if
buffer[start:end]. This will not cause an allocation likebuf[start:end]will so it saves memory.- param int address
7-bit device address
- param bytearray out_buffer
buffer containing the bytes to write
- param bytearray in_buffer
buffer to write into
- param int out_start
Index to start writing from
- param int out_end
Index to read up to but not include. Defaults to
len(buffer)- param int in_start
Index to start writing at
- param int in_end
Index to write up to but not include. Defaults to
len(buffer)
- Write the bytes from
-
-
class
busio.OneWire(pin: microcontroller.Pin)¶ Lowest-level of the Maxim OneWire protocol
-
deinit(self)¶ Deinitialize the OneWire bus and release any hardware resources for reuse.
-
__enter__(self)¶ No-op used by Context Managers.
-
__exit__(self)¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
-
reset(self)¶ Reset the OneWire bus and read presence
- Returns
False when at least one device is present
- Return type
-
write_bit(self, value: Any)¶ Write out a bit based on value.
-
-
class
busio.SPI(clock: microcontroller.Pin, MOSI: microcontroller.Pin = None, MISO: microcontroller.Pin = None)¶ A 3-4 wire serial protocol
SPI is a serial protocol that has exclusive pins for data in and out of the main device. It is typically faster than
I2Cbecause a separate pin is used to select a device rather than a transmitted address. This class only manages three of the four SPI lines:clock,MOSI,MISO. Its up to the client to manage the appropriate select line, often abbreviatedCSorSS. (This is common because multiple secondaries can share theclock,MOSIandMISOlines and therefore the hardware.)-
frequency:Any¶ The actual SPI bus frequency. This may not match the frequency requested due to internal limitations.
-
deinit(self)¶ Turn off the SPI bus.
-
__enter__(self)¶ No-op used by Context Managers. Provided by context manager helper.
-
__exit__(self)¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
-
configure(self, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, bits: int = 8)¶ Configures the SPI bus. The SPI object must be locked.
- param int baudrate
the desired clock rate in Hertz. The actual clock rate may be higher or lower due to the granularity of available clock settings. Check the
frequencyattribute for the actual clock rate.- param int polarity
the base state of the clock line (0 or 1)
- param int phase
the edge of the clock that data is captured. First (0) or second (1). Rising or falling depends on clock polarity.
- param int bits
the number of bits per word
Note
- On the SAMD21, it is possible to set the baudrate to 24 MHz, but that
speed is not guaranteed to work. 12 MHz is the next available lower speed, and is within spec for the SAMD21.
Note
On the nRF52840, these baudrates are available: 125kHz, 250kHz, 1MHz, 2MHz, 4MHz, and 8MHz. If you pick a a baudrate other than one of these, the nearest lower baudrate will be chosen, with a minimum of 125kHz. Two SPI objects may be created, except on the Circuit Playground Bluefruit, which allows only one (to allow for an additional I2C object).
-
try_lock(self)¶ Attempts to grab the SPI lock. Returns True on success.
- Returns
True when lock has been grabbed
- Return type
-
unlock(self)¶ Releases the SPI lock.
-
write(self, buffer: bytearray, *, start: Any = 0, end: int = None)¶ Write the data contained in
buffer. The SPI object must be locked. If the buffer is empty, nothing happens.
-
readinto(self, buffer: bytearray, *, start: Any = 0, end: int = None, write_value: int = 0)¶ Read into
bufferwhile writingwrite_valuefor each byte read. The SPI object must be locked. If the number of bytes to read is 0, nothing happens.
-
write_readinto(self, buffer_out: bytearray, buffer_in: bytearray, *, out_start: Any = 0, out_end: int = None, in_start: Any = 0, in_end: int = None)¶ Write out the data in
buffer_outwhile simultaneously reading data intobuffer_in. The SPI object must be locked. The lengths of the slices defined bybuffer_out[out_start:out_end]andbuffer_in[in_start:in_end]must be equal. If buffer slice lengths are both 0, nothing happens.- Parameters
buffer_out (bytearray) – Write out the data in this buffer
buffer_in (bytearray) – Read data into this buffer
out_start (int) – Start of the slice of buffer_out to write out:
buffer_out[out_start:out_end]out_end (int) – End of the slice; this index is not included. Defaults to
len(buffer_out)in_start (int) – Start of the slice of
buffer_into read into:buffer_in[in_start:in_end]in_end (int) – End of the slice; this index is not included. Defaults to
len(buffer_in)
-
-
class
busio.UART(tx: microcontroller.Pin, rx: microcontroller.Pin, *, baudrate: int = 9600, bits: int = 8, parity: Parity = None, stop: int = 1, timeout: float = 1, receiver_buffer_size: int = 64)¶ A bidirectional serial protocol
-
baudrate:Any¶ The current baudrate.
-
in_waiting:Any¶ The number of bytes in the input buffer, available to be read
-
timeout:Any¶ The current timeout, in seconds (float).
-
deinit(self)¶ Deinitialises the UART and releases any hardware resources for reuse.
-
__enter__(self)¶ No-op used by Context Managers.
-
__exit__(self)¶ Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
-
read(self, nbytes: Any = None)¶ Read characters. If
nbytesis specified then read at most that many bytes. Otherwise, read everything that arrives until the connection times out. Providing the number of bytes expected is highly recommended because it will be faster.
-
readinto(self, buf: Any)¶ Read bytes into the
buf. Read at mostlen(buf)bytes.New in CircuitPython 4.0: No length parameter is permitted.
-
readline(self)¶ - Read a line, ending in a newline character, or
return None if a timeout occurs sooner, or return everything readable if no newline is found and timeout=0
-
write(self, buf: Any)¶ Write the buffer of bytes to the bus.
New in CircuitPython 4.0:
bufmust be bytes, not a string.- return
the number of bytes written
- rtype
int or None
-
reset_input_buffer(self)¶
-