Package: pyft4222
PyFT4222A libft4222 library python wrapper.DescriptionBuildingRun thecreate_dev_env.shcommand to create a virtual environment suitable for development:./create_dev_env.shInstallationThe pyft4222 package can be installed using pip:pipinstallpyft4222Use virtual environment preferably.udev ruleThe FT4222 device is not accessible by all users by default.
You can create a rule in/etc/udev/rules.d/99-ftdi.rulesto
make the device available to all users.# FTDI's ft4222 USB-I2C Adapter
SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="601c", GROUP="plugdev", MODE="0666"DocumentationUse mypy or other language server that supports Python types.
Library functions are easier to use with type hints.WIPExamplesOpen an SPI master stream:fromkodaimportErr,Okimportpyft4222asftfrompyft4222.streamimportInterfaceTypefrompyft4222.wrapper.spiimportClkPhase,ClkPolarityfrompyft4222.wrapper.spi.masterimportClkDiv,SsoMap# Print out list of connected devicesfordevinft.get_device_info_list():print(dev)# Open device using 'device index'dev=ft.open_by_idx(0)# Check if it was opened successfully# If using Python < 3.10, use "isinstance(dev, Ok)"matchdev:caseOk(handle):# Check if the FT4222 mode is as expectedifhandle.tag==InterfaceType.DATA_STREAM:# Use context manager to close the handle automatically at the end of scopewithhandle:# Initialize FT4222 in SPI master mode using a single-bit# full-duplex transferwithhandle.init_single_spi_master(ClkDiv.CLK_DIV_2,ClkPolarity.CLK_IDLE_LOW,ClkPhase.CLK_TRAILING,SsoMap.SS_0,)asspi_master:# Write and read back data simultaneouslyread_data=spi_master.single_read_write(bytes([0x01,0x02,0x03,0x04]))print("Data read: ")print(read_data)else:print("FT4222 is in invalid mode!")caseErr(err):print("Couldn't open the handle")print(err)Open an I2C slave stream:importpyft4222asftfrompyft4222.streamimportInterfaceTypefromkodaimportErr,Ok# Print out list of connected devicesfordevinft.get_device_info_list():print(dev)# Open device using 'device index'dev=ft.open_by_idx(0)# Check if it was opened successfully# If using Python < 3.10, use "isinstance(dev, Ok)"matchdev:caseOk(handle):# Check if the FT4222 mode is as expectedifhandle.tag==InterfaceType.DATA_STREAM:# The handle is closed automatically at the end of scopewithhandle:# Initialize the FT4222 in I2C slave mode# The handle is uninitialized automatically at the end of scopewithhandle.init_i2c_slave()asiic_slave:# Set an I2C device addressiic_slave.set_address(0x0340)# Write data into the device bufferiic_slave.write(bytes([0xFF,0x01,0x02,0x03]))else:print("FT4222 is in invalid mode!")caseErr(err):print("Couldn't open the handle")print(err)Open GPIO stream:fromkodaimportErr,Okimportpyft4222asftfrompyft4222.streamimportInterfaceTypefrompyft4222.wrapper.gpioimportDirection,PortId# Print out list of connected devicesfordevinft.get_device_info_list():print(dev)# Open device using 'device index'result=ft.open_by_idx(0)# Check if it was opened successfully# If using Python < 3.10, use "isinstance(dev, Ok)"matchresult:caseOk(handle):# Check if the FT4222 mode is as expectedifhandle.tag==InterfaceType.GPIO:# The handle is automatically closed at the end of the scopewithhandle:# The handle is automatically uninitialized at the end of the scopewithhandle.init_gpio((Direction.OUTPUT,Direction.INPUT,Direction.OUTPUT,Direction.OUTPUT,))asgpio:# Disable suspend out, else we cannot control GPIO2gpio.set_suspend_out(False)# Disable wakeup interrupt, else we cannot control GPIO3gpio.set_wakeup_interrupt(False)# Set GPIO port 0 into a logical 1 stategpio.write(PortId.PORT_0,True)# Read GPIO port 1 input stateport1_state=gpio.read(PortId.PORT_1)print(port1_state)else:print("FT4222 is in invalid mode!")caseErr(err):print("Couldn't open the handle")print(err)