:mod:`bitbangio` ================ .. py:module:: bitbangio .. autoapi-nested-parse:: Digital protocols implemented by the CPU The `bitbangio` module contains classes to provide digital bus protocol support regardless of whether the underlying hardware exists to use the protocol. First try to use `busio` module instead which may utilize peripheral hardware to implement the protocols. Native implementations will be faster than bitbanged versions and have more capabilities. 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 :py:meth:`!deinit` or use a context manager. See :ref:`lifetime-and-contextmanagers` for more info. For example:: import bitbangio from board import * i2c = bitbangio.I2C(SCL, SDA) print(i2c.scan()) i2c.deinit() This example will initialize the the device, run :py:meth:`~bitbangio.I2C.scan` and then :py:meth:`~bitbangio.I2C.deinit` the hardware. The last step is optional because CircuitPython automatically resets hardware after a program finishes. .. py:class:: I2C(scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 400000, timeout: int) Two wire serial protocol .. method:: deinit(self) Releases control of the underlying hardware so other classes can use it. .. method:: __enter__(self) No-op used in Context Managers. .. method:: __exit__(self) Automatically deinitializes the hardware on context exit. See :ref:`lifetime-and-contextmanagers` for more info. .. method:: scan(self) Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond. A device responds if it pulls the SDA line low after its address (including a read bit) is sent on the bus. .. method:: try_lock(self) Attempts to grab the I2C lock. Returns True on success. .. method:: unlock(self) Releases the I2C lock. .. method:: readfrom_into(self, address: int, buffer: bytearray, *, start: int = 0, end: int = None) Read into ``buffer`` from the device selected by ``address``. The number of bytes read will be the length of ``buffer``. At least one byte must be read. If ``start`` or ``end`` is provided, then the buffer will be sliced as if ``buffer[start:end]``. This will not cause an allocation like ``buf[start:end]`` will so it saves memory. :param int address: 7-bit device address :param bytearray buffer: buffer to write into :param int start: Index to start writing at :param int end: Index to write up to but not include .. method:: writeto(self, address: int, buffer: bytearray, *, start: int = 0, end: int = None, stop: bool = True) Write the bytes from ``buffer`` to the device selected by ``address`` and then transmits a stop bit. Use `writeto_then_readfrom` when needing a write, no stop and repeated start before a read. If ``start`` or ``end`` is provided, then the buffer will be sliced as if ``buffer[start:end]``. This will not cause an allocation like ``buffer[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 :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. .. method:: 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_buffer`` to the device selected by ``address``, generate no stop bit, generate a repeated start and read into ``in_buffer``. ``out_buffer`` and ``in_buffer`` can be the same buffer because they are used sequentially. If ``start`` or ``end`` is provided, then the corresponding buffer will be sliced as if ``buffer[start:end]``. This will not cause an allocation like ``buf[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)`` .. py:class:: OneWire(pin: microcontroller.Pin) Lowest-level of the Maxim OneWire protocol :class:`~bitbangio.OneWire` implements the timing-sensitive foundation of the Maxim (formerly Dallas Semi) OneWire protocol. Protocol definition is here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/126 .. method:: deinit(self) Deinitialize the OneWire bus and release any hardware resources for reuse. .. method:: __enter__(self) No-op used by Context Managers. .. method:: __exit__(self) Automatically deinitializes the hardware when exiting a context. See :ref:`lifetime-and-contextmanagers` for more info. .. method:: reset(self) Reset the OneWire bus .. method:: read_bit(self) Read in a bit :returns: bit state read :rtype: bool .. method:: write_bit(self, value: Any) Write out a bit based on value. .. py:class:: 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 :py:class:`~bitbangio.I2C` because 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 abbreviated `!CS` or `!SS`. (This is common because multiple secondaries can share the `!clock`, `!MOSI` and `!MISO` lines and therefore the hardware.) .. method:: deinit(self) Turn off the SPI bus. .. method:: __enter__(self) No-op used by Context Managers. .. method:: __exit__(self) Automatically deinitializes the hardware when exiting a context. See :ref:`lifetime-and-contextmanagers` for more info. .. method:: configure(self, *, baudrate: int = 100000, polarity: int = 0, phase: int = 0, bits: int = 8) Configures the SPI bus. Only valid when locked. :param int baudrate: the clock rate in Hertz :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 .. method:: try_lock(self) Attempts to grab the SPI lock. Returns True on success. :return: True when lock has been grabbed :rtype: bool .. method:: unlock(self) Releases the SPI lock. .. method:: write(self, buf: Any) Write the data contained in ``buf``. Requires the SPI being locked. If the buffer is empty, nothing happens. .. method:: readinto(self, buf: Any) Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked. If the number of bytes to read is 0, nothing happens. .. method:: 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_out`` while simultaneously reading data into ``buffer_in``. The lengths of the slices defined by ``buffer_out[out_start:out_end]`` and ``buffer_in[in_start:in_end]`` must be equal. If buffer slice lengths are both 0, nothing happens. :param bytearray buffer_out: Write out the data in this buffer :param bytearray buffer_in: Read data into this buffer :param int out_start: Start of the slice of buffer_out to write out: ``buffer_out[out_start:out_end]`` :param int out_end: End of the slice; this index is not included. Defaults to ``len(buffer_out)`` :param int in_start: Start of the slice of ``buffer_in`` to read into: ``buffer_in[in_start:in_end]`` :param int in_end: End of the slice; this index is not included. Defaults to ``len(buffer_in)``