-
Notifications
You must be signed in to change notification settings - Fork 25
register accessor layer and SPI support #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+406
−0
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dc6d647
register accessor layer and SPI support
FoamyGuy 4532b05
repo and version meta vars
FoamyGuy 77983d4
I2C accessor implementation, repo and version meta vars
FoamyGuy a5e9569
Merge remote-tracking branch 'foamyguy/accessor_layer' into accessor_…
FoamyGuy 9bdc1f8
docstrings. implement bool vs int logic in i2c write
FoamyGuy 5196696
move read current value out of accessor into Bit(s). Support multibyt…
FoamyGuy c850754
pass address to read/write Accessor functions. Fix start/end indexes …
FoamyGuy 5039f29
SPI start/end fix for multibyte address. big endian multibyte address…
FoamyGuy b42f50e
split address and data buffers. Address buffer inside Accessor, data …
FoamyGuy 262e989
only use full_buffer for i2c.write(). Split SPI write into two instea…
FoamyGuy f4861a7
add protocol details to both Accessor docstrings. Remove buffer lengt…
FoamyGuy 4b553b4
add i2c comms details
FoamyGuy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2022 Max Holliday | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| """ | ||
| `adafruit_register.register_accessor` | ||
| ==================================================== | ||
|
|
||
| SPI and I2C Register Accessor classes. | ||
|
|
||
| * Author(s): Max Holliday | ||
| * Adaptation by Tim Cocks | ||
| """ | ||
|
|
||
| __version__ = "0.0.0+auto.0" | ||
| __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git" | ||
|
|
||
|
|
||
| try: | ||
| from typing import Union | ||
|
|
||
| from adafruit_bus_device.i2c_device import I2CDevice | ||
| from adafruit_bus_device.spi_device import SPIDevice | ||
| except ImportError: | ||
| pass | ||
|
|
||
|
|
||
| class SPIRegisterAccessor: | ||
| """ | ||
| RegisterAccessor class for SPI bus transport. Provides interface to read/write | ||
| registers over SPI. | ||
|
|
||
| :param SPIDevice spi_device: The SPI bus device to communicate over. | ||
| """ | ||
|
|
||
| def __init__(self, spi_device: SPIDevice): | ||
| self.spi_device = spi_device | ||
|
|
||
| def _shift_rw_cmd_bit_into_address_byte(self, buffer, bit_value): | ||
| if bit_value not in {0, 1}: | ||
| raise ValueError("bit_value must be 0 or 1") | ||
|
|
||
| # Clear the MSB (set bit 7 to 0) | ||
| cleared_byte = buffer[0] & 0x7F | ||
| # Set the MSB to the desired bit value | ||
| buffer[0] = cleared_byte | (bit_value << 7) | ||
|
|
||
| def read_register(self, buffer: bytearray): | ||
tannewt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Read register value over SPIDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device. Data will be | ||
| read into indexes 1-N. | ||
| :return: None | ||
| """ | ||
| with self.spi_device as spi: | ||
| self._shift_rw_cmd_bit_into_address_byte(buffer, 1) | ||
| spi.write(buffer, end=1) | ||
| spi.readinto(buffer, start=1) | ||
|
|
||
| def write_register( | ||
| self, | ||
| buffer: bytearray, | ||
| ): | ||
| """ | ||
| Write register value over SPIDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device for specified register. | ||
| :return: None | ||
| """ | ||
|
|
||
| with self.spi_device as spi: | ||
| self._shift_rw_cmd_bit_into_address_byte(buffer, 0) | ||
| spi.write(buffer) | ||
|
|
||
|
|
||
| class I2CRegisterAccessor: | ||
| """ | ||
| RegisterAccessor class for I2C bus transport. Provides interface to read/write | ||
| registers over I2C | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, explain the protocol over I2C that this supports.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also talk about the relationship between address and data. |
||
| :param I2CDevice i2c_device: I2C device to communicate over | ||
| """ | ||
|
|
||
| def __init__(self, i2c_device: I2CDevice): | ||
| self.i2c_device = i2c_device | ||
| pass | ||
|
|
||
| def read_register(self, buffer): | ||
| """ | ||
| Read register value over I2CDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
| Must be long enough to be read all data send by the device. Data will be | ||
| read into indexes 1-N. | ||
| :return: None | ||
| """ | ||
| with self.i2c_device as i2c: | ||
| i2c.write_then_readinto(buffer, buffer, out_end=1, in_start=1) | ||
FoamyGuy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def write_register(self, buffer): | ||
| """ | ||
| Write register value over I2CDevice. | ||
|
|
||
| :param bytearray buffer: Buffer must have register address value at index 0. | ||
FoamyGuy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Must be long enough to be read all data send by the device for specified register. | ||
| :return: None | ||
| """ | ||
|
|
||
| with self.i2c_device as i2c: | ||
| i2c.write(buffer) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| """ | ||
| `adafruit_register.register_bit` | ||
| ==================================================== | ||
|
|
||
| Single bit registers that use RegisterAccessor | ||
|
|
||
| * Author(s): Tim Cocks | ||
|
|
||
| """ | ||
|
|
||
| __version__ = "0.0.0+auto.0" | ||
| __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git" | ||
|
|
||
|
|
||
| class RWBit: | ||
| """ | ||
| Single bit register that is readable and writeable. | ||
|
|
||
| Values are `bool` | ||
|
|
||
| :param int register_address: The register address to read the bit from | ||
| :param int bit: The bit index within the byte at ``register_address`` | ||
| :param int register_width: The number of bytes in the register. Defaults to 1. | ||
| :param bool lsb_first: Is the first byte we read from spi the LSB? Defaults to true | ||
|
|
||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| register_address: int, | ||
| bit: int, | ||
| register_width: int = 1, | ||
| lsb_first: bool = True, | ||
| address_width: int = 1, | ||
| ): | ||
| self.bit_mask = 1 << (bit % 8) # the bitmask *within* the byte! | ||
|
|
||
| self.address = register_address | ||
|
|
||
| self.address_width = address_width | ||
| self.buffer = bytearray(address_width + register_width) | ||
|
|
||
| # Pack possible multibyte address into the buffer | ||
| for i in range(address_width): | ||
| if lsb_first: | ||
| # Little-endian: least significant byte first | ||
| self.buffer[i] = (register_address >> (i * 8)) & 0xFF | ||
| else: | ||
| # Big-endian: most significant byte first | ||
| self.buffer[i] = (register_address >> ((address_width - 1 - i) * 8)) & 0xFF | ||
|
|
||
| self.lsb_first = lsb_first | ||
| self.bit_index = bit | ||
| if lsb_first: | ||
| self.byte = address_width + (bit // 8) # Little-endian: bit 0 in first register byte | ||
| else: | ||
| self.byte = ( | ||
| address_width + register_width - 1 - (bit // 8) | ||
| ) # Big-endian: bit 0 in last register byte | ||
|
|
||
| def __get__(self, obj, objtype=None): | ||
| # read data from register | ||
| obj.register_accessor.read_register(self.buffer) | ||
|
|
||
| # check specified bit and return boolean | ||
| return bool(self.buffer[self.byte] & self.bit_mask) | ||
|
|
||
| def __set__(self, obj, value): | ||
| # read current data from register | ||
| obj.register_accessor.read_register(self.buffer) | ||
|
|
||
| # update current data with new value | ||
| if value: | ||
| self.buffer[self.byte] |= self.bit_mask | ||
| else: | ||
| self.buffer[self.byte] &= ~self.bit_mask | ||
|
|
||
| # write updated data to register | ||
| obj.register_accessor.write_register(self.buffer) | ||
|
|
||
|
|
||
| class ROBit(RWBit): | ||
| """Single bit register that is read only. Subclass of `RWBit`. | ||
|
|
||
| Values are `bool` | ||
|
|
||
| :param int register_address: The register address to read the bit from | ||
| :param type bit: The bit index within the byte at ``register_address`` | ||
| :param int register_width: The number of bytes in the register. Defaults to 1. | ||
|
|
||
| """ | ||
|
|
||
| def __set__(self, obj, value): | ||
| raise AttributeError() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 Tim Cocks for Adafruit Industries | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| """ | ||
| `adafruit_register.register_bits` | ||
| ==================================================== | ||
|
|
||
| Multi bit registers | ||
|
|
||
| * Author(s): Tim Cocks | ||
|
|
||
| """ | ||
|
|
||
| __version__ = "0.0.0+auto.0" | ||
| __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git" | ||
|
|
||
|
|
||
| class RWBits: | ||
| """ | ||
| Multibit register (less than a full byte) that is readable and writeable. | ||
| This must be within a byte register. | ||
|
|
||
| Values are `int` between 0 and 2 ** ``num_bits`` - 1. | ||
|
|
||
| :param int num_bits: The number of bits in the field. | ||
| :param int register_address: The register address to read the bit from | ||
| :param int lowest_bit: The lowest bits index within the byte at ``register_address`` | ||
| :param int register_width: The number of bytes in the register. Defaults to 1. | ||
| :param bool lsb_first: Is the first byte we read from SPI the LSB? Defaults to true | ||
| :param int address_width: The width of the register address in bytes. Defaults to 1. | ||
| """ | ||
|
|
||
| # pylint: disable=too-many-arguments | ||
| def __init__( | ||
| self, | ||
| num_bits: int, | ||
| register_address: int, | ||
| lowest_bit: int, | ||
| register_width: int = 1, | ||
| lsb_first: bool = True, | ||
| address_width: int = 1, | ||
| ): | ||
| self.bit_mask = ((1 << num_bits) - 1) << lowest_bit | ||
|
|
||
| if self.bit_mask >= 1 << (register_width * 8): | ||
| raise ValueError("Cannot have more bits than register size") | ||
| self.lowest_bit = lowest_bit | ||
|
|
||
| self.address_width = address_width | ||
| self.buffer = bytearray(address_width + register_width) | ||
|
|
||
| # Pack possible multibyte address into the buffer | ||
| for i in range(address_width): | ||
| if lsb_first: | ||
| # Little-endian: least significant byte first | ||
| self.buffer[i] = (register_address >> (i * 8)) & 0xFF | ||
| else: | ||
| # Big-endian: most significant byte first | ||
| self.buffer[i] = (register_address >> ((address_width - 1 - i) * 8)) & 0xFF | ||
|
|
||
| # self.buffer[1] = register_width - 1 | ||
| self.lsb_first = lsb_first | ||
|
|
||
| def __get__(self, obj, objtype=None): | ||
| # read data from register | ||
| obj.register_accessor.read_register(self.buffer) | ||
|
|
||
| # read the bytes into a single variable | ||
| reg = 0 | ||
| order = range(len(self.buffer) - 1, self.address_width - 1, -1) | ||
| if not self.lsb_first: | ||
| order = reversed(order) | ||
| for i in order: | ||
| reg = (reg << 8) | self.buffer[i] | ||
|
|
||
| # extract integer value from specified bits | ||
| result = (reg & self.bit_mask) >> self.lowest_bit | ||
| return result | ||
|
|
||
| def __set__(self, obj, value): | ||
| # read current data from register | ||
| obj.register_accessor.read_register(self.buffer) | ||
|
|
||
| # shift in integer value to register data | ||
| reg = 0 | ||
| order = range(len(self.buffer) - 1, self.address_width - 1, -1) | ||
| if not self.lsb_first: | ||
| order = range(1, len(self.buffer)) | ||
| for i in order: | ||
| reg = (reg << 8) | self.buffer[i] | ||
| shifted_value = value << self.lowest_bit | ||
| reg &= ~self.bit_mask # mask off the bits we're about to change | ||
| reg |= shifted_value # then or in our new value | ||
|
|
||
| # put data from reg back into buffer | ||
| for i in reversed(order): | ||
| self.buffer[i] = reg & 0xFF | ||
| reg >>= 8 | ||
|
|
||
| # write updated data into the register | ||
| obj.register_accessor.write_register(self.buffer) | ||
|
|
||
|
|
||
| class ROBits(RWBits): | ||
| """ | ||
| Multibit register (less than a full byte) that is read-only. This must be | ||
| within a byte register. | ||
|
|
||
| Values are `int` between 0 and 2 ** ``num_bits`` - 1. | ||
|
|
||
| :param int num_bits: The number of bits in the field. | ||
| :param int register_address: The register address to read the bit from | ||
| :param type lowest_bit: The lowest bits index within the byte at ``register_address`` | ||
| :param int register_width: The number of bytes in the register. Defaults to 1. | ||
| """ | ||
|
|
||
| def __set__(self, obj, value): | ||
| raise AttributeError() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.