Skip to content

Commit

Permalink
Merge pull request #4 from runger1101001/gpio_setting
Browse files Browse the repository at this point in the history
Get/Set GPIOs
  • Loading branch information
multiplemonomials authored Dec 2, 2024
2 parents bc1e50f + 315fb79 commit 3048998
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
65 changes: 65 additions & 0 deletions src/cy_serial_bridge/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,71 @@ def serial_term(
term.close()


#
# GPIO command
# ---------------------------------------------------------------------------------------------

GpioArgument = typer.Argument(
help="GPIOs to get/set. A string in the format 'io1 io4 io2=0 io3=1'."
)

class GpioOutputStyle(str, enum.Enum):
"""
Enum of output styles for the GPIO command
"""
ASCII = "ascii"
PLAIN = "plain"
JSON = "json"

GpioOutputStyleOption = typer.Option("--output-style", help="Output style to use", case_sensitive=False)


@app.command(help="Set/Get GPIO pins on the CY7C652xx")
def gpio(
gpio_opt: Annotated[str, GpioArgument] = "", outstyle: Annotated[GpioOutputStyle, GpioOutputStyleOption] = GpioOutputStyle.ASCII
) -> None:
with cast(
cy_serial_bridge.driver.CyMfgrIface,
context.open_device(
global_opt.vid, global_opt.pid, cy_serial_bridge.OpenMode.MFGR_INTERFACE, global_opt.serial_number
),
) as dev:
if outstyle == GpioOutputStyle.JSON:
print("[")
dev.connect()
gpio_opts = gpio_opt.split()
first = True
for opt in gpio_opts:
if "=" in opt:
pin, value = opt.split("=")
pin = int(pin.strip("io"))
value = int(value)
dev.set_gpio(pin, value)
else:
pin = int(opt.strip("io"))
value = dev.get_gpio(pin)
if outstyle == GpioOutputStyle.ASCII:
if not first:
print(" ", end="")
print(f"io{pin}={value}", end="")
elif outstyle == GpioOutputStyle.PLAIN:
print(f"{value}")
elif outstyle == GpioOutputStyle.JSON:
if not first:
print(",")
print(f'{{"pin": {pin}, "value": {value}}}', end="")
first = False
dev.disconnect()
if outstyle == GpioOutputStyle.JSON:
print("")
print("]")
elif outstyle == GpioOutputStyle.ASCII:
print("")


# Main entry point
# ---------------------------------------------------------------------------------------------

def main() -> None:
app()

Expand Down
40 changes: 40 additions & 0 deletions src/cy_serial_bridge/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,46 @@ def read_user_flash(self, addr: int, size: int) -> bytearray:
return result_bytes


def set_gpio(self, gpio_nr: int, value: bool) -> None:
"""
Set the value of a GPIO pin.
:param pin: GPIO pin number to set
:param value: Value to set the pin to
"""

self.dev.controlWrite(
request_type=CY_VENDOR_REQUEST_HOST_TO_DEVICE,
request=CyVendorCmds.CY_GPIO_SET_VALUE_CMD,
value=gpio_nr,
index=1 if value==True else 0,
data=[],
timeout=self.timeout
)
# TODO check for errors


def get_gpio(self, gpio_nr: int) -> bool:
"""
Get the value of a GPIO pin.
:param pin: GPIO pin number to get
:return: Value of the pin
"""

result_bytes = self.dev.controlRead(
request_type=CY_VENDOR_REQUEST_DEVICE_TO_HOST,
request=CyVendorCmds.CY_GPIO_GET_VALUE_CMD,
value=gpio_nr,
index=0,
length=CY_GET_GPIO_LEN,
timeout=self.timeout
)
if len(result_bytes) != CY_GET_GPIO_LEN or result_bytes[0] != 0:
message = f"Error getting GPIO {gpio_nr}"
raise CySerialBridgeError(message)
return (result_bytes[1]==1)


class CyMfgrIface(CySerBridgeBase):
"""
Class allowing access to a CY7C652xx in the manufacturing interface mode.
Expand Down
8 changes: 7 additions & 1 deletion src/cy_serial_bridge/usb_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ class CyVendorCmds(IntEnum):
CY_JTAG_READ_CMD = 0xD2
CY_JTAG_WRITE_CMD = 0xD3

# From Infineon's forums on Mar 24, 2023, here: https://community.infineon.com/t5/USB-low-full-high-speed/CY7C65215-Get-Set-GPIO-Config/td-p/336658
# "CY_GPIO_GET_CONFIG_CMD is maintaining in CyUSBCommon.h but it has not been implemented at the
# hoist side and Silicon so this will not work, we apologize for the confusion with this code
# will remove this in the upcoming release so it does not create confusion to the user."
CY_GPIO_GET_CONFIG_CMD = 0xD8
CY_GPIO_SET_CONFIG_CMD = 0xD9
# GET_VALUE and SET_VALUE are implemented and can be used
CY_GPIO_GET_VALUE_CMD = 0xDA
CY_GPIO_SET_VALUE_CMD = 0xDB

Expand Down Expand Up @@ -191,7 +196,8 @@ class CyUart(IntEnum):
CY_GET_SILICON_ID_LEN = 4
CY_GET_FIRMWARE_VERSION_LEN = 8
CY_GET_SIGNATURE_LEN = 4

CY_GET_GPIO_LEN = 2
CY_SET_GPIO_LEN = 1

# PHDC related macros
class CyPhdc(IntEnum):
Expand Down

0 comments on commit 3048998

Please sign in to comment.