Skip to content

Commit

Permalink
i2c_gpio: Get actual direction register from device instead internal …
Browse files Browse the repository at this point in the history
…state

Until now we have tried to infer the state of the output register from an
internal state of the GPIO driver.
But: This means that the assumed state and the actual state can diverge.

With this change we always to a read-modify-write on this register.

Signed-off-by: Chris Fiege <[email protected]>
  • Loading branch information
SmithChart committed Nov 24, 2023
1 parent 768f54b commit fee410b
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions usbsdmux/i2c_gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ def __init__(self, sg):
"""
self._usb = Usb2642I2C(sg)

# After POR all Pins are Inputs.
# This value mirrors the value of the GPIO configuration
self._directionMask = 0xFF

def _write_register(self, register, value):
"""
Writes a register on the Pca9536 with a given value.
Expand All @@ -64,8 +60,9 @@ def set_pin_to_output(self, pins):
Arguments:
pins -- Combination of Pca9536.gpio_*
"""
self._directionMask = self._directionMask & (~pins)
self._write_register(self._register_configuration, self._directionMask)
direction = self.read_register(self._register_configuration)[0]
direction = (direction & ~pins) & 0xFF
self._write_register(self._register_configuration, direction)

def set_pin_to_input(self, pins):
"""
Expand All @@ -74,8 +71,9 @@ def set_pin_to_input(self, pins):
Arguments:
pins -- Combination of Pca9536.gpio_*
"""
self._directionMask = self._directionMask | pins
self._write_register(self._register_configuration, self._directionMask)
direction = self.read_register(self._register_configuration)[0]
direction = direction | pins
self._write_register(self._register_configuration, direction)

def output_values(self, values: int, bitmask: int = 0xFF):
"""
Expand Down

0 comments on commit fee410b

Please sign in to comment.