Private
Public Access
2
0

ft4222: adds initial support for on-chip GPIO in mode0

This commits uses CLK_* and so requires libftdi 1.2.1.4+

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Mihai Tudor Panu
2015-12-01 11:56:08 +00:00
committed by Brendan Le Foll
parent 5f4f0a2aef
commit 063a3154aa
2 changed files with 110 additions and 49 deletions

View File

@@ -4,7 +4,8 @@ FTDI FT4222H {#ft4222}
The FT4222H is a High/Full Speed USB2.0-to-Quad SPI/I2C device controller. Mraa
supports it as a USB subplatform using the libft4222 library from FTDI which
can be found
[here](http://www.ftdichip.com/Support/SoftwareExamples/libft4222-1.2.0.240.tgz).
[here](http://www.ftdichip.com/Support/SoftwareExamples/libft4222-1.2.1.4.tgz).
You need the latest version for the GPIO to work
The FT 4222H has 4 configuration modes selected by {DCNF1, DCNF0}. The c hip
configuration mode will determine the number of USB interface s for data stream

View File

@@ -37,19 +37,18 @@
#define PCA9672_ADDR 0x20
static FT_HANDLE ftHandleGPIO = (FT_HANDLE) NULL;
static FT_HANDLE ftHandle = (FT_HANDLE) NULL; //I2C Handle
static FT_HANDLE ftHandleGPIO = (FT_HANDLE) NULL; //GPIO Handle
static FT_HANDLE ftHandle = (FT_HANDLE) NULL; //I2C/SPI Handle
static GPIO_Dir pinDirection[] = {GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT};
static int bus_speed = 400;
static int numI2cGpioExapnderPins = 0;
static int numUsbGpio = 0;
static int numI2cGpioExpanderPins;
mraa_result_t
mraa_ftdi_ft4222_init()
{
mraa_result_t mraaStatus = MRAA_SUCCESS;
FT_STATUS ftStatus;
FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
FT_STATUS ftStatus;
DWORD numDevs = 0;
int i;
int retCode = 0;
@@ -98,6 +97,7 @@ mraa_ftdi_ft4222_init()
goto init_exit;
}
// CNFMODE 0, GPIO interface available.
if(numDevs = 2) {
ftStatus = FT_OpenEx((PVOID)(uintptr_t) devInfo[1].LocId, FT_OPEN_BY_LOCATION, &ftHandleGPIO);
if (ftStatus != FT_OK) {
@@ -106,17 +106,19 @@ mraa_ftdi_ft4222_init()
goto init_exit;
}
FT4222_SetSuspendOut(ftHandleGPIO, false);
FT4222_SetWakeUpInterrupt(ftHandleGPIO, false);
// Disable Suspend and Wake on GPIO2 & GPIO3
FT4222_SetSuspendOut(ftHandleGPIO, 0);
FT4222_SetWakeUpInterrupt(ftHandleGPIO, 0);
ftStatus = FT4222_GPIO_Init(ftHandleGPIO, pinDirection);
if (tStatus != FT_OK) {
if (ftStatus != FT_OK) {
syslog(LOG_ERR, "FT4222_GPIO_Init failed (error %d)\n", (int) ftStatus);
mraaStatus = MRAA_ERROR_NO_RESOURCES;
goto init_exit;
}
}
// I2C or SPI interface.
ftStatus = FT_OpenEx((PVOID)(uintptr_t) devInfo[0].LocId, FT_OPEN_BY_LOCATION, &ftHandle);
if (ftStatus != FT_OK) {
syslog(LOG_ERR, "FT_OpenEx I2C handle failed (error %d)\n", (int) ftStatus);
@@ -124,7 +126,7 @@ mraa_ftdi_ft4222_init()
goto init_exit;
}
// Tell the FT4222 to be an I2C Master.
// Tell the FT4222 to be an I2C Master by default on init.
FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
if (FT4222_OK != ft4222Status) {
syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
@@ -140,7 +142,6 @@ mraa_ftdi_ft4222_init()
goto init_exit;
}
init_exit:
if (devInfo != NULL)
;
@@ -208,14 +209,13 @@ mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_
return bytesWritten;
}
static mraa_boolean_t
// Function detects known I2C expanders and returns the number of GPIO pins on expander
static int
mraa_ftdi_ft4222_detect_io_expander()
{
uint8_t data;
if(mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &data, 1) == 1) {
numI2cGpioExapnderPins = 8;
return 1;
return 8;
}
return 0;
}
@@ -270,6 +270,8 @@ mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
return MRAA_ERROR_NO_RESOURCES;
}
syslog(LOG_NOTICE, "I2C interface enabled GPIO0 and GPIO1 will be unavailable.\n");
dev->handle = ftHandle;
dev->fh = -1; // We don't use file descriptors
dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
@@ -400,6 +402,16 @@ mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
static mraa_result_t
mraa_ftdi_ft4222_gpio_init_internal_replace(int pin)
{
if ((pin - numI2cGpioExpanderPins) == 0 || (pin - numI2cGpioExpanderPins) == 1) {
syslog(LOG_NOTICE, "Closing I2C interface to enable GPIO%d\n", pin);
/* Replace with call to SPI init when SPI is fully implemented */
FT4222_STATUS ft4222Status = FT4222_SPIMaster_Init(ftHandle, SPI_IO_SINGLE, CLK_DIV_4, CLK_IDLE_HIGH, CLK_LEADING, 0x01);
if (FT4222_OK != ft4222Status){
syslog(LOG_ERR, "Failed to close I2C interface and start SPI (error %d)!\n", ft4222Status);
return MRAA_ERROR_NO_RESOURCES;
}
}
return MRAA_SUCCESS;
}
@@ -421,10 +433,23 @@ mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
uint8_t pin = dev->phy_pin;
uint8_t mask = 1 << pin;
uint8_t value;
if(pin >= numI2cGpioExpanderPins) {
// FTDI GPIO
FT4222_STATUS ft4222Status = FT4222_GPIO_Read(ftHandleGPIO, (pin - numI2cGpioExpanderPins), (BOOL*)&value);
if (FT4222_OK != ft4222Status) {
syslog(LOG_ERR, "FT4222_GPIO_Read failed (error %d)!\n", ft4222Status);
return -1;
}
return value;
}
else {
// Expander GPIO
if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
return -1;
return (value & mask) == mask;
}
}
static mraa_result_t
@@ -433,6 +458,17 @@ mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
uint8_t pin = dev->phy_pin;
uint8_t mask = 1 << pin;
uint8_t value;
if(pin >= numI2cGpioExpanderPins) {
// FTDI GPIO
FT4222_STATUS ft4222Status = FT4222_GPIO_Write(ftHandleGPIO, (pin - numI2cGpioExpanderPins), write_value);
if (FT4222_OK != ft4222Status) {
syslog(LOG_ERR, "FT4222_GPIO_Write failed (error %d)!\n", ft4222Status);
return MRAA_ERROR_UNSPECIFIED;
}
}
else {
// Expander GPIO
if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
return MRAA_ERROR_UNSPECIFIED;
if (write_value == 1)
@@ -441,6 +477,8 @@ mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
value &= (~mask);
if (mraa_ftdi_ft4222_i2c_write_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
return MRAA_ERROR_UNSPECIFIED;
}
return MRAA_SUCCESS;
}
@@ -449,29 +487,36 @@ mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
{
switch (dir) {
case MRAA_GPIO_IN:
if(dev->phy_pin >= numI2cGpioExapnderPins) {
pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_INPUT;
return MRAA_SUCCESS;
if (dev->phy_pin >= numI2cGpioExpanderPins) {
pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_INPUT;
if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
return MRAA_ERROR_UNSPECIFIED;
}
return MRAA_SUCCESS;
case MRAA_GPIO_OUT:
if(dev->phy_pin >= numI2cGpioExapnderPins) {
pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
return MRAA_SUCCESS;
if (dev->phy_pin >= numI2cGpioExpanderPins) {
pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
return MRAA_ERROR_UNSPECIFIED;
}
return MRAA_SUCCESS;
case MRAA_GPIO_OUT_HIGH:
if(dev->phy_pin >= numI2cGpioExapnderPins) {
pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
if (dev->phy_pin >= numI2cGpioExpanderPins) {
pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
return MRAA_ERROR_UNSPECIFIED;
}
return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
case MRAA_GPIO_OUT_LOW:
if(dev->phy_pin >= numI2cGpioExapnderPins) {
pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
if (dev->phy_pin >= numI2cGpioExpanderPins) {
pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
return MRAA_ERROR_UNSPECIFIED;
}
return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
default:
;
return MRAA_ERROR_INVALID_PARAMETER;
}
return MRAA_SUCCESS;
}
static void
@@ -494,6 +539,7 @@ mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
while (1) {
int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
// MRAA_GPIO_EDGE_BOTH
if (level != prev_level) {
dev->isr(dev->isr_args);
prev_level = level;
@@ -541,13 +587,12 @@ mraa_ftdi_ft4222()
mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
if (sub_plat == NULL)
return NULL;
mraa_boolean_t haveGpio = mraa_ftdi_ft4222_detect_io_expander();
numI2cGpioExpanderPins = mraa_ftdi_ft4222_detect_io_expander();
int pinIndex = 0;
numUsbGpio = haveGpio ? numI2cGpioExapnderPins : 0;
int numUsbPins = numUsbGpio + 2; // Add SDA and SCL
int numUsbPins = numI2cGpioExpanderPins + 4; // Add GPIO0/SDA, GPIO1/SCL, GPIO2, GPIO3
sub_plat->platform_name = PLATFORM_NAME;
sub_plat->phy_pin_count = numUsbPins;
sub_plat->gpio_count = numUsbGpio;
sub_plat->gpio_count = numUsbPins;
mraa_pininfo_t* pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * numUsbPins);
if (pins == NULL) {
return NULL;
@@ -556,7 +601,7 @@ mraa_ftdi_ft4222()
// Virtual gpio pins on i2c I/O expander.
mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
for (pinIndex = 0; pinIndex < numUsbGpio; ++pinIndex) {
for (pinIndex = 0; pinIndex < numI2cGpioExpanderPins; ++pinIndex) {
char name[8];
sprintf(name, "Pin%d", pinIndex);
strncpy(sub_plat->pins[pinIndex].name, name, 8);
@@ -569,17 +614,33 @@ mraa_ftdi_ft4222()
sub_plat->def_i2c_bus = bus;
sub_plat->i2c_bus[bus].bus_id = bus;
// i2c pins (these are virtual, entries are required to configure i2c layer)
mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
strncpy(sub_plat->pins[pinIndex].name, "SCL", 8);
// I2c pins (these are virtual, entries are required to configure i2c layer)
mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 1, 0, 0 };
strncpy(sub_plat->pins[pinIndex].name, "SCL/GPIO0", 10);
sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
sub_plat->pins[pinIndex].gpio.mux_total = 0;
sub_plat->pins[pinIndex].i2c.mux_total = 0;
sub_plat->i2c_bus[bus].scl = pinIndex;
pinIndex++;
strncpy(sub_plat->pins[pinIndex].name, "SDA", 8);
strncpy(sub_plat->pins[pinIndex].name, "SDA/GPIO1", 10);
sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
sub_plat->pins[pinIndex].gpio.mux_total = 0;
sub_plat->pins[pinIndex].i2c.mux_total = 0;
sub_plat->i2c_bus[bus].sda = pinIndex;
pinIndex++;
// FTDI4222 gpio
strncpy(sub_plat->pins[pinIndex].name, "GPIO2", 8);
sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
sub_plat->pins[pinIndex].gpio.mux_total = 0;
pinIndex++;
strncpy(sub_plat->pins[pinIndex].name, "GPIO3", 8);
sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
sub_plat->pins[pinIndex].gpio.mux_total = 0;
// Set override functions
mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
@@ -587,7 +648,6 @@ mraa_ftdi_ft4222()
return NULL;
}
mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
if (haveGpio)
mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
sub_plat->adv_func = func_table;