PyFT4222
A libft4222 library python wrapper.
Description
Building
Run the create_dev_env.sh
command to create a virtual environment suitable for development:
./create_dev_env.sh
Installation
The pyft4222 package can be installed using pip:
pip install pyft4222
Use virtual environment preferably.
udev rule
The FT4222 device is not accessible by all users by default.
You can create a rule in /etc/udev/rules.d/99-ftdi.rules
to
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"
Documentation
Use mypy or other language server that supports Python types.
Library functions are easier to use with type hints.
WIP
Examples
Open an SPI master stream:
from koda import Err, Ok
import pyft4222 as ft
from pyft4222.stream import InterfaceType
from pyft4222.wrapper.spi import ClkPhase, ClkPolarity
from pyft4222.wrapper.spi.master import ClkDiv, SsoMap
for dev in ft.get_device_info_list():
print(dev)
dev = ft.open_by_idx(0)
match dev:
case Ok(handle):
if handle.tag == InterfaceType.DATA_STREAM:
with handle:
with handle.init_single_spi_master(
ClkDiv.CLK_DIV_2,
ClkPolarity.CLK_IDLE_LOW,
ClkPhase.CLK_TRAILING,
SsoMap.SS_0,
) as spi_master:
read_data = spi_master.single_read_write(
bytes([0x01, 0x02, 0x03, 0x04])
)
print("Data read: ")
print(read_data)
else:
print("FT4222 is in invalid mode!")
case Err(err):
print("Couldn't open the handle")
print(err)
Open an I2C slave stream:
import pyft4222 as ft
from pyft4222.stream import InterfaceType
from koda import Err, Ok
for dev in ft.get_device_info_list():
print(dev)
dev = ft.open_by_idx(0)
match dev:
case Ok(handle):
if handle.tag == InterfaceType.DATA_STREAM:
with handle:
with handle.init_i2c_slave() as iic_slave:
iic_slave.set_address(0x0340)
iic_slave.write(bytes([0xFF, 0x01, 0x02, 0x03]))
else:
print("FT4222 is in invalid mode!")
case Err(err):
print("Couldn't open the handle")
print(err)
Open GPIO stream:
from koda import Err, Ok
import pyft4222 as ft
from pyft4222.stream import InterfaceType
from pyft4222.wrapper.gpio import Direction, PortId
for dev in ft.get_device_info_list():
print(dev)
result = ft.open_by_idx(0)
match result:
case Ok(handle):
if handle.tag == InterfaceType.GPIO:
with handle:
with handle.init_gpio(
(
Direction.OUTPUT,
Direction.INPUT,
Direction.OUTPUT,
Direction.OUTPUT,
)
) as gpio:
gpio.set_suspend_out(False)
gpio.set_wakeup_interrupt(False)
gpio.write(PortId.PORT_0, True)
port1_state = gpio.read(PortId.PORT_1)
print(port1_state)
else:
print("FT4222 is in invalid mode!")
case Err(err):
print("Couldn't open the handle")
print(err)