2 * Author: Henry Bruce <henry.bruce@intel.com>
3 * Copyright (c) 2015 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "linux/i2c-dev.h"
32 #include "libft4222.h"
33 #include "usb/ftdi_ft4222.h"
35 #define PLATFORM_NAME "FTDI FT4222"
36 #define I2CM_ERROR(status) (((status) &0x02) != 0)
37 #define PCA9672_ADDR 0x20
40 static FT_HANDLE ftHandleGPIO = (FT_HANDLE) NULL;
41 static FT_HANDLE ftHandle = (FT_HANDLE) NULL; //I2C Handle
42 static GPIO_Dir pinDirection[] = {GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT};
43 static int bus_speed = 400;
44 static int numI2cGpioExapnderPins = 0;
45 static int numUsbGpio = 0;
48 mraa_ftdi_ft4222_init()
50 mraa_result_t mraaStatus = MRAA_SUCCESS;
52 FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
57 ftStatus = FT_CreateDeviceInfoList(&numDevs);
58 if (ftStatus != FT_OK) {
59 syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus);
60 mraaStatus = MRAA_ERROR_NO_RESOURCES;
65 syslog(LOG_ERR, "No FT4222 devices connected.\n");
69 devInfo = calloc((size_t) numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE));
70 if (devInfo == NULL) {
71 syslog(LOG_ERR, "FT4222 allocation failure.\n");
72 mraaStatus = MRAA_ERROR_NO_RESOURCES;
76 ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
77 if (ftStatus != FT_OK) {
78 syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int) ftStatus);
79 mraaStatus = MRAA_ERROR_NO_RESOURCES;
84 FT4222_Version ft4222Version;
85 FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
86 if (FT4222_OK == ft4222Status){
87 syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", ft4222Version.chipVersion,
88 ft4222Version.dllVersion);
90 syslog(LOG_ERR, "FT4222_GetVersion failed with code %d", ft4222Status);
93 syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs);
96 syslog(LOG_ERR, "CNFMODE not supported. Valid modes are 0 or 3.\n");
97 mraaStatus = MRAA_ERROR_NO_RESOURCES;
102 ftStatus = FT_OpenEx((PVOID)(uintptr_t) devInfo[1].LocId, FT_OPEN_BY_LOCATION, &ftHandleGPIO);
103 if (ftStatus != FT_OK) {
104 syslog(LOG_ERR, "FT_OpenEx GPIO handle failed (error %d)\n", (int) ftStatus);
105 mraaStatus = MRAA_ERROR_NO_RESOURCES;
109 FT4222_SetSuspendOut(ftHandleGPIO, false);
110 FT4222_SetWakeUpInterrupt(ftHandleGPIO, false);
112 ftStatus = FT4222_GPIO_Init(ftHandleGPIO, pinDirection);
113 if (tStatus != FT_OK) {
114 syslog(LOG_ERR, "FT4222_GPIO_Init failed (error %d)\n", (int) ftStatus);
115 mraaStatus = MRAA_ERROR_NO_RESOURCES;
120 ftStatus = FT_OpenEx((PVOID)(uintptr_t) devInfo[0].LocId, FT_OPEN_BY_LOCATION, &ftHandle);
121 if (ftStatus != FT_OK) {
122 syslog(LOG_ERR, "FT_OpenEx I2C handle failed (error %d)\n", (int) ftStatus);
123 mraaStatus = MRAA_ERROR_NO_RESOURCES;
127 // Tell the FT4222 to be an I2C Master.
128 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
129 if (FT4222_OK != ft4222Status) {
130 syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
131 mraaStatus = MRAA_ERROR_NO_RESOURCES;
135 // Reset the I2CM registers to a known state.
136 ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
137 if (FT4222_OK != ft4222Status) {
138 syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
139 mraaStatus = MRAA_ERROR_NO_RESOURCES;
148 if (mraaStatus == MRAA_SUCCESS)
149 syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n");
155 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
157 if (ftHandle != NULL) {
158 FT4222_Version ft4222Version;
159 FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
160 if (FT4222_OK == ft4222Status) {
161 *versionChip = (unsigned int) ft4222Version.chipVersion;
162 *versionLib = (unsigned int) ft4222Version.dllVersion;
163 syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", *versionChip, *versionLib);
166 syslog(LOG_ERR, "libmraa: FT4222_GetVersion failed (error %d)\n", (int) ft4222Status);
167 return MRAA_ERROR_NO_RESOURCES;
170 syslog(LOG_ERR, "libmraa: bad FT4222 handle\n");
171 return MRAA_ERROR_INVALID_HANDLE;
177 mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle, uint8_t addr, uint8_t* data, int length)
179 uint16 bytesRead = 0;
180 uint8 controllerStatus;
181 // syslog(LOG_NOTICE, "FT4222_I2CMaster_Read(%#02X, %#02X)", addr, length);
182 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Read(handle, addr, data, length, &bytesRead);
183 ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
184 if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
185 syslog(LOG_ERR, "FT4222_I2CMaster_Read failed (error %d)\n", (int) ft4222Status);
192 mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_t* data, int bytesToWrite)
194 uint16 bytesWritten = 0;
195 uint8 controllerStatus;
196 // syslog(LOG_NOTICE, "FT4222_I2CMaster_Write(%#02X, %#02X, %d)", addr, *data, bytesToWrite);
197 FT4222_STATUS ft4222Status =
198 FT4222_I2CMaster_Write(handle, addr, (uint8_t*) data, bytesToWrite, &bytesWritten);
199 ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
200 if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
201 syslog(LOG_ERR, "FT4222_I2CMaster_Write failed (error %d)\n", (int) ft4222Status);
205 if (bytesWritten != bytesToWrite)
206 syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
212 static mraa_boolean_t
213 mraa_ftdi_ft4222_detect_io_expander()
216 if(mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &data, 1) == 1) {
217 numI2cGpioExapnderPins = 8;
224 /******************* I2C functions *******************/
225 // Function not currently mapped or used since we have virtual pin definitions
226 static mraa_i2c_context
227 mraa_ftdi_ft4222_i2c_init_raw_replace(unsigned int bus)
229 // Tell the FT4222 to be an I2C Master.
230 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
231 if (FT4222_OK != ft4222Status) {
232 syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
236 // Reset the I2CM registers to a known state.
237 ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
238 if (FT4222_OK != ft4222Status) {
239 syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
243 mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
245 syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
249 dev->handle = ftHandle;
250 dev->fh = -1; // We don't use file descriptors
251 dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
252 // https://www.kernel.org/doc/Documentation/i2c/functionality
257 mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
259 // Tell the FT4222 to be an I2C Master.
260 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, 400);
261 if (FT4222_OK != ft4222Status) {
262 syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
263 return MRAA_ERROR_NO_RESOURCES;
266 // Reset the I2CM registers to a known state.
267 ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
268 if (FT4222_OK != ft4222Status) {
269 syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
270 return MRAA_ERROR_NO_RESOURCES;
273 dev->handle = ftHandle;
274 dev->fh = -1; // We don't use file descriptors
275 dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
276 // https://www.kernel.org/doc/Documentation/i2c/functionality
282 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
285 case MRAA_I2C_STD: /**< up to 100Khz */
288 MRAA_I2C_FAST: /**< up to 400Khz */
291 MRAA_I2C_HIGH: /**< up to 3.4Mhz */
300 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
302 dev->addr = (int) addr;
303 return FT4222_I2CMaster_Init(ftHandle, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
308 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
310 return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
314 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
317 if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, &data, 1) == 1)
325 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
329 if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
331 if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, buf, 2) != 2)
333 data = *(uint16_t*)buf;
338 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
340 if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
342 return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
347 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
349 uint16 bytesWritten = mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, data, bytesToWrite);
350 return bytesToWrite == bytesWritten ? MRAA_SUCCESS : MRAA_ERROR_INVALID_HANDLE;
355 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
357 return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
362 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
364 const uint8_t reg_addr = command;
366 if (mraa_ftdi_ft4222_i2c_write(dev, ®_addr, 1) != MRAA_SUCCESS)
368 if (mraa_ftdi_ft4222_i2c_read(dev, &data, 1) != 1)
374 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
379 return mraa_ftdi_ft4222_i2c_write(dev, buf, 2);
383 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
387 buf[1] = (uint8_t) data;
388 buf[2] = (uint8_t)(data >> 8);
389 return mraa_ftdi_ft4222_i2c_write(dev, buf, 3);
393 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
398 /******************* GPIO functions *******************/
401 mraa_ftdi_ft4222_gpio_init_internal_replace(int pin)
407 mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
413 mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev, mraa_gpio_edge_t mode)
419 mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
421 uint8_t pin = dev->phy_pin;
422 uint8_t mask = 1 << pin;
424 if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
426 return (value & mask) == mask;
431 mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
433 uint8_t pin = dev->phy_pin;
434 uint8_t mask = 1 << pin;
436 if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
437 return MRAA_ERROR_UNSPECIFIED;
438 if (write_value == 1)
442 if (mraa_ftdi_ft4222_i2c_write_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
443 return MRAA_ERROR_UNSPECIFIED;
448 mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
452 if(dev->phy_pin >= numI2cGpioExapnderPins) {
453 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_INPUT;
457 if(dev->phy_pin >= numI2cGpioExapnderPins) {
458 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
461 case MRAA_GPIO_OUT_HIGH:
462 if(dev->phy_pin >= numI2cGpioExapnderPins) {
463 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
465 return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
466 case MRAA_GPIO_OUT_LOW:
467 if(dev->phy_pin >= numI2cGpioExapnderPins) {
468 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
470 return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
478 mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)
480 struct timespec sleepTime;
482 sleepTime.tv_sec = mseconds / 1000; // Number of seconds
483 sleepTime.tv_nsec = (mseconds % 1000) * 1000000; // Convert fractional seconds to nanoseconds
485 // Iterate nanosleep in a loop until the total sleep time is the original
486 // value of the seconds parameter
487 while ((nanosleep(&sleepTime, &sleepTime) != 0) && (errno == EINTR))
492 mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
494 int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
496 int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
497 if (level != prev_level) {
498 dev->isr(dev->isr_args);
501 // printf("mraa_ftdi_ft4222_gpio_interrupt_handler_replace\n");
502 mraa_ftdi_ft4222_sleep_ms(100);
508 mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t* func_table)
510 func_table->i2c_init_bus_replace = &mraa_ftdi_ft4222_i2c_init_bus_replace;
511 func_table->i2c_set_frequency_replace = &mraa_ftdi_ft4222_i2c_frequency;
512 func_table->i2c_address_replace = &mraa_ftdi_ft4222_i2c_address;
513 func_table->i2c_read_replace = &mraa_ftdi_ft4222_i2c_read;
514 func_table->i2c_read_byte_replace = &mraa_ftdi_ft4222_i2c_read_byte;
515 func_table->i2c_read_byte_data_replace = &mraa_ftdi_ft4222_i2c_read_byte_data;
516 func_table->i2c_read_word_data_replace = &mraa_ftdi_ft4222_i2c_read_word_data;
517 func_table->i2c_read_bytes_data_replace = &mraa_ftdi_ft4222_i2c_read_bytes_data;
518 func_table->i2c_write_replace = &mraa_ftdi_ft4222_i2c_write;
519 func_table->i2c_write_byte_replace = &mraa_ftdi_ft4222_i2c_write_byte;
520 func_table->i2c_write_byte_data_replace = &mraa_ftdi_ft4222_i2c_write_byte_data;
521 func_table->i2c_write_word_data_replace = &mraa_ftdi_ft4222_i2c_write_word_data;
522 func_table->i2c_stop_replace = &mraa_ftdi_ft4222_i2c_stop;
526 mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t* func_table)
528 func_table->gpio_init_internal_replace = &mraa_ftdi_ft4222_gpio_init_internal_replace;
529 func_table->gpio_mode_replace = &mraa_ftdi_ft4222_gpio_mode_replace;
530 func_table->gpio_edge_mode_replace = &mraa_ftdi_ft4222_gpio_edge_mode_replace;
531 func_table->gpio_dir_replace = &mraa_ftdi_ft4222_gpio_dir_replace;
532 func_table->gpio_read_replace = &mraa_ftdi_ft4222_gpio_read_replace;
533 func_table->gpio_write_replace = &mraa_ftdi_ft4222_gpio_write_replace;
534 func_table->gpio_interrupt_handler_replace = &mraa_ftdi_ft4222_gpio_interrupt_handler_replace;
541 mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
542 if (sub_plat == NULL)
544 mraa_boolean_t haveGpio = mraa_ftdi_ft4222_detect_io_expander();
546 numUsbGpio = haveGpio ? numI2cGpioExapnderPins : 0;
547 int numUsbPins = numUsbGpio + 2; // Add SDA and SCL
548 sub_plat->platform_name = PLATFORM_NAME;
549 sub_plat->phy_pin_count = numUsbPins;
550 sub_plat->gpio_count = numUsbGpio;
551 mraa_pininfo_t* pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * numUsbPins);
555 sub_plat->pins = pins;
557 // Virtual gpio pins on i2c I/O expander.
558 mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
559 for (pinIndex = 0; pinIndex < numUsbGpio; ++pinIndex) {
561 sprintf(name, "Pin%d", pinIndex);
562 strncpy(sub_plat->pins[pinIndex].name, name, 8);
563 sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
564 sub_plat->pins[pinIndex].gpio.mux_total = 0;
568 sub_plat->i2c_bus_count = 1;
569 sub_plat->def_i2c_bus = bus;
570 sub_plat->i2c_bus[bus].bus_id = bus;
572 // i2c pins (these are virtual, entries are required to configure i2c layer)
573 mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
574 strncpy(sub_plat->pins[pinIndex].name, "SCL", 8);
575 sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
576 sub_plat->pins[pinIndex].i2c.mux_total = 0;
577 sub_plat->i2c_bus[bus].scl = pinIndex;
579 strncpy(sub_plat->pins[pinIndex].name, "SDA", 8);
580 sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
581 sub_plat->pins[pinIndex].i2c.mux_total = 0;
582 sub_plat->i2c_bus[bus].sda = pinIndex;
584 // Set override functions
585 mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
586 if (func_table == NULL) {
589 mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
591 mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
593 sub_plat->adv_func = func_table;