SPI is a serial protocol that has exclusive pins for data in and out of the
master. It is typically faster than
I2C because a
separate pin is used to control the active slave rather than a transitted
address. This class only manages three of the four SPI lines:
MISO. Its up to the client to manage the appropriate slave
select line. (This is common because multiple slaves can share the
MISO lines and therefore the hardware.)
SPI(clock, MOSI=None, MISO=None)¶
- Construct an SPI object on the given pins.
Using this class directly requires careful lock management. Instead, use
SPIDeviceto manage locks.
Using this class to directly read registers requires manual bit unpacking. Instead, use an existing driver or make one with Register data descriptors.
Turn off the SPI bus.
No-op used by Context Managers.
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
configure(*, baudrate=100000, polarity=0, phase=0, bits=8)¶
Configures the SPI bus. The SPI object must be locked.
- baudrate (int) – the desired clock rate in Hertz. The actual clock rate may be higher or lower
due to the granularity of available clock settings.
frequencyattribute for the actual clock rate.
- polarity (int) – the base state of the clock line (0 or 1)
- phase (int) – the edge of the clock that data is captured. First (0) or second (1). Rising or falling depends on clock polarity.
- bits (int) – the number of bits per word
- baudrate (int) – 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
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.
On the nRF52840, these baudrates are available: 125kHz, 250kHz, 1MHz, 2MHz, 4MHz, and 8MHz. 16MHz and 32MHz are also available, but only on the first
busio.SPIobject you create. Two more
busio.SPIobjects can be created, but they are restricted to 8MHz maximum. This is a hardware restriction: there is only one high-speed SPI peripheral. If you pick a a baudrate other than one of these, the nearest lower baudrate will be chosen, with a minimum of 125kHz.
Attempts to grab the SPI lock. Returns True on success.
Returns: True when lock has been grabbed Return type: bool
Releases the SPI lock.
write(buffer, *, start=0, end=len(buffer))¶
Write the data contained in
buffer. The SPI object must be locked. If the buffer is empty, nothing happens.
readinto(buffer, *, start=0, end=len(buffer), write_value=0)¶
write_valuefor each byte read. The SPI object must be locked. If the number of bytes to read is 0, nothing happens.
write_readinto(buffer_out, buffer_in, *, out_start=0, out_end=len(buffer_out), in_start=0, in_end=len(buffer_in))¶
Write out the data in
buffer_outwhile simultaneously reading data into
buffer_in. The SPI object must be locked. The lengths of the slices defined by
buffer_in[in_start:in_end]must be equal. If buffer slice lengths are both 0, nothing happens.
- 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:
- out_end (int) – End of the slice; this index is not included
- in_start (int) – Start of the slice of
buffer_into read into:
- in_end (int) – End of the slice; this index is not included
The actual SPI bus frequency. This may not match the frequency requested due to internal limitations.