Application Note AN_329 User Guide For LibFT4222 Version 1.1 Issue Date: 2015-09-10 The application note is a guide for LibFT4222 based on D2XX. It provides high-level and convenient APIs for FT4222H application development. 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 © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Table of Contents 1 Introduction .............................................................. 4 1.1 Overview .............................................................................5 1.2 Scope ..................................................................................7 2 Getting Started .......................................................... 8 3 Application Programming Interface (API) ............... 11 3.1 Typedefs............................................................................ 11 3.2 FT4222 General Functions ................................................. 11 3.2.1 Open and Close ............................................................................................ 11 3.2.2 Un-initialize ................................................................................................. 12 3.2.3 Set Clock ..................................................................................................... 12 3.2.4 Get Clock..................................................................................................... 13 3.2.5 Set Suspend Out .......................................................................................... 13 3.2.6 Set Wake Up/Interrupt .................................................................................. 13 3.2.7 Set Interrupt Trigger Condition....................................................................... 14 3.2.8 Get Max Transfer Size ................................................................................... 14 3.2.9 Set Event Notification .................................................................................... 15 3.2.10 Get Version .................................................................................................. 16 3.3 SPI Master Functions ........................................................ 17 3.3.1 SPI Master Init ............................................................................................. 17 3.3.2 SPI Master Set Lines ..................................................................................... 19 3.3.3 SPI Master Single Read ................................................................................. 19 3.3.4 SPI Master Single Write ................................................................................. 20 3.3.5 SPI Master Single Read and Write ................................................................... 20 3.3.6 SPI Master Multi Read and Write ..................................................................... 21 3.4 SPI Slave Functions ........................................................... 24 3.4.1 SPI Slave Init ............................................................................................... 26 3.4.2 SPI Slave Get Rx Status ................................................................................ 27 3.4.3 SPI Slave Read ............................................................................................. 27 3.4.4 SPI Slave Write ............................................................................................ 27 3.5 SPI General Functions ....................................................... 29 3.5.1 SPI Reset Transaction ................................................................................... 29 Product Page Document Feedback 1 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.5.2 SPI Reset .................................................................................................... 29 3.5.3 SPI Set Driving Strength ............................................................................... 30 3.6 I2C Master Functions ......................................................... 31 3.6.1 I2C Master Init ............................................................................................. 31 3.6.2 I2C Master Read ........................................................................................... 31 3.6.3 I2C Master Write .......................................................................................... 32 3.6.4 I2C Master Write Extension ............................................................................ 32 3.6.5 I2C Master Read Extension ............................................................................ 33 3.6.6 I2C Master GetStatus .................................................................................... 34 3.6.7 I2C Master Reset .......................................................................................... 35 3.7 I2C Slave Functions ........................................................... 36 3.7.1 I2C Slave Init ............................................................................................... 36 3.7.2 I2C Slave Get Address .................................................................................. 36 3.7.3 I2C Slave Set Address ................................................................................... 37 3.7.4 I2C Slave Get Rx Status ................................................................................ 37 3.7.5 I2C Slave Read ............................................................................................ 37 3.7.6 I2C Slave Write ............................................................................................ 38 3.7.7 I2C Slave Reset ............................................................................................ 38 3.8 GPIO Functions .................................................................39 3.8.1 GPIO Init ..................................................................................................... 39 3.8.2 GPIO Read ................................................................................................... 40 3.8.3 GPIO Write .................................................................................................. 40 3.8.4 GPIO Set Input Trigger .................................................................................. 41 3.8.5 GPIO Get Trigger Status ................................................................................ 42 3.8.6 GPIO Read Trigger Queue .............................................................................. 42 4 Contact Information ................................................ 44 Appendix A – Enumeration and Structure Definitions .. 45 Appendix B – References ............................................. 48 Document References ............................................................... 48 Acronyms and Abbreviations..................................................... 48 Appendix C – List of Tables & Figures .......................... 49 List of Tables............................................................................. 49 List of Figures ........................................................................... 49 Product Page Document Feedback 2 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Appendix D – Revision History ..................................... 50 Product Page Document Feedback 3 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 1 Introduction 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. User Application LibFT4222 (SPI/I2C Library) D2XX API USB Bus driver FTDI FT4222H chip Figure 1.1 The software stack Note that the Linux version of LibFT4222 has D2XX built-in. The LibFT4222 sample code, release notes, and all necessary files can be downloaded from the FTDI website at: http://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. Product Page Document Feedback 4 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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. Product Page Document Feedback 5 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Figure 1.2 Mode 0: FT4222H works as SPI master (quad mode) Figure 1.3 Mode 0: FT4222H works as I2C master Product Page Document Feedback 6 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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. Product Page Document Feedback 7 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 2 Getting 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 Num of Interfaces 0 2 Device Function 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. a. The first 3 interfaces: SPI master connects up to 3 SPI slaves. 1 4 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. b. The 4th interface: GPIO 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) { Product Page Document Feedback 8 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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) { 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); } Product Page Document Feedback 9 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 } } } 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; 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; } ftStatus = FT4222_SPIMaster_Init(ftHandle, SPI_IO_SINGLE, CLK_DIV_4, CLK_ACTIVE_LOW, CLK_LEADING, 0x01); if (FT_OK != ftStatus) { 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; } Product Page Document Feedback 10 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3 Application 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. 3.1 Typedefs The following typedefs have been defined for keeping cross platform portability: typedef typedef typedef typedef typedef typedef typedef typedef unsigned long DWORD unsigned char uint8 unsigned short uint16 unsigned long uint32 signed char int8 signed short int16 signed long int32 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. Product Page Document Feedback 11 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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) Summary: Release allocated resources. FT4222_Uninitialize should be called before calling FT_Close. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.2.3 Set Clock FT4222_STATUS FT4222_SetClock(FT_HANDLE ftHandle, FT4222_ClockRate clk) 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. Product Page Document Feedback 12 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.2.4 Get Clock FT4222_STATUS FT4222_GetClock(FT_HANDLE ftHandle, FT4222_ClockRate* pClk) 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. 3.2.5 Set Suspend Out FT4222_STATUS FT4222_SetSuspendOut(FT_HANDLE ftHandle, BOOL enable) 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. 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. 3.2.6 Set Wake Up/Interrupt FT4222_STATUS FT4222_SetWakeUpInterrupt(FT_HANDLE ftHandle, BOOL enable) Summary: Enable or disable wakeup/interrupt. Product Page Document Feedback 13 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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. 3.2.7 Set Interrupt Trigger Condition FT4222_STATUS FT4222_SetInterruptTrigger(FT_HANDLE ftHandle, GPIO_Trigger trigger) Summary: Set trigger condition for the pin wakeup/interrupt 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. 3.2.8 Get Max Transfer Size FT4222_STATUS pMaxSize) FT4222_GetMaxTransferSize(FT_HANDLE ftHandle, uint16* Summary: This function returns the maximum packet size in a transaction. It will be affected by different bus speeds, chip modes, and functions. Product Page Document Feedback 14 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 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. 3.2.9 Set Event Notification FT4222_STATUS FT4222_SetEventNotification(FT_HANDLE dwEventMask, PVOID pvArg) ftHandle, DWORD 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. 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: pvArg FT4222_EVENT_RXCHAR The event will be set when a data packet has been received by the device. 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. Product Page Document Feedback 15 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.2.10 Get Version FT4222_STATUS FT4222_GetVersion(FT_HANDLE ftHandle, FT4222_Version* pVersion) Summary: Get the versions of FT4222H and LibFT4222. Parameters: ftHandle Handle of the device. pVersion Pointer to a variable of type FT4222_Version where the value will be stored. Type FT4222_Version is defined as follows: struct FT4222_Version { DWORD chipVersion; // The version of FT4222H chip DWORD dllVersion; // The version of LibFT4222 }; Revision A chips report chipVersion as 0x42220100; revision B chips report 0x42220200. Revision B chips require version 1.2 or later of LibFT4222, indicated by dllVersion being greater than 0x01020000. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 16 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.3 SPI Master Functions The FT4222H can be initialized as an SPI master under all modes. As SPI master, it allows data transfers in three types of bit width: Single SPI transfer – Standard data transfer format – data is read and written simultaneously DUAL SPI Transfer/Receive - Data is transferred out or received in on 2 SPI lines simultaneously QUAD SPI Transfer/Receive – Data is transferred out or received in on 4 SPI lines simultaneously Please refer to DS_FT4222H for more details. 3.3.1 SPI Master Init FT4222_STATUS FT4222_SPIMaster_Init(FT_HANDLE ftHandle, FT4222_SPIMode ioLine, FT4222_SPIClock clock_div, FT4222_SPICPOL cpol, FT4222_SPICPHA cpha, uint8 ssoMap) Summary: Initialize the FT4222H as an SPI master. In order to support various types of SPI slave devices, the FT4222H SPI master is configurable using the following parameters: IO lines: SPI transmission lines. The FT4222H SPI supports single, dual, or quad transmission mode. An application may override this initial selection dynamically using FT4222_SPIMaster_SetLines. For example, commands might be sent in single mode but data transferred in dual or quad mode. Clock divider: SPI clock rate is subject to system clock. The FT4222H SPI clock could be 1/2, 1/4, 1/8, 1/16, 1/32, 1/64, 1/128, 1/256, or 1/512 system clock rate. Clock polarity: Active high or active low. Clock phase: Data is sampled on the leading (first) or trailing (second) clock edge. Slave selection output pins: Select slave devices by ss0o, ss1o, ss2o, ss3o. The FT4222H supports active low only. Please note that the FT4222H has only one SPI controller. Even though the FT4222H provides up to 4 interfaces for connecting up to 4 SPI slave devices as the Figure 1.4, the 4 slave devices share the same SPI data bus: MOSI, MISO, and SCK. A user can decide how to map the 4 interfaces to the 4 SS signals (ss0o, ss1o, ss2o and ss3o) by the ssoMap parameter. For example: a. interface 0 for ss0o and ss1o b. interface 1 for ss2o c. interface 2 for ss3o d. interface 3 is not used The 4 interfaces cannot work simultaneously because there is only one data bus. Product Page Document Feedback 17 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Parameters: ftHandle Handle of the device. ioLine SPI transmission lines: clock_div cpol cpha ssoMap SPI_IO_SINGLE SPI_IO_DUAL SPI_IO_QUAD Clock divider: CLK_DIV_2 (1/2 System Clock) CLK_DIV_4 (1/4 System Clock) CLK_DIV_8 (1/8 System Clock) CLK_DIV_16 (1/16 System Clock) CLK_DIV_32 (1/32 System Clock) CLK_DIV_64 (1/64 System Clock) CLK_DIV_128 (1/128 System Clock) CLK_DIV_256 (1/256 System Clock) CLK_DIV_512 (1/512 System Clock) Clock polarity: CLK_IDLE_LOW CLK_IDLE_HIGH Clock phase: CLK_LEADING CLK_TRAILING Slave selection output pins. It’s a bitmap: Bit 0: select device connected with ss0o Bit 1: select device connected with ss1o Bit 2: select device connected with ss2o Bit 3: select device connected with ss3o Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 18 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.3.2 SPI Master Set Lines FT4222_STATUS FT4222_SPIMaster_SetLines(FT_HANDLE ftHandle, FT4222_SPIMode spiMode) Summary: 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: ftHandle Handle of the device. spiMode SPI mode could be: SPI_IO_SINGLE SPI_IO_DUAL SPI_IO_QUAD Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.3.3 SPI Master Single Read FT4222_STATUS FT4222_SPIMaster_SingleRead(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToRead, uint16* sizeOfRead, BOOL isEndTransaction) Summary: Under SPI single mode, read data from an SPI slave. Parameters: ftHandle Handle of the device. Buffer Pointer to the buffer that receives the data from the device. bytesToRead Number of bytes to read from the device. sizeOfRead Pointer to a variable of type uint16 which receives the number of bytes read from the device. isEndTransaction If TRUE the Slave Select pin will be raised at the end of the read. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 19 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.3.4 SPI Master Single Write FT4222_STATUS FT4222_SPIMaster_SingleWrite(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToWrite, uint16* sizeTransferred, BOOL isEndTransaction) Summary: Under SPI single mode, write data to an SPI slave. Parameters: ftHandle Handle of the device. Buffer Pointer to the buffer that contains the data to be written to the device. bytesToWrite Number of bytes to write to the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes written to the device. isEndTransaction If TRUE the Slave Select pin will be raised at the end of the write. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.3.5 SPI Master Single Read and Write FT4222_STATUS FT4222_SPIMaster_SingleReadWrite(FT_HANDLE ftHandle, uint8* readBuffer, uint8* writeBuffer, uint16 sizeToTransfer, uint16* sizeTransferred, BOOL isEndTransaction) Summary: Under SPI single mode, full-duplex write data to and read data from an SPI slave. The standard SPI protocol simultaneously sends data onto the MOSI data line and receives data from the MISO line as shown below. Product Page Document Feedback 20 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Figure 3.1 SPI full duplex communication Parameters: ftHandle Handle of the device. readBuffer Pointer to the buffer that receives data from the device. writeBuffer Pointer to the buffer that contains data to be written to the device. sizeToTransfer The size of read and write buffer. They must be the same. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes read and written to the device. isEndTransaction TRUE to raise the pin of SS at the end of the transaction. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.3.6 SPI Master Multi Read and Write FT4222_STATUS FT4222_SPIMaster_MultiReadWrite(FT_HANDLE ftHandle, uint8* readBuffer, uint8* writeBuffer, uint8 singleWriteBytes,uint16 multiWriteBytes,uint16 multiReadBytes, uint32* sizeOfRead) Summary: Under SPI dual or quad mode, write data to and read data from an SPI slave. The figure below illustrates the dual-SPI protocol supported by the FT4222H SPI master. It is a mixed protocol initiated with a single write transmission, which may be an SPI command and dummy cycles, and followed by dual-write and dual-read transmission that use 2 signals in parallel for the data. All three parts of the protocol are optional. For example, developers can ignore the multi-read part by setting multiReadBytes=0. Product Page Document Feedback 21 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Figure 3.2 Dual SPI communication The figure below illustrates the quad-SPI protocol supported by the FT4222H SPI master. It is the same as the dual-protocol illustrated above - it is a mixed protocol initiated with a single write transmission and followed by quad-write and quad-read transmission that use 4 signals in parallel for the data. Figure 3.3 Quad SPI communication Parameters: ftHandle Handle of the device. readBuffer Pointer to the buffer that receives the data from the device. writeBuffer Pointer to the buffer that contains the data to be written to the device. The data is comprised of both single-write and multi-write parts. It starts with single-write data, whose length is specified by singleWriteBytes, and followed by multi-write data, whose length is specified by multiWriteBytes. singleWriteBytes Number of bytes in writeBuffer will be written on single-line. multiWriteBytes Number of bytes in writeBuffer will be written on multi-line. Product Page Document Feedback 22 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 multiReadBytes Number of bytes to read on multi-line. sizeOfRead Pointer to a variable of type uint16 which receives the number of bytes read from the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 23 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.4 SPI Slave Functions The FT4222H can be initialized as an SPI slave under mode 0 to mode 3. As an SPI slave, the FT4222H only supports the standard single SPI transfer. Refer to DS_FT4222H for more details. A USB-SPI bridge usually faces the challenge that USB cannot guarantee the throughput for each endpoint, but SPI requires data transmission at a steady rate. It is highly possible when an SPI master starts to request data from a USB-SPI slave bridge device, the data has not arrived from the USB host side yet. In addition, SPI does not have a standard protocol to allow the master side to check the status of the slave side. The protocol is usually provided by an SPI slave device on its own, which makes the SPI master device communicate with the slave device by its specified commands. The FT4222H and LibFT4222 design have implemented an SPI slave protocol which must be used to handle the integrity of data transmission. In this protocol, a master starts an SPI transaction by sending a packet in the format illustrated below: Figure 3.4 SPI Slave Protocol Format It starts with Sync word: 0x5A, and followed by a Command field: Command Value Master Transfer 0x80 Slave Transfer 0x81 Short master transfer (without checksum) 0x82 Short slave transfer (without checksum) 0x83 ACK 0x84 Product Page Document Feedback 24 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 SN stands for serial number. It is monotonically increased, and helps to identify packets. Size is a two-byte field, which is the size of the data field in big-endian order. The Checksum is the summation of all data fields’ lower two bytes starting from the first byte, the sync word, to the latest data byte. The checksum is in big-endian order as well. When the slave, FT4222H, receives the transfer request from the master, it will respond with an ACK. The master can confirm the transaction succeeded when it receives the ACK from the slave. Figure 3.5 SPI Master transfer request Here is an example of an ACK packet. The SN field of the ACK packet identifies which request it corresponds to. An ACK packet has no data therefore the Size field should be 0. Figure 3.6 An example of the SPI slave responding with ACK If the SPI master does not receive the ACK response from the slave, it should send its request again. Product Page Document Feedback 25 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Figure 3.7 An example of when the SPI master doesn’t receive ACK When the FT4222H SPI slave wants to send data to the master, which may be requested by the master, it just sends a transfer request in the same protocol format as shown in figure 3.4. Figure 3.8 Slave sends transfer request 3.4.1 SPI Slave Init FT4222_STATUS FT4222_SPISlave_Init(FT_HANDLE ftHandle) Summary: Initialize the FT4222H as an SPI slave. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 26 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.4.2 SPI Slave Get Rx Status FT4222_STATUS pRxSize) FT4222_SPISlave_GetRxStatus(FT_HANDLE ftHandle, uint16* Summary: Get number of bytes in the receive queue. Parameters: ftHandle Handle of the device. pRxSize Pointer to a variable of type uint16 which receives the number of bytes in the receive queue. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.4.3 SPI Slave Read FT4222_STATUS FT4222_SPISlave_Read(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToRead, uint16* sizeOfRead) Summary: Read data from the receive queue of the SPI slave device. Parameters: ftHandle Handle of the device. buffer Pointer to the buffer that receives the data from the device. bytesToRead Number of bytes to read from the device. sizeOfRead Pointer to a variable of type uint16 which receives the number of bytes read from the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.4.4 SPI Slave Write FT4222_STATUS FT4222_SPISlave_Write(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToWrite, uint16* sizeTransferred) Summary: Write data to the transmit queue of the SPI slave device. Product Page Document Feedback 27 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Parameters: ftHandle Handle of the device. buffer Pointer to the buffer that contains the data to be written to the device. bytesToWrite Number of bytes to write to the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes written to the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 28 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.5 SPI General Functions 3.5.1 SPI Reset Transaction FT4222_STATUS FT4222_SPI_ResetTransaction(FT_HANDLE ftHandle, uint8 spiIdx) Summary: Reset the SPI transaction. Parameters: ftHandle Handle of the device. spiIdx The index of the SPI transaction, which ranges from 0~3 depending on the mode of the chip. For example, under mode 0 and mode 3 as we mentioned in chapter 1.1, it should be 0 because there is only one SPI master or slave connection, and so forth. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.5.2 SPI Reset FT4222_STATUS FT4222_SPI_Reset (FT_HANDLE ftHandle) Summary: Reset the SPI master or slave device. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 29 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.5.3 SPI Set Driving Strength FT4222_STATUS FT4222_SPI_SetDrivingStrength(FT_HANDLE ftHandle, SPI_DrivingStrength clkStrength, SPI_DrivingStrength ioStrength, SPI_DrivingStrength ssoStrength) Summary: For the FT4222H SPI, set the driving strength of clk, io, and sso pins. Parameters: ftHandle Handle of the device. clkStrength The driving strength of the clk pin (SPI master only): ioStrength ssoStrength DS_4MA DS_8MA DS_12MA DS_16MA The driving strength of the io pin: DS_4MA DS_8MA DS_12MA DS_16MA The driving strength of the sso pin (SPI master only): DS_4MA DS_8MA DS_12MA DS_16MA Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 30 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.6 I2C Master Functions I2C (Inter Integrated Circuit) is a multi-master serial bus invented by Philips. I2C uses two bi-directional open-drain wires called serial data (SDA) and serial clock (SCL). Common I²C bus speeds are the 100 kbit/s standard mode (SM), 400 kbit/s fast mode (FM), 1 Mbit/s Fast mode plus (FM+), and 3.4 Mbit/s High Speed mode (HS) The FT4222H device can be initialized as either an I2C master or I2C slave under mode 0 and mode 3. Here is a brief overview of FT4222H I2C features: Fully compatible to I2C v2.1 and v3 specification 7-bit address support Support 4 speed configurations: 100KHz(SM), 400KHz(FM), 1MHz(FM+), and 3.4MHz(HS). Clock stretching support in both master and slave mode. Refer to DS_FT4222H for more details. 3.6.1 I2C Master Init FT4222_STATUS FT4222_I2CMaster_Init(FT_HANDLE ftHandle, uint32 kbps) Summary: Initialize the FT4222H as an I2C master with the requested I2C speed. Parameters: ftHandle Handle of the device. kbps The speed of I2C transmission. It ranges from 60K bps to 3400K bps. By specified speed, the initialization function helps to setup the bus speed with the corresponding mode. This parameter is used to configure the FT4222H to be either SM, FB, FM+ or HS mode. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.6.2 I2C Master Read FT4222_STATUS FT4222_I2CMaster_Read(FT_HANDLE ftHandle, uint16 slaveAddress, uint8* buffer, uint16 bytesToRead, uint16* sizeTransferred) Summary: Read data from the specified I2C slave device with START and STOP conditions. Product Page Document Feedback 31 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Parameters: ftHandle Handle of the device. slaveAddress Address of the target I2C slave. buffer Pointer to the buffer that receives data from the device. bytesToRead Number of bytes to read from the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes read from the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.6.3 I2C Master Write FT4222_STATUS FT4222_I2CMaster_Write(FT_HANDLE ftHandle, uint16 slaveAddress, uint8* buffer, uint16 bytesToWrite, uint16* sizeTransferred) Summary: Write data to the specified I2C slave device with START and STOP conditions. Parameters: ftHandle Handle of the device. slaveAddress Address of the target I2C slave. buffer Pointer to the buffer that contains the data to be written to the device. bytesToWrite Number of bytes to write to the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes written to the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.6.4 I2C Master Write Extension FT4222_STATUS FT4222_I2CMaster_WriteEx(FT_HANDLE ftHandle, uint16 deviceAddress, uint8 flag, uint8* buffer, uint16 bytesToWrite, uint16* sizeTransferred) Summary: Write data to the specified I2C slave device with the specified I2C condition. This function is supported by the rev B FT4222H or later. Product Page Document Feedback 32 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Parameters: ftHandle Handle of the device. slaveAddress Address of the target I2C slave. flag The I2C condition will be sent with this I2C transaction START = 0x02 Repeated_START = 0x03 Repeated_START will not send master code in HS mode STOP = 0x04 START_AND_STOP = 0x06 buffer Pointer to the buffer that contains the data to be written to the device. bytesToWrite Number of bytes to write to the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes written to the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.6.5 I2C Master Read Extension FT4222_STATUS FT4222_I2CMaster_ReadEx(FT_HANDLE ftHandle, uint16 deviceAddress, uint8 flag, uint8* buffer, uint16 bytesToRead, uint16* sizeTransferred) Summary: Read data from the specified I2C slave device with the specified I2C condition. This function is supported by the rev B FT4222H or later. Parameters: ftHandle Handle of the device. slaveAddress Address of the target I2C slave. flag The I2C condition will be sent with this I2C transaction START = 0x02 Repeated_START = 0x03 Repeated_START will not send master code in HS mode STOP = 0x04 START_AND_STOP = 0x06 buffer Pointer to the buffer that receives the data from the device. bytesToRead Number of bytes to read from the device. Product Page Document Feedback 33 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 sizeTransferred Clearance No.: FTDI#406 Pointer to a variable of type uint16 which receives the number of bytes read from the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. I²C combined message support In a combined message, each read or write begins with a START and the slave address. After the first START, the subsequent starts are referred to as repeated START bits; repeated START bits are not preceded by STOP bits, which indicate to the slave the next transfer is part of the same message. SR = repeated START condition Here is an example for typical usage of I2C combined message: // Write to I2C slave with START bit FT4222_I2CMaster_WriteEx(ftHandle, sizeTransferred); deviceAddress, START, buffer, bufferSize, // Read from I2C slave with Repeated START and STOP bit // Use Repeated_START flag instead of START to avoid the FT4222H sending master code // again in HS mode FT4222_I2CMaster_ReadEx(ftHandle, deviceAddress, Repeated_START | STOP, buffer, bufferSize, sizeTransferred); 3.6.6 I2C Master GetStatus FT4222_STATUS *controllerStatus) FT4222_I2CMaster_GetStatus(FT_HANDLE ftHandle, uint8 Summary: Read the status of the I2C master controller. This can be used to poll a slave until its write-cycle is complete. Parameters: ftHandle Handle of the device. controllerStatus Address of byte to receive status flags: bit 0: controller busy: all other status bits invalid Product Page Document Feedback 34 Copyright © 2015 Future Technology Devices International Limited ACK 8 bit data STOP 8 bit data ACK ACK 7 bit slave address read SR 8 bit data ACK ACK write Start 7 bit slave address Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 bit 1: error condition bit 2: slave address was not acknowledged during last operation bit 3: data not acknowledged during last operation bit 4: arbitration lost during last operation bit 5: controller idle bit 6: bus busy The header file provides convenience I2CM_BUS_BUSY) to test these bits. macros (such as Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.6.7 I2C Master Reset FT4222_STATUS FT4222_I2CMaster_Reset(FT_HANDLE ftHandle) Summary: 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. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 35 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.7 I2C Slave Functions The FT4222H device can be initialized as an I2C slave under mode 0 and mode 3. It conforms to v2.1 and v3.0 of the I2C specification and supports all the transmission modes: Standard, Fast, Fast-plus and High Speed. When the I2C slave receives data from the I2C bus, it will keep the data in its internal receive buffer (256 bytes), and then send the data to the USB host through IN packets. When data is requested by an I2C master, data will be moved from an OUT packet to the transmit register directly. 3.7.1 I2C Slave Init FT4222_STATUS FT4222_I2CSlave_Init(FT_HANDLE ftHandle) Summary: Initialize FT4222H as an I2C slave. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.7.2 I2C Slave Get Address FT4222_STATUS FT4222_I2CSlave_GetAddress(FT_HANDLE ftHandle, uint8* pAddr) Summary: Get the address of the I2C slave device. Parameters: ftHandle Handle of the device. pAddr Pointer to a variable of type uint16 which receives the address of the I2C slave device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 36 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.7.3 I2C Slave Set Address FT4222_STATUS FT4222_I2CSlave_SetAddress(FT_HANDLE ftHandle, uint8 addr) Summary: Set the address of the I2C slave device. Parameters: ftHandle Handle of the device. addr The 7-bit address of the I2C slave device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.7.4 I2C Slave Get Rx Status FT4222_STATUS pRxSize) FT4222_I2CSlave_GetRxStatus(FT_HANDLE ftHandle, uint16* Summary: Get number of bytes in the receive queue. Parameters: ftHandle Handle of the device. pRxSize Pointer to a variable of type uint16 which receives the number of bytes in the receive queue. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.7.5 I2C Slave Read FT4222_STATUS FT4222_I2CSlave_Read(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToRead, uint16* sizeTransferred) Summary: Read data from the buffer of the I2C slave device. Product Page Document Feedback 37 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Parameters: ftHandle Handle of the device. buffer Pointer to the buffer that receives the data from the device. bytesToRead Number of bytes to read from the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes read from the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.7.6 I2C Slave Write FT4222_STATUS FT4222_I2CSlave_Write(FT_HANDLE ftHandle, uint8* buffer, uint16 bytesToWrite, uint16* sizeTransferred) Summary: Write data to the buffer of I2C slave device. Parameters: ftHandle Handle of the device. buffer Pointer to the buffer that contains the data to be written to the device. bytesToWrite Number of bytes to write to the device. sizeTransferred Pointer to a variable of type uint16 which receives the number of bytes written to the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.7.7 I2C Slave Reset FT4222_STATUS FT4222_I2CSlave_Reset(FT_HANDLE ftHandle) Summary: Reset the I2C slave device. Parameters: ftHandle Handle of the device. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 38 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.8 GPIO Functions The FT4222H contains 4 GPIO. When the USB GPIO interface is supported, chip mode 0 and mode 1, LibFT4222 helps application developers to control GPIO directly. However, each GPIO 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 The number of GPIO pins available depends on the mode of the chip. For example, if the FT4222H is initialized as an I2C device, as shown above, the pins of gpio0 and gpio1 will be switched to scl and sda, and cannot be used as GPIO. If suspend out and remote wakeup are enabled gpio2 and gpio3 cannot be used as GPIO. The FT4222H supports GPIO on the second USB interface in mode 0 or on the fourth interface in mode 2 (Please refer table 2.1 for chip mode and interface). 3.8.1 GPIO Init FT4222_STATUS FT4222_GPIO_Init(FT_HANDLE ftHandle, GPIO_Dir gpioDir[4]) Summary: Initialize the GPIO interface of the FT4222H. Please note the GPIO interface is available on the 2nd USB interface in mode 0 or on the 4th USB interface in mode 1. Parameters: ftHandle Handle of the device. gpioDir An array defines the directions of 4 GPIO pins. The GPIO direction will be: GPIO_OUTPUT GPIO_INPUT Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 39 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.8.2 GPIO Read FT4222_STATUS BOOL* pValue) FT4222_GPIO_Read(FT_HANDLE ftHandle, GPIO_Port portNum, Summary: Read the value from the specified GPIO pin. Parameters: ftHandle Handle of the device. portNum One of the following GPIO ports: pValue GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 Pointer to a variable of type BOOL which receives the value of the GPIO pin. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.8.3 GPIO Write FT4222_STATUS BOOL bValue) FT4222_GPIO_Write(FT_HANDLE ftHandle, GPIO_Port portNum, Summary: Write value to the specified GPIO pin. Parameters: ftHandle Handle of the device. portNum One of the following GPIO port: bValue Product Page Document Feedback GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 The output value. 40 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. 3.8.4 GPIO Set Input Trigger FT4222_STATUS FT4222_GPIO_SetInputTrigger(FT_HANDLE ftHandle, GPIO_Port portNum, GPIO_Trigger trigger) Summary: Set software trigger conditions on the specified GPIO pin. This function allows developers to monitor value changes of the GPIO pins. Values that satisfy the trigger condition will be stored in a queue. For example, if GPIO_TRIGGER_RISING is set on GPIO0, and GPIO0 then changes value from 0 to 1, the event GPIO_TRIGGER_RISING will be recorded into the queue. Developers can query the queue status by FT4222_GPIO_GetTriggerStatus, and FT4222_GPIO_ReadTriggerQueue. Parameters: ftHandle Handle of the device. portNum One of the following GPIO port: trigger GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 Combination of the following trigger conditions: 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. Product Page Document Feedback 41 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 3.8.5 GPIO Get Trigger Status FT4222_STATUS FT4222_GPIO_GetTriggerStatus(FT_HANDLE ftHandle, GPIO_Port portNum, uint16* pQueueSize) Summary: Get the size of trigger event queue. Parameters: ftHandle Handle of the device. portNum One of the following GPIO port: pQueueSize GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 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. 3.8.6 GPIO Read Trigger Queue FT4222_STATUS FT4222_GPIO_ReadTriggerQueue(FT_HANDLE ftHandle, GPIO_Port portNum, GPIO_Trigger* events, uint16 readSize, uint16* sizeofRead) Summary: Get events recorded in the trigger event queue. Trigger conditions are set by a call to FT4222_GPIO_SetInputTrigger. After calling this function, all events will be removed from the event queue. Parameters: ftHandle Handle of the device. portNum One of the following GPIO port: events Product Page Document Feedback GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 Pointer to the buffer that receives the values of the trigger event queue. 42 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 The value of events will be: GPIO_TRIGGER_RISING GPIO_TRIGGER_FALLING GPIO_TRIGGER_LEVEL_HIGH GPIO_TRIGGER_LEVEL_LOW readSize Number of bytes to read from trigger event queue. sizeofRead Pointer to a variable of type uint16 which receives the number of bytes read from the queue. Return Value: FT4222_OK if successful, otherwise the return value is an FT error code. Product Page Document Feedback 43 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 4 Contact Information Head Office – Glasgow, UK Branch Office – Tigard, Oregon, USA Future Technology Devices International Limited Unit 1, 2 Seaward Place, Centurion Business Park Glasgow G41 1HH United Kingdom Tel: +44 (0) 141 429 2777 Fax: +44 (0) 141 429 2758 Future Technology Devices International Limited (USA) 7130 SW Fir Loop Tigard, OR 97223-8160 USA Tel: +1 (503) 547 0988 Fax: +1 (503) 547 0987 E-mail (Sales) E-mail (Support) E-mail (General Enquiries) [email protected] [email protected] [email protected] E-Mail (Sales) E-Mail (Support) E-Mail (General Enquiries) Branch Office – Taipei, Taiwan Branch Office – Shanghai, China Future Technology Devices International Limited (Taiwan) 2F, No. 516, Sec. 1, NeiHu Road Taipei 114 Taiwan , R.O.C. Tel: +886 (0) 2 8791 3570 Fax: +886 (0) 2 8791 3576 E-mail (Sales) E-mail (Support) E-mail (General Enquiries) [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] Future Technology Devices International Limited (China) Room 1103, 666 West Huaihai Road, Shanghai, 200052 China Tel: +86 21 62351596 Fax: +86 21 62351595 E-mail (Sales) E-mail (Support) E-mail (General Enquiries) [email protected] [email protected] [email protected] Web Site http://ftdichip.com Distributor and Sales Representatives Please visit the Sales Network page of the FTDI Web site for the contact details of our distributor(s) and sales representative(s) in your country. System and equipment manufacturers and designers are responsible to ensure that their systems, and any Future Technology Devices International Ltd (FTDI) devices incorporated in their systems, meet all applicable safety, regulatory and system-level performance requirements. All application-related information in this document (including application descriptions, suggested FTDI devices and other materials) is provided for reference only. While FTDI has taken care to assure it is accurate, this information is subject to customer confirmation, and FTDI disclaims all liability for system designs and for any applications assistance provided by FTDI. 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 harmless FTDI from any and all damages, claims, suits or expense resulting from such use. This document is subject to change without notice. No freedom to use patents or other intellectual property rights is implied by the publication of this document. Neither the whole nor any part of the information contained in, or the product described in this document, may be adapted or reproduced in any material or electronic form without the prior written consent of the copyright holder. Future Technology Devices International Ltd, Unit 1, 2 Seaward Place, Centurion Business Park, Glasgow G41 1HH, United Kingdom. Scotland Registered Company Number: SC136640 Product Page Document Feedback 44 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Appendix A – Enumeration and Structure Definitions Enumeration FT4222_STATUS FT4222_DEVICE_NOT_SUPPORTED = 1000 FT4222_CLK_NOT_SUPPORTED // spi master do not support 80MHz/CLK_2 FT4222_VENDER_CMD_NOT_SUPPORTED FT4222_IS_NOT_SPI_MODE FT4222_IS_NOT_I2C_MODE FT4222_IS_NOT_SPI_SINGLE_MODE FT4222_IS_NOT_SPI_MULTI_MODE FT4222_WRONG_I2C_ADDR FT4222_INVAILD_FUNCTION FT4222_INVALID_POINTER FT4222_EXCEEDED_MAX_TRANSFER_SIZE FT4222_FAILED_TO_READ_DEVICE FT4222_I2C_NOT_SUPPORTED_IN_THIS_MODE FT4222_GPIO_NOT_SUPPORTED_IN_THIS_MODE FT4222_GPIO_EXCEEDED_MAX_PORTNUM FT4222_GPIO_WRITE_NOT_SUPPORTED FT4222_GPIO_PULLUP_INVALID_IN_INPUTMODE FT4222_GPIO_PULLDOWN_INVALID_IN_INPUTMODE FT4222_GPIO_OPENDRAIN_INVALID_IN_OUTPUTMODE FT4222_INTERRUPT_NOT_SUPPORTED FT4222_GPIO_INPUT_NOT_SUPPORTED FT4222_EVENT_NOT_SUPPORTED FT4222_FUN_NOT_SUPPORT FT4222_ClockRate SYS_CLK_60 = 0 SYS_CLK_24 SYS_CLK_48 SYS_CLK_80 FT4222_SPIMode Product Page Document Feedback 45 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 SPI_IO_NONE Clearance No.: FTDI#406 = 0 SPI_IO_SINGLE = 1 SPI_IO_DUAL = 2 SPI_IO_QUAD = 4 FT4222_SPIClock CLK_NONE = 0 CLK_DIV_2 // 1/2 System Clock CLK_DIV_4 // 1/4 System Clock CLK_DIV_8 // 1/8 System Clock CLK_DIV_16 // 1/16 System Clock CLK_DIV_32 // 1/32 System Clock CLK_DIV_64 // 1/64 System Clock CLK_DIV_128 // 1/128 System Clock CLK_DIV_256 // 1/256 System Clock CLK_DIV_512 // 1/512 System Clock FT4222_SPICPOL CLK_IDLE_LOW =0 CLK_IDLE_HIGH =1 FT4222_SPICPHA CLK_LEADING =0 CLK_TRAILING =1 SPI_DrivingStrength DS_4MA DS_8MA DS_12MA DS_16MA enum GPIO_Port GPIO_PORT0 GPIO_PORT1 GPIO_PORT2 GPIO_PORT3 enum GPIO_Dir Product Page Document Feedback 46 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 GPIO_OUTPUT GPIO_INPUT enum GPIO_Trigger GPIO_TRIGGER_RISING GPIO_TRIGGER_FALLING GPIO_TRIGGER_LEVEL_HIGH GPIO_TRIGGER_LEVEL_LOW enum GPIO_Output GPIO_OUTPUT_LOW GPIO_OUTPUT_HIGH enum I2C_MasterFlag START = 0x02 Repeated_START = 0x03 // Repeated_START will not send master code in HS mode STOP = 0x04 START_AND_STOP = 0x06 // START condition followed by SEND and STOP condition Structure Definitions struct FT4222_Version { DWORD chipVersion; // The version of FT4222H chip DWORD dllVersion; // The version of LibFT4222 }; struct SPI_Slave_Header { uint8 syncWord; uint8 cmd; uint8 sn; uint16 size; }; Product Page Document Feedback 47 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Appendix B – References Document References DS_FT4222H D2XX Programmers Guide D2XX Drivers Acronyms and Abbreviations Terms Description D2XX FTDI’s proprietary “direct” driver interface via FTD2XX.DLL GPIO General-purpose input/output I2C Inter-Integrated Circuit SPI Serial Peripheral Interconnect USB Universal Serial Bus USB-IF Product Page Document Feedback USB Implementers Forum 48 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Appendix C – List of Tables & Figures List of Tables Table 2.1 Chip mode and device functions ................................................................ 8 List of Figures Figure 1.1 The software stack ................................................................................. 4 Figure 1.2 Mode 0: FT4222H works as SPI master (quad mode) .................................. 6 Figure 1.3 Mode 0: FT4222H works as I2C master ..................................................... 6 Figure 1.4 Mode 2: FT4222H works as SPI master ..................................................... 7 Figure 3.1 SPI full duplex communication ............................................................... 21 Figure 3.2 Dual SPI communication ....................................................................... 22 Figure 3.3 Quad SPI communication ...................................................................... 22 Figure 3.4 SPI Slave Protocol Format ..................................................................... 24 Figure 3.5 SPI Master transfer request ................................................................... 25 Figure 3.6 An example of the SPI slave responding with ACK .................................... 25 Figure 3.7 An example of when the SPI master doesn’t receive ACK .......................... 26 Figure 3.8 Slave sends transfer request ................................................................. 26 Product Page Document Feedback 49 Copyright © 2015 Future Technology Devices International Limited Application Note AN_329 User Guide For LibFT4222 Version 1.1 Document Reference No.: FT_001060 Clearance No.: FTDI#406 Appendix D – Revision History Document Title: AN_329 User Guide For LibFT4222 Document Reference No.: FT_000546 Clearance No.: FTDI#406 Product Page: http://www.ftdichip.com/FTProducts.htm Document Feedback: Send Feedback Revision 1.0 1.1 Product Page Document Feedback Changes Date Initial Release 2014-09-16 Two new I2C functions are added to support combined message format Update to FT4222_I2CMaster_WriteEx and FT4222_I2CMaster_ReadEx 2015-09-10 50 Copyright © 2015 Future Technology Devices International Limited