ft4222: prepares onboard gpio support in addition to i2c expander option
[contrib/mraa.git] / src / usb / ftdi_ft4222.c
1 /*
2  * Author: Henry Bruce <henry.bruce@intel.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <errno.h>
29 #include "linux/i2c-dev.h"
30 #include "common.h"
31 #include "ftd2xx.h"
32 #include "libft4222.h"
33 #include "usb/ftdi_ft4222.h"
34
35 #define PLATFORM_NAME "FTDI FT4222"
36 #define I2CM_ERROR(status) (((status) &0x02) != 0)
37 #define PCA9672_ADDR 0x20
38
39
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;
46
47 mraa_result_t
48 mraa_ftdi_ft4222_init()
49 {
50     mraa_result_t mraaStatus = MRAA_SUCCESS;
51     FT_STATUS ftStatus;
52     FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
53     DWORD numDevs = 0;
54     int i;
55     int retCode = 0;
56
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;
61         goto init_exit;
62     }
63
64     if (numDevs == 0) {
65         syslog(LOG_ERR, "No FT4222 devices connected.\n");
66         goto init_exit;
67     }
68
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;
73         goto init_exit;
74     }
75
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;
80         goto init_exit;
81     }
82
83     /*
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);
89              } else
90                 syslog(LOG_ERR, "FT4222_GetVersion failed with code %d", ft4222Status);
91     */
92
93     syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs);
94
95     if(numDevs > 2) {
96         syslog(LOG_ERR, "CNFMODE not supported. Valid modes are 0 or 3.\n");
97         mraaStatus = MRAA_ERROR_NO_RESOURCES;
98         goto init_exit;
99     }
100
101     if(numDevs = 2) {
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;
106             goto init_exit;
107         }
108
109         FT4222_SetSuspendOut(ftHandleGPIO, false);
110         FT4222_SetWakeUpInterrupt(ftHandleGPIO, false);
111
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;
116             goto init_exit;
117         }
118     }
119
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;
124         goto init_exit;
125     }
126
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;
132         goto init_exit;
133     }
134
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;
140         goto init_exit;
141     }
142
143
144 init_exit:
145     if (devInfo != NULL)
146         ;
147     free(devInfo);
148     if (mraaStatus == MRAA_SUCCESS)
149         syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n");
150     return mraaStatus;
151 }
152
153
154 mraa_result_t
155 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
156 {
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);
164             return MRAA_SUCCESS;
165         } else {
166             syslog(LOG_ERR, "libmraa: FT4222_GetVersion failed (error %d)\n", (int) ft4222Status);
167             return MRAA_ERROR_NO_RESOURCES;
168         }
169     } else {
170         syslog(LOG_ERR, "libmraa: bad FT4222 handle\n");
171         return MRAA_ERROR_INVALID_HANDLE;
172     }
173 }
174
175
176 static int
177 mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle, uint8_t addr, uint8_t* data, int length)
178 {
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);
186         return 0;
187     }
188     return bytesRead;
189 }
190
191 static int
192 mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_t* data, int bytesToWrite)
193 {
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);
202         return 0;
203     }
204
205     if (bytesWritten != bytesToWrite)
206         syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
207
208     return bytesWritten;
209 }
210
211
212 static mraa_boolean_t
213 mraa_ftdi_ft4222_detect_io_expander()
214 {
215     uint8_t data;
216     if(mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &data, 1) == 1) {
217         numI2cGpioExapnderPins = 8;
218         return 1;
219     }
220     return 0;
221 }
222
223
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)
228 {
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);
233         return NULL;
234     }
235
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);
240         return NULL;
241     }
242
243     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
244     if (dev == NULL) {
245         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
246         return NULL;
247     }
248
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
253     return dev;
254 }
255
256 static mraa_result_t
257 mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
258 {
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;
264     }
265
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;
271     }
272
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
277     return MRAA_SUCCESS;
278 }
279
280
281 static mraa_result_t
282 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
283 {
284     switch (mode) {
285         case MRAA_I2C_STD: /**< up to 100Khz */
286             bus_speed = 100;
287             break;
288         MRAA_I2C_FAST: /**< up to 400Khz */
289             bus_speed = 400;
290             break;
291         MRAA_I2C_HIGH: /**< up to 3.4Mhz */
292             bus_speed = 3400;
293             break;
294     }
295     return MRAA_SUCCESS;
296 }
297
298
299 static mraa_result_t
300 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
301 {
302     dev->addr = (int) addr;
303     return FT4222_I2CMaster_Init(ftHandle, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
304 }
305
306
307 static int
308 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
309 {
310     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
311 }
312
313 static uint8_t
314 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
315 {
316     uint8_t data;
317     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, &data, 1) == 1)
318         return data;
319     else
320         return 0;
321 }
322
323
324 static uint16_t
325 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
326 {
327     uint8_t buf[2];
328     uint16_t data;
329     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
330         return 0;
331     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, buf, 2) != 2)
332         return 0;
333     data = *(uint16_t*)buf;
334     return data;
335 }
336
337 static int
338 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
339 {
340     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
341         return 0;
342     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
343 }
344
345
346 static mraa_result_t
347 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
348 {
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;
351 }
352
353
354 static mraa_result_t
355 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
356 {
357     return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
358 }
359
360
361 static uint8_t
362 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
363 {
364     const uint8_t reg_addr = command;
365     uint8_t data;
366     if (mraa_ftdi_ft4222_i2c_write(dev, &reg_addr, 1) != MRAA_SUCCESS)
367         return 0;
368     if (mraa_ftdi_ft4222_i2c_read(dev, &data, 1) != 1)
369         return 0;
370     return data;
371 }
372
373 static mraa_result_t
374 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
375 {
376     uint8_t buf[2];
377     buf[0] = command;
378     buf[1] = data;
379     return mraa_ftdi_ft4222_i2c_write(dev, buf, 2);
380 }
381
382 static mraa_result_t
383 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
384 {
385     uint8_t buf[3];
386     buf[0] = command;
387     buf[1] = (uint8_t) data;
388     buf[2] = (uint8_t)(data >> 8);
389     return mraa_ftdi_ft4222_i2c_write(dev, buf, 3);
390 }
391
392 static mraa_result_t
393 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
394 {
395     return MRAA_SUCCESS;
396 }
397
398 /******************* GPIO functions *******************/
399
400 static mraa_result_t
401 mraa_ftdi_ft4222_gpio_init_internal_replace(int pin)
402 {
403     return MRAA_SUCCESS;
404 }
405
406 static mraa_result_t
407 mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
408 {
409     return MRAA_SUCCESS;
410 }
411
412 static mraa_result_t
413 mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev, mraa_gpio_edge_t mode)
414 {
415     return MRAA_SUCCESS;
416 }
417
418 static int
419 mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
420 {
421     uint8_t pin = dev->phy_pin;
422     uint8_t mask = 1 << pin;
423     uint8_t value;
424     if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
425         return -1;
426     return (value & mask) == mask;
427 }
428
429
430 static mraa_result_t
431 mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
432 {
433     uint8_t pin = dev->phy_pin;
434     uint8_t mask = 1 << pin;
435     uint8_t value;
436     if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
437         return MRAA_ERROR_UNSPECIFIED;
438     if (write_value == 1)
439         value |= mask;
440     else
441         value &= (~mask);
442     if (mraa_ftdi_ft4222_i2c_write_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
443         return MRAA_ERROR_UNSPECIFIED;
444     return MRAA_SUCCESS;
445 }
446
447 static mraa_result_t
448 mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
449 {
450     switch (dir) {
451         case MRAA_GPIO_IN:
452             if(dev->phy_pin >= numI2cGpioExapnderPins) {
453                 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_INPUT;
454                 return MRAA_SUCCESS;
455             }
456         case MRAA_GPIO_OUT:
457             if(dev->phy_pin >= numI2cGpioExapnderPins) {
458                 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
459                 return MRAA_SUCCESS;
460             }
461         case MRAA_GPIO_OUT_HIGH:
462             if(dev->phy_pin >= numI2cGpioExapnderPins) {
463                 pinDirection[dev->phy_pin - numI2cGpioExapnderPins] = GPIO_OUTPUT;
464             }
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;
469             }
470             return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
471         default:
472             ;
473     }
474     return MRAA_SUCCESS;
475 }
476
477 static void
478 mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)
479 {
480     struct timespec sleepTime;
481
482     sleepTime.tv_sec = mseconds / 1000;              // Number of seconds
483     sleepTime.tv_nsec = (mseconds % 1000) * 1000000; // Convert fractional seconds to nanoseconds
484
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))
488         ;
489 }
490
491 static void*
492 mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
493 {
494     int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
495     while (1) {
496         int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
497         if (level != prev_level) {
498             dev->isr(dev->isr_args);
499             prev_level = level;
500         }
501         // printf("mraa_ftdi_ft4222_gpio_interrupt_handler_replace\n");
502         mraa_ftdi_ft4222_sleep_ms(100);
503     }
504     return NULL;
505 }
506
507 static void
508 mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t* func_table)
509 {
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;
523 }
524
525 static void
526 mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t* func_table)
527 {
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;
535 }
536
537
538 mraa_board_t*
539 mraa_ftdi_ft4222()
540 {
541     mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
542     if (sub_plat == NULL)
543         return NULL;
544     mraa_boolean_t haveGpio = mraa_ftdi_ft4222_detect_io_expander();
545     int pinIndex = 0;
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);
552     if (pins == NULL) {
553         return NULL;
554     }
555     sub_plat->pins = pins;
556
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) {
560         char name[8];
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;
565     }
566
567     int bus = 0;
568     sub_plat->i2c_bus_count = 1;
569     sub_plat->def_i2c_bus = bus;
570     sub_plat->i2c_bus[bus].bus_id = bus;
571
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;
578     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;
583
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) {
587         return NULL;
588     }
589     mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
590     if (haveGpio)
591         mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
592
593     sub_plat->adv_func = func_table;
594     return sub_plat;
595 }