Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/cy_serial_bridge/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,80 @@ def read_user_flash(self, addr: int, size: int) -> bytearray:
result_bytes.extend(page_bytes)

return result_bytes


def is_gpio_output(self, gpio_nr: int) -> bool:
"""
Check if a GPIO pin is in output mode.

:param pin: GPIO pin number to check
:return: True if the pin is an output pin, False otherwise
"""

# TODO implement by checking it in the active configuration block
return True


def is_gpio_input(self, gpio_nr: int) -> bool:
"""
Check if a GPIO pin is readable.
We can read the value of input or output pins, but
not if they are tristated or in some other mode.

:param pin: GPIO pin number to check
:return: True if the pin is an input pin, False otherwise
"""

# TODO implement by checking it in the active configuration block
return True


def set_gpio(self, gpio_nr: int, value: int) -> None:
"""
Set the value of a GPIO pin.

:param pin: GPIO pin number to set
:param value: Value to set the pin to
"""

if not self.is_gpio_output(gpio_nr):
message = f"GPIO {gpio_nr} is not usable!"
raise CySerialBridgeError(message)
if value != 0 and value != 1:
value = 1
self.dev.controlWrite(
request_type=CY_VENDOR_REQUEST_HOST_TO_DEVICE,
request=CyVendorCmds.CY_GPIO_SET_VALUE_CMD,
value=gpio_nr,
index=value,
data=[],
timeout=self.timeout
)
# TODO check for errors


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

if not self.is_gpio_input(gpio_nr):
message = f"GPIO {gpio_nr} is not usable!"
raise CySerialBridgeError(message)
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]


class CyMfgrIface(CySerBridgeBase):
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