Skip to content

Commit

Permalink
Add functionality to set eGPIO pin in/out mode
Browse files Browse the repository at this point in the history
  • Loading branch information
emericklaw committed Jun 5, 2024
1 parent 6e1ddc4 commit bf55c5a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/tildagon/pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ def __init__(self, pin_name, mode=-1):
self.name = pin_name
self.pin = HEXPANSION_GPIOS[pin_name]
self.mode = mode
if self.mode in (machine.Pin.IN, -1):
tildagonos.set_pin_mode(self.pin, machine.Pin.IN)
elif self.mode == machine.Pin.OUT:
tildagonos.set_pin_mode(self.pin, machine.Pin.OUT)
else:
raise ValueError("Invalid pin mode")

def on(self):
self.value(1)
Expand Down
13 changes: 13 additions & 0 deletions modules/tildagonos.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ def init_gpio(self):
# chip C - switch mode to push-pull
self.system_i2c.writeto_mem(0x5A, 0x11, bytes([0x10]))

def set_pin_mode(self, pin, mode=Pin.IN):
portstates = list(map(int, self.system_i2c.readfrom_mem(pin[0], 0x04, 2)))
if mode == Pin.IN:
self.system_i2c.writeto_mem(
pin[0], 0x04 + pin[1], bytes([portstates[pin[1]] | pin[2]])
)
elif mode == Pin.OUT:
self.system_i2c.writeto_mem(
pin[0], 0x04 + pin[1], bytes([portstates[pin[1]] & (pin[2] ^ 0xFF)])
)
else:
raise ValueError("Invalid pin mode")

def set_egpio_pin(self, pin, state):
portstates = list(map(int, self.system_i2c.readfrom_mem(pin[0], 0x02, 2)))
if state:
Expand Down

0 comments on commit bf55c5a

Please sign in to comment.