FTDI LibFT4222 User manual

Use of FTDI devices in life support and/or safety applications is entirely at the user’s risk,
and the user agrees to defend, indemnify and hold FTDI harmless from any and all
damages, claims, suits or expense resulting from such use.
Future Technology Devices International Limited (FTDI)
Unit 1, 2 Seaward Place, Glasgow G41 1HH, United Kingdom
Tel.: +44 (0) 141 429 2777 Fax: + 44 (0) 141 429 2758
Web Site: http://ftdichip.com
Copyright © Future Technology Devices International Limited
Application Note
AN_329
User Guide for LibFT4222
Version 1.5
Issue Date: 2020-04-15
The application note is a guide for LibFT4222 based on D2XX. It provides
high-level and convenient APIs for FT4222H application development.

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
1
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
Table of Contents
1Introduction .............................................................. 4
1.1 Overview .............................................................................5
1.2 Scope ..................................................................................7
2Getting Started .......................................................... 8
3Application Programming Interface (API) ............... 10
3.1 Typedefs............................................................................10
3.2 FT4222 General Functions .................................................10
3.2.1 Open and Close ................................................................................ 10
3.2.2 Un-initialize...................................................................................... 10
3.2.3 Set Clock ......................................................................................... 11
3.2.4 Get Clock ......................................................................................... 12
3.2.5 Set Suspend Out............................................................................... 13
3.2.6 Set Wake Up/Interrupt ...................................................................... 14
3.2.7 Set Interrupt Trigger Condition........................................................... 15
3.2.8 Get Max Transfer Size ....................................................................... 18
3.2.9 Set Event Notification ........................................................................ 19
3.2.10 Get Version ...................................................................................... 20
3.2.11 Chip Reset ....................................................................................... 22
3.3 SPI Master Functions ........................................................22
3.3.1 SPI Master Init ................................................................................. 23
3.3.2 SPI Master Set Lines ......................................................................... 25
3.3.3 SPI Master Single Read...................................................................... 26
3.3.4 SPI Master Single Write ..................................................................... 27
3.3.5 SPI Master Single Read and Write ....................................................... 28
3.3.6 SPI Master Multi Read and Write ......................................................... 31
3.4 SPI Slave Functions...........................................................33
3.4.1 SPI Slave Init ................................................................................... 36
3.4.2 SPI Slave Init extend function ............................................................ 37
3.4.3 SPI Slave Set mode function .............................................................. 38
3.4.4 SPI Slave Get Rx Status .................................................................... 39

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
2
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
3.4.5 SPI Slave Read ................................................................................. 39
3.4.6 SPI Slave Write ................................................................................ 41
3.4.7 SPI Slave RX Response Time .............................................................. 42
3.5 SPI General Functions .......................................................43
3.5.1 SPI Reset Transaction........................................................................ 43
3.5.2 SPI Reset......................................................................................... 44
3.5.3 SPI Set Driving Strength.................................................................... 45
3.6 I2C Master Functions .........................................................46
3.6.1 I2C Master Init ................................................................................. 46
3.6.2 I2C Master Read................................................................................ 47
3.6.3 I2C Master Write .............................................................................. 48
3.6.4 I2C Master Write Extension................................................................. 50
3.6.5 I2C Master Read Extension ................................................................. 51
3.6.6 I2C Master GetStatus......................................................................... 53
3.6.7 I2C Master Reset .............................................................................. 54
3.7 I2C Slave Functions ...........................................................54
3.7.1 I2C Slave Init................................................................................... 54
3.7.2 I2C Slave Get Address ...................................................................... 55
3.7.3 I2C Slave Set Address........................................................................ 56
3.7.4 I2C Slave Get Rx Status ..................................................................... 57
3.7.5 I2C Slave Read ................................................................................. 59
3.7.6 I2C Slave Write ................................................................................. 59
3.7.7 I2C Slave Reset................................................................................. 60
3.7.8 I2C Slave Clock Stretch ...................................................................... 62
3.7.9 I2C Slave Set Response Word ............................................................. 62
3.8 GPIO Functions .................................................................63
3.8.1 GPIO Init ......................................................................................... 63
3.8.2 GPIO Read ....................................................................................... 64
3.8.3 GPIO Write....................................................................................... 65
3.8.4 GPIO Set Input Trigger ...................................................................... 66
3.8.5 GPIO Get Trigger Status .................................................................... 67
3.8.6 GPIO Read Trigger Queue .................................................................. 68
3.8.7 GPIO Set WaveForm Mode ................................................................. 70

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
3
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
4Contact Information ................................................ 71
Appendix A –Enumeration and Structure Definitions .. 72
Appendix B –References ............................................. 74
Document References ...............................................................74
Acronyms and Abbreviations.....................................................74
Appendix C –List of Tables & Figures .......................... 75
List of Tables.............................................................................75
List of Figures ...........................................................................75
Appendix D –D2XX API support .................................. 76
D2XX supported API .................................................................76
1. FT_CreateDeviceInfoList .......................................................................... 76
Appendix E –Revision History ..................................... 77

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
4
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
1Introduction
The FT4222H is a USB interface device which supports SPI and I2C communication protocol. It is
accompanied with the support library “LibFT4222”based on D2XX, which provides high-level APIs
to facilitate user application development. At the time of writing support for Windows and Linux OS
has been published. Android support uses a different package also available from the FTDI website.
The FT4222H contains SPI/I2C configurable interfaces. The SPI interface can be configured as
master mode with single, dual, quad bits wide data transfer or slave mode with single bit wide
data transfer. The I2C interface can be configured as master or slave mode.
Figure 1.1 The Software Stack
Note that the Window, Linux and MAC OS version of LibFT4222 have D2XX and mini-boost built-in.
The LibFT4222 sample code, release notes, and all necessary files can be downloaded from the
FTDI website at: https://www.ftdichip.com/Products/ICs/FT4222H.html
The sample source code contained in this application note is provided as an example and is neither
guaranteed nor supported by FTDI.
User Application
LibFT4222
(SPI/I2C Library)
D2XX API
USB Bus driver
FTDI FT4222H chip

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
5
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
1.1 Overview
The FT4222H supports 4 operation modes to allow various I2C/SPI devices to be connected to USB
bus. The attachable device configuration for each mode is listed below:
Mode 0 (2 USB interfaces):
1 SPI master, SPI slave, I2C master, or I2C slave device
1 GPIO device
Mode 1 (4 USB interfaces):
SPI master connects up to 3 SPI slave devices
1 GPIO device
Mode 2 (4 USB interfaces):
SPI master connects up to 4 SPI slave devices
Mode 3 (1 USB interface):
1 SPI master, SPI slave, I2C master, or I2C slave device
In mode 0 and 3, the connected device can be a SPI/I2C master or slave, depending on how an
application developer initializes the FT4222H chip. Mode 1 and mode 2 are designed to connect to
multiple SPI slave devices.
The FT4222H can be configured with up to 4 GPIO pins for user applications in mode 0 and mode 1,
but each pin is multiplexed with interrupt/suspend out/SPI slave select/I2C functions as listed
below:
gpio0 / ss1o / scl
gpio1 / ss2o / sda
gpio2 / ss3o / suspend out
gpio3 / wakeup/intr
If the FT4222H is initialized as an I2C device, with pins as shown above, the pins of gpio0 and
gpio1 will be switched to scl and sda, and cannot be used as GPIO.
By default the pin for gpio2 is configured as suspend out, and the pin for gpio3 is configured as
wakeup/intr. Only those configured GPIO pins can support GPIO read/set operation through the
corresponding endpoint.
The following diagrams show the examples of FT4222H SPI/I2C master connections.

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
6
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
Figure 1.2 Mode 0: FT4222H works as SPI master (Quad Mode)
Figure 1.3 Mode 0: FT4222H works as I2C Master

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
7
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
Figure 1.4 Mode 2: FT4222H works as SPI Master
1.2 Scope
The guide is intended for developers who are creating applications, extending FTDI provided
applications or implementing FTDI’s applications for the FT4222H.

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
8
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
2Getting Started
A LibFT4222 application usually starts with FT_CreateDeviceInfoList and FT_GetDeviceInfoList as a
traditional D2XX application does. Under different chip modes, FT_CreateDeviceInfoList reports a
different number of interfaces as shown in the table below.
Mode
Number of
Interfaces
Device Function
0
2
a. The first interface: it can be one of SPI master, SPI slave, I2C master,
or I2C slave device.
b. The second interface: GPIO device.
1
4
a. The first 3 interfaces: SPI master connects up to 3 SPI slaves.
b. The 4th interface: GPIO device.
2
4
a. SPI master connects up to 4 SPI slaves. Please refer figure 1.4.
FT4222H works as SPI master.
3
1
a. it can be one of SPI master, SPI slave, I2C master, or I2C slave device.
Table 2.1 Chip Mode and Device Functions
After opening the device with FT_Open, developers need to initialize the FT4222H device as either
SPI master, SPI slave, I2C master, or I2C slave. Different types of device require different
configurations. For more details, please refer the next chapter.
Following example code shows FT4222H works in SPI master mode.
Example#
include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string>
#include "ftd2xx.h"
#include "LibFT4222.h"
std::vector< FT_DEVICE_LIST_INFO_NODE > g_FT4222DevList;
inline std::string DeviceFlagToString(DWORD flags)
{
std::string msg;
msg += (flags & 0x1)? "DEVICE_OPEN" : "DEVICE_CLOSED";
msg += ", ";
msg += (flags & 0x2)? "High-speed USB" : "Full-speed USB";
return msg;
}
void ListFtUsbDevices()
{
DWORD numOfDevices = 0;
FT_STATUS status = FT_CreateDeviceInfoList(&numOfDevices);
for(DWORD iDev=0; iDev<numOfDevices; ++iDev)
{
FT_DEVICE_LIST_INFO_NODE devInfo;
memset(&devInfo, 0, sizeof(devInfo));
status = FT_GetDeviceInfoDetail(iDev,
&devInfo.Flags, &devInfo.Type, &devInfo.ID, &devInfo.LocId,
devInfo.SerialNumber, devInfo.Description, &devInfo.ftHandle);
if (FT_OK == status)

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
9
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
{
printf("Dev %d:\n", iDev);
printf(" Flags= 0x%x, (%s)\n", devInfo.Flags,
DeviceFlagToString(devInfo.Flags).c_str());
printf(" Type= 0x%x\n", devInfo.Type);
printf(" ID= 0x%x\n", devInfo.ID);
printf(" LocId= 0x%x\n", devInfo.LocId);
printf(" SerialNumber= %s\n", devInfo.SerialNumber);
printf(" Description= %s\n", devInfo.Description);
printf(" ftHandle= 0x%x\n", devInfo.ftHandle);
const std::string desc = devInfo.Description;
if(desc == "FT4222" || desc == "FT4222 A") {
g_FT4222DevList.push_back(devInfo);
}
}
}
}
int main(int argc, char const *argv[])
{
ListFtUsbDevices();
if(g_FT4222DevList.empty()) {
printf("No FT4222 device is found!\n");
return 0;
}
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_OpenEx((PVOID)g_FT4222DevList[0].LocId,
FT_OPEN_BY_LOCATION, &ftHandle);
if (FT_OK != ftStatus) {
printf("Open a FT4222 device failed!\n");
return 0;
}
ft4222Status = FT4222_SPIMaster_Init(ftHandle,
SPI_IO_SINGLE, CLK_DIV_4, CLK_ACTIVE_LOW, CLK_LEADING, 0x01);
if (FT4222_OK != ft4222Status) {
printf("Init FT4222 as SPI master device failed!\n");
return 0;
}
// TODO:
// Start to work as SPI master, and read/write data to an SPI slave
// FT4222_SPIMaster_SingleWrite
// FT4222_SPIMaster_SingleRead
// FT4222_SPIMaster_SingleReadWrite
FT4222_Uninitialize(ftHandle);
FT_Close(ftHandle);
return 0;
}

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
10
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
3Application Programming Interface (API)
LibFT4222 supports SPI, I2C and GPIO communication using high-level APIs. In addition, it
provides chip configuration APIs, such as FT4222_SetClock.
After calling FT_Open, the FT4222H is required to be initialized by one of the following initial
functions:
FT4222_SPIMaster_Init
FT4222_SPISlave_Init
FT4222_I2CMaster_Init
FT4222_I2CSlave_Init
FT4222_GPIO_Init
The initialization functions help developers to switch the FT4222H into a specific mode.
At the end of the application, FT4222_Uninitialize should be called to release allocated resources,
before calling FT_Close.
All the APIs return an FT4222_STATUS, which extends FT_STATUS that is defined in the D2XX
driver. FT4222_STATUS defines additional values to report FT4222H specific status.
All definitions with prefix “FT_”is defined in the D2XX driver.
3.1 Typedefs
The following typedefs have been defined for keeping cross platform portability:
typedef unsigned long DWORD
typedef unsigned char uint8
typedef unsigned short uint16
typedef unsigned long uint32
typedef signed char int8
typedef signed short int16
typedef signed long int32
typedef unsigned char bool
Please refer to Appendix A for more enumeration and structure definitions.
3.2 FT4222 General Functions
The functions listed in this section are system-wise configuration functions.
3.2.1 Open and Close
An application of LibFT4222 should open the device and get a handle for subsequent accesses by
calling FT_Open or FT_OpenEx. Both are D2XX API. Please refer to the D2XX Programmers Guide
for more details. In addition, please note that the FT4222H assigns different functions to different
interfaces. For example, under mode 0, interface A is assigned as SPI or I2C interface, and
interface B is assigned as GPIO interface.
After finishing using the device, FT_Close should be called to release the device.
3.2.2 Un-initialize
FT4222_STATUS FT4222_UnInitialize (FT_HANDLE ftHandle)
Supported Chip:

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
11
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Release allocated resources. FT4222_Uninitialize should be called before calling FT_Close.
FT4222_Uninitialize must be called after one of the following API.
FT4222_SPIMaster_Init
FT4222_SPISlave_Init
FT4222_I2CMaster_Init
FT4222_I2CSlave_Init
FT4222_GPIO_Init
Parameters:
ftHandle
Handle of the device.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_OPENED: The initialization API is not called.
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_Open(0, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
ft4222Status = FT4222_SPIMaster_Init(ftHandle, SPI_IO_SINGLE, CLK_DIV_4, CLK_IDLE_LOW,
CLK_LEADING, 0x01);
if (FT4222_OK != ft4222Status)
{
// spi master init failed
return;
}
FT4222_UnInitialize(ftHandle);
FT_Close(ftHandle);
3.2.3 Set Clock
FT4222_STATUS FT4222_SetClock(FT_HANDLE ftHandle, FT4222_ClockRate clk)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
12
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Set the system clock rate. The FT4222H supports 4 clock rates: 80MHz, 60MHz, 48MHz, or 24MHz.
By default, the FT4222H runs at 60MHz clock rate.
Parameters:
ftHandle
Handle of the device.
clk
FT4222 system clock rate:
SYS_CLK_60
SYS_CLK_24
SYS_CLK_48
SYS_CLK_80
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_SUPPORTED: This device is not a FT4222 chip.
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_Open(0, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
// set system clock to 80MHz
ft4222Status = FT4222_SetClock(ftHandle, SYS_CLK_80);
if (FT4222_OK != ft4222Status)
{
// set clock failed
return;
}
FT_Close(ftHandle);
3.2.4 Get Clock
FT4222_STATUS FT4222_GetClock(FT_HANDLE ftHandle, FT4222_ClockRate* pClk)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
13
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
Summary:
Get the current system clock rate.
Parameters:
ftHandle
Handle of the device.
pClk
Pointer to a variable of type FT4222_ClockRate where the value will be stored.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_SUPPORTED: This device is not a FT4222 chip.
FT4222_INVALID_POINTER: Parameter pClk is NULL.
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
FT4222_ClockRate clk;
ftStatus = FT_Open(0, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
ft4222Status = FT4222_GetClock(ftHandle, &clk);
if (FT4222_OK != ft4222Status)
{
// get clock failed
return;
}
FT_Close(ftHandle);
3.2.5 Set Suspend Out
FT4222_STATUS FT4222_SetSuspendOut(FT_HANDLE ftHandle, BOOL enable)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Enable or disable, suspend out, which will emit a signal when FT4222H enters suspend mode.
Please note that the suspend-out pin is not available under mode 2. By default, suspend-out
function is on.
When suspend-out function is on, suspend-out pin emits signal according to suspend-out polarity.

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
14
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
The default value of suspend-out polarity is active high. It means suspend-out pin output low in
normal mode and output high in suspend mode. Suspend-out polarity only can be adjusted by
FT_PROG.
Parameters:
ftHandle
Handle of the device.
Enable
TRUE to enable suspend out and configure GPIO2 as an output pin for emitting a
signal when suspended.
FALSE to switch back to GPIO2.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_SUPPORTED: This device is not a FT4222 chip.
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_Open(0, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
ft4222Status = FT4222_SetSuspendOut(ftHandle, TRUE);
if (FT4222_OK != ft4222Status)
{
// set suspend failed
return;
}
FT_Close(ftHandle);
3.2.6 Set Wake Up/Interrupt
FT4222_STATUS FT4222_SetWakeUpInterrupt(FT_HANDLE ftHandle, BOOL enable)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Enable or disable wakeup/interrupt. By default, wake-up/interrupt function is on.
When Wake up/Interrupt function is on, GPIO3 pin acts as an input pin for wakeup/interrupt.
While system is in normal mode, GPIO3 acts as an interrupt pin. While system is in suspend mode,

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
15
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
GPIO3 acts as a wakeup pin. An example is provided with the support-lib. The file is located in the
following path:
example\samples\interrupt\interrupt.cpp
Parameters:
ftHandle
Handle of the device.
enable
TRUE to configure GPIO3 as an input pin for wakeup/interrupt.
FALSE to switch back to GPIO3.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_SUPPORTED: This device is not a FT4222 chip.
Example:
Please refer to the example in FT4222_SetInterruptTrigger.
3.2.7 Set Interrupt Trigger Condition
FT4222_STATUS FT4222_SetInterruptTrigger(FT_HANDLE ftHandle, GPIO_Trigger trigger)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Set trigger condition for the pin wakeup/interrupt. By default, the trigger condition is
GPIO_TRIGGER_RISING.
This function configures trigger condition for wakeup/interrupt.
When GPIO3 acts as wakeup pin. It means that ft4222H device has the capability to wake up the
host. Only GPIO_TRIGGER_RISING and GPIO_TRIGGER_FALLING are valid when GPIO3 act as a
wakeup pin. . It is not necessary to call FT4222_GPIO_Init to set up wake-up function.
When GPIO3 acts as interrupt pin. All trigger condition can be set. The result of trigger status can
be inquired by FT4222_GPIO_ReadTriggerQueue or FT4222_GPIO_Read. This is because the
trigger status is provided by the GPIO pipe. Therefore it is necessary to call FT4222_GPIO_Init to
set up interrupt function.
For GPIO triggering conditions, GPIO_TRIGGER_LEVEL_HIGH and GPIO_TRIGGER_LEVEL_LOW,
that can be configured when GPIO3 behaves as an interrupt pin, when the system enters suspend
mode, these two configurations will act as GPIO_TRIGGER_RISING and GPIO_FALLING
respectively.

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
16
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
Parameters:
ftHandle
Handle of the device.
trigger
Trigger condition. One of the following:
GPIO_TRIGGER_RISING
GPIO_TRIGGER_FALLING
GPIO_TRIGGER_LEVEL_HIGH
GPIO_TRIGGER_LEVEL_LOW
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_SUPPORTED: This device is not a FT4222 chip.
FT4222_DEVICE_NOT_OPENED: The initialization API is not called.
FT4222_INTERRUPT_NOT_SUPPORTED: interrupt/wakeup is disabled.
FT4222_INVALID_PARAMETER: parameter trigger is invalid
Example:
// example 1: This test code is running in Mode 0.
// sending notification while there is an interrupt happen.
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_OpenEx("FT4222 B",FT_OPEN_BY_DESCRIPTION, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
HANDLE hRxEvent;
hRxEvent = CreateEvent(
NULL,
false, // auto-reset event
false, // non-signalled state
NULL ); ftStatus = FT_SetEventNotification(ftHandle, FT_EVENT_RXCHAR,
hRxEvent);
if (FT_OK != ftStatus)
{
// FT_SetEventNotification failed
return ;
}
GPIO_Dir gpioDir[4];
gpioDir[0] = GPIO_OUTPUT;
gpioDir[1] = GPIO_OUTPUT;
gpioDir[2] = GPIO_OUTPUT;
gpioDir[3] = GPIO_INPUT;
// we must initialize gpio before FT4222_SetInterruptTrigger, because interrupt data is transmitted
by gpio interface.
FT4222_GPIO_Init(ftHandle, gpioDir);
// enable interrupt
FT4222_SetWakeUpInterrupt(ftHandle, true);
// setup interrupt trigger level
FT4222_SetInterruptTrigger(ftHandle, GPIO_TRIGGER_RISING);
while(1)

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
17
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
{
BOOL value;
WaitForSingleObject(hRxEvent,INFINITE);
// FT4222_GPIO_Read is a read clear function for interrupt
if(FT4222_GPIO_Read(ftHandle, (GPIO_Port)GPIO_PORT3, &value) == FT4222_OK)
{
if(value == TRUE)
{
// got interrupt
}
else
{
// no interrupt
}
}
}
FT4222_UnInitialize(ftHandle);
FT_Close(ftHandle);
// example 2: This test code is running in Mode 0.
// Monitor how many interrupts happen in a period of time.
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
ftStatus = FT_OpenEx("FT4222 B",FT_OPEN_BY_DESCRIPTION, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
GPIO_Dir gpioDir[4];
gpioDir[0] = GPIO_OUTPUT;
gpioDir[1] = GPIO_OUTPUT;
gpioDir[2] = GPIO_OUTPUT;
gpioDir[3] = GPIO_INPUT;
// we must initial gpio before FT4222_SetInterruptTrigger, because interrupt data is transmitted
by gpio interface.
FT4222_GPIO_Init(ftHandle, gpioDir);
// enable interrupt
FT4222_SetWakeUpInterrupt(ftHandle, true);
// setup interrupt trigger level
FT4222_SetInterruptTrigger(ftHandle, GPIO_TRIGGER_RISING);
while(1)
{
uint16 queueSize;
// sleep 1s
Sleep(1000);
if(FT4222_GPIO_GetTriggerStatus(ftHandle, GPIO_PORT3, &queueSize) == FT4222_OK)
{
// got interrupt times in 1s
if(queueSize > 0)
{
BOOL value;
// clear the interrupt result
FT4222_GPIO_Read(ftHandle, (GPIO_Port)GPIO_PORT3, &value);

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
18
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
}
}
}
FT4222_UnInitialize(ftHandle);
FT_Close(ftHandle);
3.2.8 Get Max Transfer Size
FT4222_STATUS FT4222_GetMaxTransferSize(FT_HANDLE ftHandle, uint16* pMaxSize)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
This function returns the maximum packet size in a transaction. It will be affected by different bus
speeds, chip modes, and functions. The maximum transfer size is maximum size in writing path.
Parameters:
ftHandle
Handle of the device.
pMaxSize
Pointer to a variable of type unit16 where the returning value will be stored.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_OPENED: The initialization API is not called.
FT4222_INVALID_POINTER: Parameter pMaxSize is NULL
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
uint16 maxSize;
ftStatus = FT_Open(0, &ftHandle);
if (FT_OK != ftStatus)
{
// open failed
return;
}
ft4222Status = FT4222_I2CMaster_Init(ftHandle, 1000);
if (FT4222_OK != ft4222Status)
{
// init i2c master failed
return;
}
ft4222Status = FT4222_GetMaxTransferSize(ftHandle, &maxSize);
if (FT4222_OK != ft4222Status)
{
// get max transfer size failed

Application Note
AN_329 User Guide For LibFT4222
Version 1.5
Document Reference No.: FT_001060 Clearance No.: FTDI#406
19
Product Page
Document Feedback Copyright © Future Technology Devices International Limited
return;
}
FT_Close(ftHandle);
3.2.9 Set Event Notification
FT4222_STATUS FT4222_SetEventNotification(FT_HANDLE ftHandle, DWORD dwEventMask,
PVOID pvArg)
Supported Chip:
FT4222 chip version
Supported
FT4222 Rev A
YES
FT4222 Rev B
YES
FT4222 Rev C
YES
FT4222 Rev D
YES
Summary:
Sets conditions for event notification.
An application can use this function to set up conditions which allow a thread to block until one of
the conditions is met. Typically, an application will create an event, call this function, and then
block on the event. When the conditions are met, the event is set, and the application thread
unblocked. Usually, the event is set to notify the application to check the condition. The application
needs to check the condition again before it goes to handle the condition. The API is only valid
when the device acts as SPI slave and SPI slave protocol is not SPI_SLAVE_NO_PROTOCOL.
Parameters:
ftHandle
Handle of the device.
dwEventMask
Conditions that cause the event to be set. It is a bit-map that describes the
events the application is interested in. Currently, this function only supports the
event below:
FT4222_EVENT_RXCHAR
The event will be set when a data packet has been received by the
device.
pvArg
Interpreted as the handle of an event which has been created by the application.
If one of the event conditions is met, the event is set.
Return Value:
FT4222_OK if successful, otherwise the return value is an FT error code.
Error code:
FT4222_DEVICE_NOT_OPENED: The initialization API is not called.
FT4222_EVENT_NOT_SUPPORTED: The device must acts as SPI salve and protocol is not
SPI_SLAVE_NO_PROTOCOL.
Prerequisite:
FT4222_SPISlave_InitEx or FT4222_SPISlave_Init
Example:
FT_HANDLE ftHandle = NULL;
FT_STATUS ftStatus;
FT4222_STATUS ft4222Status;
Table of contents