API

Control FTDI USB chips for I2C/SPI/GPIO.

Open a handle using one of the ft4222.openBy… functions and use the methods on the object thus returned.

exception ft4222.FT2XXDeviceError(msgnum)

Exception class for status messages

exception ft4222.FT4222DeviceError(msgnum)

Exception class for status messages

class ft4222.SysClock(*values)

Chip system clock

CLK_60

60 MHz

CLK_24

24 MHz

CLK_48

48 MHz

CLK_80

80 MHz

ft4222.createDeviceInfoList()

Create the internal device info list and return number of entries

ft4222.getDeviceInfoDetail(devnum=0, update=True)

Get an entry from the internal device info list. Set update to False to avoid a slow call to createDeviceInfoList.

ft4222.openByDescription(desc)

Open a handle to a usb device by description

Parameters:

desc (bytes, str) – Description of the device

Returns:

Opened device

Return type:

FT4222

Raises:

FT2XXDeviceError – on error

ft4222.openByLocation(locId)

Open a handle to a usb device by location

Parameters:

locId (int) – Location id

Returns:

Opened device

Return type:

FT4222

Raises:

FT2XXDeviceError – on error

ft4222.openBySerial(serial)

Open a handle to a usb device by serial number

Parameters:

serial (bytes, str) – Serial number

Returns:

Opened device

Return type:

FT4222

Raises:

FT2XXDeviceError – on error

FT4222

class ft4222.FT4222
chipReset()

Software reset for device. This function is used to attempt to recover system after a failure. It is a software reset for device.

Re-opening the device may be needed after calling this function.

Raises:

FT4222DeviceError – on error

chipRevision

The revision of the chip in human readable format

chipVersion

Chip version as number

close()

Closes the device.

cyclePort()

Send a cycle command to the USB port.

The effect of this function is the same as disconnecting then reconnecting the device from USB. Possible use of this function is situations where a fatal error has occurred and it is difficult, or not possible, to recover without unplugging and replugging the USB cable. This function can also be used after reprogramming the EEPROM to force the FTDI device to read the new EEPROM contents which would otherwise require a physical disconnect-reconnect. As the current session is not restored when the driver is reloaded, the application must be able to recover after calling this function. It is ithe responisbility of the application to close the handle after successfully calling FT_CyclePort. For FT4232H, FT2232H and FT2232 devices, FT_CyclePort will only work under Windows XP and later.

Only available on Windows.

Raises:

FT2XXDeviceError – on error

getChipMode()

Get chip mode information.

This function is used to attempt to get chip mode information. The chip mode information is identified when the system power on as defined by the {DCNF1, DCNF0} pins. If the chip mode is changed after the system is powered, the chip mode value would keep the value from when the system was powered on.

Raises:

FT4222DeviceError – on error

getClock()

Get the system clock

Returns:

Clock

Return type:

ft4222.SysClock

Raises:

FT4222DeviceError – on error

getLatencyTimer()

Get the current value of the latency timer.

Returns:

Latency timer value in milliseconds

Return type:

int

Raises:

FT2XXDeviceError – on error

getMaxTransferSize()

Get the maximum transfer size for the currently configured operating mode

Returns:

Maximum number of bytes for each transfer.

Return type:

int

Raises:

FT4222DeviceError – on error

gpio_GetTriggerStatus(portNum)

Get the size of trigger event queue.

Parameters:

portNum (ft4222.GPIO.Port) – GPIO Port number

Returns:

Size of the trigger queue

Return type:

int

Raises:

FT4222DeviceError – on error

gpio_Init(*args, gpio0=Dir.INPUT, gpio1=Dir.INPUT, gpio2=Dir.INPUT, gpio3=Dir.INPUT)

Initialize the GPIO interface.

Parameters:
Raises:

FT4222DeviceError – on error

gpio_Read(portNum)

Read value from selected GPIO

Parameters:

portNum (ft4222.GPIO.Port) – GPIO Port number

Returns:

True if high, False otherwise

Return type:

bool

Raises:

FT4222DeviceError – on error

gpio_ReadTriggerQueue(portNum, readSize=None)

Get events recorded in the trigger event queue.

Parameters:
Returns:

Trigger queue as list

Return type:

list of ft4222.GPIO.Trigger

Raises:

FT4222DeviceError – on error

gpio_SetInputTrigger(portNum, trigger)

Set software trigger conditions on the specified GPIO pin.

Parameters:
Raises:

FT4222DeviceError – on error

gpio_Wait(portNum, expectedValue, timeout=0, sleep=1)

Blocks until the selected GPIO reaches a certain value.

Will release the GIL while waiting.

Parameters:
  • portNum (ft4222.GPIO.Port) – GPIO Port number

  • expectedValue (bool) – True for high, False for low

  • timeout (int) – timeout in milliseconds, or 0 for no timeout

  • sleep (int) – sleep time in milliseconds between each polling attempt, or 0 for no sleep

Raises:
gpio_Write(portNum, value)

Write value to given GPIO

Parameters:
Raises:

FT4222DeviceError – on error

i2cMaster_GetStatus()

Read the status of the I2C master controller.

This can be used to poll a slave until its write-cycle is complete.

Returns:

Controller status

Return type:

ft4222.I2CMaster.ControllerStatus

Raises:

FT4222DeviceError – on error

i2cMaster_Init(kbps=100)

Initialize the FT4222H as an I2C master with the requested I2C speed.

Parameters:

kbps (int) – Speed in kb/s

Raises:

FT4222DeviceError – on error

i2cMaster_Read(addr, bytesToRead)

Read data from the specified I2C slave device with START and STOP conditions.

Parameters:
  • addr (int) – I2C slave address

  • bytesToRead (int) – Number of bytes to read from slave

Returns:

Bytes read from slave

Return type:

bytes

Raises:

FT4222DeviceError – on error

i2cMaster_ReadEx(addr, flag, bytesToRead)

Read data from the specified I2C slave device with the specified I2C condition.

Parameters:
  • addr (int) – I2C slave address

  • flag (ft4222.I2CMaster.Flag) – Flag to control start- and stopbit generation

  • bytesToRead (int) – Number of bytes to read from slave

Returns:

Bytes read from slave

Return type:

bytes

Raises:

FT4222DeviceError – on error

i2cMaster_Reset()

Reset the I2C master device.

If the I2C bus encounters errors or works abnormally, this function will reset the I2C device. It is not necessary to call I2CMaster_Init again after calling this reset function.

Raises:

FT4222DeviceError – on error

i2cMaster_Write(addr, data)

Write data to the specified I2C slave device with START and STOP conditions.

Parameters:
Returns:

Bytes sent to slave

Return type:

int

Raises:

FT4222DeviceError – on error

i2cMaster_WriteEx(addr, flag, data)

Write data to the specified I2C slave device with the specified I2C condition.

Parameters:
Returns:

Bytes sent to slave

Return type:

int

Raises:

FT4222DeviceError – on error

i2cSlave_GetAddress()

Get the current address of the I2C slave device.

Returns:

current slave address

Return type:

int

Raises:

FT4222DeviceError – on error

i2cSlave_GetRxStatus()

Get the number of bytes in the receive queue.

Returns:

number of bytes in receive queue

Return type:

int

Raises:

FT4222DeviceError – on error

i2cSlave_Init()

Initialize the FT4222H as an I2C slave device (default address 0x40).

Raises:

FT4222DeviceError – on error

i2cSlave_Read(bytesToRead)

Read data from the buffer of the I2C slave device.

Parameters:

bytesToRead (int) – number of bytes to read from slave device buffer

Returns:

data read from the slave device buffer

Return type:

bytes

Raises:

FT4222DeviceError – on error

i2cSlave_Reset()

Reset the I2C slave device.

Maintains slave address, response word, and clock stretch enable. Clears any queued RX or TX buffer data.

Raises:

FT4222DeviceError – on error

i2cSlave_SetAddress(addr)

Set the address of the I2C slave device.

Parameters:

addr (int) – new I2C slave address

Raises:

FT4222DeviceError – on error

i2cSlave_SetClockStretch(enable)

Enable or disable I2C clock stretch (default disabled).

Only supported on chip revisions C & D

Parameters:

enable (bool) – enable clock stretch

Raises:

FT4222DeviceError – on error

i2cSlave_SetRespWord(response)

Response from I2C slave device when the device is not ready (default 0xFF).

Only takes effect when clock stretch is disabled. Only supported on chip revisions C & D.

Parameters:

response (int) – default response byte value

Raises:

FT4222DeviceError – on error

i2cSlave_Write(data)

Write data to the buffer of the I2C slave device.

Parameters:

data (int, bytes, bytearray) – data to write to I2C slave device buffer

Returns:

number of bytes written to the I2C slave device buffer

Return type:

int

Raises:

FT4222DeviceError – on error

libVersion

Library version as number

purge(mask=3)

This function purges receive and transmit buffers in the device.

Parameters:

mask (ft4222.Purge) – Buffer to purge

Raises:

FT2XXDeviceError – on error

resetDevice()

This function sends a reset command to the device.

Raises:

FT2XXDeviceError – on error

resetPort()

Send a reset command to the port.

This function is used to attempt to recover the port after a failure. It is not equivalent to an unplugreplug event. For the equivalent of an unplug-replug event, use cyclePort.

Only available on Windows.

Raises:

FT2XXDeviceError – on error

setClock(clk)

Set the system clock

Parameters:

clk (ft4222.SysClock) – Desired clock

Raises:

FT4222DeviceError – on error

setLatencyTimer(latency)

Set the current value of the latency timer.

Parameters:

latency (int) – Latency timer value in milliseconds. Valid range is 2 - 255

Raises:

FT2XXDeviceError – on error

setSuspendOut(enable)

Enable or disable, suspend out, which will emit a signal when FT4222H enters suspend mode.

Parameters:

enable (bool) – TRUE to enable suspend out and configure GPIO2 as an output pin for emitting a signal when suspended. FALSE to switch back to GPIO2

Raises:

FT4222DeviceError – on error

setTimeouts(read_timeout, write_timeout)

Set the read and write timeouts

Parameters:
  • read_timeout (int) – Read timeout in milliseconds.

  • write_timeout (int) – Write timeout in milliseconds.

Raises:

FT2XXDeviceError – on error

setWakeUpInterrupt(enable)

Enable or disable the wakeup/interrupt

Parameters:

enable (bool) – True to configure GPIO3 as an input pin for wakeup/interrupt. FALSE to switch back to GPIO3.

Raises:

FT4222DeviceError – on error

spiMaster_EndTransaction()

End the current SPI transaction.

Raises:

FT4222DeviceError – on error

spiMaster_Init(mode, clock, cpol, cpha, ssoMap)

Initialize as an SPI master under all modes.

Parameters:
Raises:

FT4222DeviceError – on error

spiMaster_MultiReadWrite(singleWrite, multiWrite, bytesToRead)

Write and read data to and from a SPI slave in dual- or quad-mode (multi-mode).

Parameters:
  • singleWrite (bytes, bytearray, int) – Data to write to slave in signle-line mode (max. 15 bytes)

  • multiWrite (bytes, bytearray, int) – Data to write to slave in multi-line mode (max. 65535 bytes)

  • bytesToRead (int) – Number of bytes to read on multi-line (max. 65535 bytes)

Returns:

Bytes read from slave in multi-line mode

Return type:

bytes

Raises:

FT4222DeviceError – on error

spiMaster_SetLines(mode)

Switch the FT4222H SPI master to single, dual, or quad mode.

This overrides the mode passed to FT4222_SPIMaster_init. This might be needed if a device accepts commands in single mode but data transfer is to use dual or quad mode.

Parameters:

mode (ft4222.SPIMaster.Mode) – SPI transmission lines / mode

Raises:

FT4222DeviceError – on error

spiMaster_SingleRead(bytesToRead, isEndTransaction)

Read data from a SPI slave in single mode

Parameters:
  • bytesToRead (int) – Number of bytes to read

  • isEndTransaction (bool) – If True the slave select pin will be raised at the end

Returns:

Bytes read from slave

Return type:

bytes

Raises:

FT4222DeviceError – on error

spiMaster_SingleReadWrite(data, isEndTransaction)

Write and read data to and from a SPI slave in single mode

Parameters:
  • data (bytes, bytearray, int) – Data to write to slave

  • isEndTransaction (bool) – If True the slave select pin will be raised at the end

Returns:

Bytes read from slave

Return type:

bytes

Raises:

FT4222DeviceError – on error

spiMaster_SingleWrite(data, isEndTransaction)

Write data to a SPI slave in single mode

Parameters:
  • data (bytes, bytearray, int) – Data to write to slave

  • isEndTransaction (bool) – If True the slave select pin will be raised at the end

Returns:

Bytes sent to slave

Return type:

int

Raises:

FT4222DeviceError – on error

spiSlave_GetRxStatus()

Get number of bytes in the receive queue.

Returns:

Number of bytes in the receive queue

Return type:

pRxSize (uint16)

Raises:

FT4222DeviceError – on error

spiSlave_Init()

Initialize the FT4222H as an SPI slave. Default SPI_SlaveProtocol is SPI_SLAVE_WITH_PROTOCOL.

Raises:

FT4222DeviceError – on error

spiSlave_InitEx(proto)

Initialize as an SPI slave under all modes.

Parameters:

mode (ft4222.SPISlave.Protocol) – SPI slave protocol

Raises:

FT4222DeviceError – on error

spiSlave_Read(bytesToRead)

Read data from the receive queue of the SPI slave device.

Parameters:

bytesToRead (int) – Number of bytes to read

Returns:

Bytes read from slave

Return type:

bytes

Raises:

FT4222DeviceError – on error

spiSlave_SetMode(cpol, cpha)

Set SPI slave cpol and cpha. The Default value of cpol is (ft4222.SPI.Cpol.CLK_IDLE_LOW) , default value of cpha is (ft4222.SPI.Cpol.CLK_LEADING)

Parameters:
Raises:

FT4222DeviceError – on error

spiSlave_Write(data)

Write data to the transmit queue of the SPI slave device.

Parameters:

data (bytes, bytearray, int) – Data to write to slave

Returns:

Number of bytes written to the device.

Return type:

sizeTransferred (uint16)

Raises:

FT4222DeviceError – on error

spi_Reset()

Reset the SPI master or slave device

Raises:

FT4222DeviceError – on error

spi_ResetTransaction(spiIdx)

Reset the SPI transaction

Parameters:

spiIdx (int) – The index of the SPI transaction, which ranges from 0~3 depending on the mode of the chip.

Raises:

FT4222DeviceError – on error

spi_SetDrivingStrength(clkStrength, ioStrength, ssoStrength)

Reset the SPI master or slave device.

Parameters:
Raises:

FT4222DeviceError – on error

vendorCmdGet(req, bytesToRead)

Vendor get command

vendorCmdSet(req, data)

Vendor set command

I2CMaster

I2C Master

Definitions to configure the I2C Master interface.

class ft4222.I2CMaster.ControllerStatus(*values)

I2CMaster controller Status

BUSY

controller busy: all other status bits invalid

ERROR

error condition

ADDRESS_NACK

slave address was not acknowledged during last operation

DATA_NACK

data not acknowledged during last operation

ARB_LOST

arbitration lost during last operation

IDLE

controller idle

BUS_BUSY

bus busy

class ft4222.I2CMaster.Flag(*values)

I2CMaster Flags

This flags control the start and stopbit generation during a I2C transfer

NONE

No start nor stopbit

START

Startbit

REPEATED_START

Repeated startbit (will not send master code in HS mode)

STOP

Stopbit

START_AND_STOP

Startbit and stopbit

GPIO

GPIO

Definitions to configure the GPIO interface.

class ft4222.GPIO.Dir(*values)

GPIO Output direction

OUTPUT

Use as output

INPUT

Use as input

class ft4222.GPIO.Output(*values)

GPIO Output

LOW

Logic low, 0

HIGH

Logic high, 1

class ft4222.GPIO.Port(*values)

GPIO Port

P0

Port 0

P1

Port 1

P2

Port 2

P3

Port 3

class ft4222.GPIO.Trigger(*values)

GPIO Trigger in ‘interrupt’ mode

RISING

Rising edge

FALLING

Falling edge

LEVEL_HIGH

High level

LEVEL_LOW

Low level

SPI

SPI

Definitions to configure either the SPI Master or Slave interface.

class ft4222.SPI.Cpha(*values)

SPI Phase

CLK_LEADING

Leading phase

CLK_TRAILING

Trailing phase

class ft4222.SPI.Cpol(*values)

SPI Polarization

IDLE_LOW

Idle low

IDLE_HIGH

Idle high

class ft4222.SPI.DrivingStrength(*values)

SPI Driving Strength

DS4MA
DS8MA
DS12MA
DS16MA

SPIMaster

SPI Master

Definitions to configure the SPI Master interface.

class ft4222.SPIMaster.Clock(*values)

SPIMaster Clock

NONE
DIV_2

1/2 System Clock

DIV_4

1/4 System Clock

DIV_8

1/8 System Clock

DIV_16

1/16 System Clock

DIV_32

1/32 System Clock

DIV_64

1/64 System Clock

DIV_128

1/128 System Clock

DIV_256

1/256 System Clock

DIV_512

1/512 System Clock

class ft4222.SPIMaster.Mode(*values)

SPIMaster Mode

SPI_IO_NONE

No IOs

SPI_IO_SINGLE

Single mode

SPI_IO_DUAL

Dual mode

SPI_IO_QUAD

Quad mode

class ft4222.SPIMaster.SlaveSelect(*values)

Slave Slave bitmap

SS0

Slave select 0

SS1

Slave select 1

SS2

Slave select 2

SS3

Slave select 3

SPISlave

SPI Slave

Definitions to configure the SPI Slave interface.

class ft4222.SPISlave.Protocol(*values)

SPI SLAVE protocol

SPI_SLAVE_WITH_PROTOCOL

With the full SPI SLAVE PROTOCOL supported

SPI_SLAVE_NO_PROTOCOL

Remove SPI SLAVE protocol, users can design their own protocol

SPI_SLAVE_NO_ACK

Retain SPI SLAVE protocol but remove command ‘ACK’