cf638a2f3ef4528ace468bcd1f1718251f5ed9f9
[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 ftHandle = (FT_HANDLE) NULL;
41 static int bus_speed = 400;
42 static int numI2cGpioExapnderPins = 8;
43 static int numUsbGpio = 0;
44
45
46 mraa_result_t
47 mraa_ftdi_ft4222_init()
48 {
49     mraa_result_t mraaStatus = MRAA_SUCCESS;
50     FT_STATUS ftStatus;
51     FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
52     DWORD numDevs = 0;
53     int i;
54     int retCode = 0;
55
56     ftStatus = FT_CreateDeviceInfoList(&numDevs);
57     if (ftStatus != FT_OK) {
58         syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus);
59         mraaStatus = MRAA_ERROR_NO_RESOURCES;
60         goto init_exit;
61     }
62
63     if (numDevs == 0) {
64         syslog(LOG_ERR, "No FT4222 devices connected.\n");
65         goto init_exit;
66     }
67
68     devInfo = calloc((size_t) numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE));
69     if (devInfo == NULL) {
70         syslog(LOG_ERR, "FT4222 allocation failure.\n");
71         mraaStatus = MRAA_ERROR_NO_RESOURCES;
72         goto init_exit;
73     }
74
75     ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
76     if (ftStatus != FT_OK) {
77         syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int) ftStatus);
78         mraaStatus = MRAA_ERROR_NO_RESOURCES;
79         goto init_exit;
80     }
81
82     /*
83             FT4222_Version ft4222Version;
84             FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
85             if (FT4222_OK == ft4222Status){
86                 syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", ft4222Version.chipVersion,
87        ft4222Version.dllVersion);
88              } else
89                 syslog(LOG_ERR, "FT4222_GetVersion failed with code %d", ft4222Status);
90     */
91
92     syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs);
93     DWORD locationId = 0;
94     for (i = 0; i < numDevs && locationId == 0; ++i) {
95         // printf("%d: type = %d, location = %d\n", i, devInfo[i].Type, devInfo[i].LocId);
96         if (devInfo[i].Type == FT_DEVICE_4222H_0 || devInfo[i].Type == FT_DEVICE_4222H_3)
97             locationId = devInfo[i].LocId;
98     }
99     if (locationId == 0) {
100         syslog(LOG_ERR, "FT_GetDeviceInfoList contains no valid devices\n");
101         mraaStatus = MRAA_ERROR_NO_RESOURCES;
102         goto init_exit;
103     }
104
105     ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationId, FT_OPEN_BY_LOCATION, &ftHandle);
106     if (ftStatus != FT_OK) {
107         syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus);
108         mraaStatus = MRAA_ERROR_NO_RESOURCES;
109         goto init_exit;
110     }
111
112     // Tell the FT4222 to be an I2C Master.
113     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
114     if (FT4222_OK != ft4222Status) {
115         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
116         mraaStatus = MRAA_ERROR_NO_RESOURCES;
117         goto init_exit;
118     }
119
120     // Reset the I2CM registers to a known state.
121     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
122     if (FT4222_OK != ft4222Status) {
123         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
124         mraaStatus = MRAA_ERROR_NO_RESOURCES;
125         goto init_exit;
126     }
127
128
129 init_exit:
130     if (devInfo != NULL)
131         ;
132     free(devInfo);
133     if (mraaStatus == MRAA_SUCCESS)
134         syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n");
135     return mraaStatus;
136 }
137
138
139 mraa_result_t
140 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
141 {
142     if (ftHandle != NULL) {
143         FT4222_Version ft4222Version;
144         FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
145         if (FT4222_OK == ft4222Status) {
146             *versionChip = (unsigned int) ft4222Version.chipVersion;
147             *versionLib = (unsigned int) ft4222Version.dllVersion;
148             syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", *versionChip, *versionLib);
149             return MRAA_SUCCESS;
150         } else {
151             syslog(LOG_ERR, "libmraa: FT4222_GetVersion failed (error %d)\n", (int) ft4222Status);
152             return MRAA_ERROR_NO_RESOURCES;
153         }
154     } else {
155         syslog(LOG_ERR, "libmraa: bad FT4222 handle\n");
156         return MRAA_ERROR_INVALID_HANDLE;
157     }
158 }
159
160
161 static int
162 mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle, uint8_t addr, uint8_t* data, int length)
163 {
164     uint16 bytesRead = 0;
165     uint8 controllerStatus;
166     // syslog(LOG_NOTICE, "FT4222_I2CMaster_Read(%#02X, %#02X)", addr, length);
167     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Read(handle, addr, data, length, &bytesRead);
168     ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
169     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
170         syslog(LOG_ERR, "FT4222_I2CMaster_Read failed (error %d)\n", (int) ft4222Status);
171         return 0;
172     }
173     return bytesRead;
174 }
175
176 static int
177 mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_t* data, int bytesToWrite)
178 {
179     uint16 bytesWritten = 0;
180     uint8 controllerStatus;
181     // syslog(LOG_NOTICE, "FT4222_I2CMaster_Write(%#02X, %#02X, %d)", addr, *data, bytesToWrite);
182     FT4222_STATUS ft4222Status =
183     FT4222_I2CMaster_Write(handle, addr, (uint8_t*) data, bytesToWrite, &bytesWritten);
184     ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
185     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
186         syslog(LOG_ERR, "FT4222_I2CMaster_Write failed (error %d)\n", (int) ft4222Status);
187         return 0;
188     }
189
190     if (bytesWritten != bytesToWrite)
191         syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
192
193     return bytesWritten;
194 }
195
196
197 static mraa_boolean_t
198 mraa_ftdi_ft4222_detect_io_expander()
199 {
200     uint8_t data;
201     return mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &data, 1) == 1;
202 }
203
204
205 /******************* I2C functions *******************/
206 static mraa_i2c_context
207 mraa_ftdi_ft4222_i2c_init_raw(unsigned int bus)
208 {
209     // Tell the FT4222 to be an I2C Master.
210     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
211     if (FT4222_OK != ft4222Status) {
212         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
213         return NULL;
214     }
215
216     // Reset the I2CM registers to a known state.
217     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
218     if (FT4222_OK != ft4222Status) {
219         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
220         return NULL;
221     }
222
223     mraa_i2c_context dev = (mraa_i2c_context) malloc(sizeof(struct _i2c));
224     if (dev == NULL) {
225         syslog(LOG_CRIT, "i2c: Failed to allocate memory for context");
226         return NULL;
227     }
228
229     dev->handle = ftHandle;
230     dev->fh = -1;              // We don't use file descriptors
231     dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
232                                // https://www.kernel.org/doc/Documentation/i2c/functionality
233     return dev;
234 }
235
236 static mraa_result_t
237 mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
238 {
239     // Tell the FT4222 to be an I2C Master.
240     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, 400);
241     if (FT4222_OK != ft4222Status) {
242         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
243         return MRAA_ERROR_NO_RESOURCES;
244     }
245
246     // Reset the I2CM registers to a known state.
247     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
248     if (FT4222_OK != ft4222Status) {
249         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
250         return MRAA_ERROR_NO_RESOURCES;
251     }
252
253     dev->handle = ftHandle;
254     dev->fh = -1;              // We don't use file descriptors
255     dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
256                                // https://www.kernel.org/doc/Documentation/i2c/functionality
257     return MRAA_SUCCESS;
258 }
259
260
261 static mraa_result_t
262 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
263 {
264     switch (mode) {
265         case MRAA_I2C_STD: /**< up to 100Khz */
266             bus_speed = 100;
267             break;
268         MRAA_I2C_FAST: /**< up to 400Khz */
269             bus_speed = 400;
270             break;
271         MRAA_I2C_HIGH: /**< up to 3.4Mhz */
272             bus_speed = 3400;
273             break;
274     }
275     return MRAA_SUCCESS;
276 }
277
278
279 static mraa_result_t
280 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
281 {
282     dev->addr = (int) addr;
283     return FT4222_I2CMaster_Init(ftHandle, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
284 }
285
286
287 static int
288 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
289 {
290     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
291 }
292
293 static uint8_t
294 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
295 {
296     uint8_t data;
297     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, &data, 1) == 1)
298         return data;
299     else
300         return 0;
301 }
302
303
304 static uint16_t
305 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
306 {
307     uint8_t buf[2];
308     uint16_t data;
309     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
310         return 0;
311     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, buf, 2) != 2)
312         return 0;
313     data = *(uint16_t*)buf;
314     return data;
315 }
316
317 static int
318 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
319 {
320     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
321         return 0;
322     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
323 }
324
325
326 static mraa_result_t
327 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
328 {
329     uint16 bytesWritten = mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, data, bytesToWrite);
330     return bytesToWrite == bytesWritten ? MRAA_SUCCESS : MRAA_ERROR_INVALID_HANDLE;
331 }
332
333
334 static mraa_result_t
335 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
336 {
337     return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
338 }
339
340
341 static uint8_t
342 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
343 {
344     const uint8_t reg_addr = command;
345     uint8_t data;
346     if (mraa_ftdi_ft4222_i2c_write(dev, &reg_addr, 1) != MRAA_SUCCESS)
347         return 0;
348     if (mraa_ftdi_ft4222_i2c_read(dev, &data, 1) != 1)
349         return 0;
350     return data;
351 }
352
353 static mraa_result_t
354 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
355 {
356     uint8_t buf[2];
357     buf[0] = command;
358     buf[1] = data;
359     return mraa_ftdi_ft4222_i2c_write(dev, buf, 2);
360 }
361
362 static mraa_result_t
363 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
364 {
365     uint8_t buf[3];
366     buf[0] = command;
367     buf[1] = (uint8_t) data;
368     buf[2] = (uint8_t)(data >> 8);
369     return mraa_ftdi_ft4222_i2c_write(dev, buf, 3);
370 }
371
372 static mraa_result_t
373 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
374 {
375     return MRAA_SUCCESS;
376 }
377
378 /******************* GPIO functions *******************/
379
380 static mraa_result_t
381 mraa_ftdi_ft4222_gpio_init_internal_replace(int pin)
382 {
383     return MRAA_SUCCESS;
384 }
385
386 static mraa_result_t
387 mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
388 {
389     return MRAA_SUCCESS;
390 }
391
392 static mraa_result_t
393 mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev, mraa_gpio_edge_t mode)
394 {
395     return MRAA_SUCCESS;
396 }
397
398 static int
399 mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
400 {
401     uint8_t pin = dev->phy_pin;
402     uint8_t mask = 1 << pin;
403     uint8_t value;
404     if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
405         return -1;
406     return (value & mask) == mask;
407 }
408
409
410 static mraa_result_t
411 mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
412 {
413     uint8_t pin = dev->phy_pin;
414     uint8_t mask = 1 << pin;
415     uint8_t value;
416     if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
417         return MRAA_ERROR_UNSPECIFIED;
418     if (write_value == 1)
419         value |= mask;
420     else
421         value &= (~mask);
422     if (mraa_ftdi_ft4222_i2c_write_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
423         return MRAA_ERROR_UNSPECIFIED;
424     return MRAA_SUCCESS;
425 }
426
427 static mraa_result_t
428 mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
429 {
430     switch (dir) {
431         case MRAA_GPIO_IN:
432         case MRAA_GPIO_OUT_HIGH:
433             return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
434         case MRAA_GPIO_OUT_LOW:
435             return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
436         default:
437             ;
438     }
439     return MRAA_SUCCESS;
440 }
441
442 static void
443 mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)
444 {
445     struct timespec sleepTime;
446
447     sleepTime.tv_sec = mseconds / 1000;              // Number of seconds
448     sleepTime.tv_nsec = (mseconds % 1000) * 1000000; // Convert fractional seconds to nanoseconds
449
450     // Iterate nanosleep in a loop until the total sleep time is the original
451     // value of the seconds parameter
452     while ((nanosleep(&sleepTime, &sleepTime) != 0) && (errno == EINTR))
453         ;
454 }
455
456 static void*
457 mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
458 {
459     int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
460     while (1) {
461         int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
462         if (level != prev_level) {
463             dev->isr(dev->isr_args);
464             prev_level = level;
465         }
466         // printf("mraa_ftdi_ft4222_gpio_interrupt_handler_replace\n");
467         mraa_ftdi_ft4222_sleep_ms(100);
468     }
469     return NULL;
470 }
471
472 static void
473 mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t* func_table)
474 {
475     func_table->i2c_init_bus_replace = &mraa_ftdi_ft4222_i2c_init_bus_replace;
476     func_table->i2c_set_frequency_replace = &mraa_ftdi_ft4222_i2c_frequency;
477     func_table->i2c_address_replace = &mraa_ftdi_ft4222_i2c_address;
478     func_table->i2c_read_replace = &mraa_ftdi_ft4222_i2c_read;
479     func_table->i2c_read_byte_replace = &mraa_ftdi_ft4222_i2c_read_byte;
480     func_table->i2c_read_byte_data_replace = &mraa_ftdi_ft4222_i2c_read_byte_data;
481     func_table->i2c_read_word_data_replace = &mraa_ftdi_ft4222_i2c_read_word_data;
482     func_table->i2c_read_bytes_data_replace = &mraa_ftdi_ft4222_i2c_read_bytes_data;
483     func_table->i2c_write_replace = &mraa_ftdi_ft4222_i2c_write;
484     func_table->i2c_write_byte_replace = &mraa_ftdi_ft4222_i2c_write_byte;
485     func_table->i2c_write_byte_data_replace = &mraa_ftdi_ft4222_i2c_write_byte_data;
486     func_table->i2c_write_word_data_replace = &mraa_ftdi_ft4222_i2c_write_word_data;
487     func_table->i2c_stop_replace = &mraa_ftdi_ft4222_i2c_stop;
488 }
489
490 static void
491 mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t* func_table)
492 {
493     func_table->gpio_init_internal_replace = &mraa_ftdi_ft4222_gpio_init_internal_replace;
494     func_table->gpio_mode_replace = &mraa_ftdi_ft4222_gpio_mode_replace;
495     func_table->gpio_edge_mode_replace = &mraa_ftdi_ft4222_gpio_edge_mode_replace;
496     func_table->gpio_dir_replace = &mraa_ftdi_ft4222_gpio_dir_replace;
497     func_table->gpio_read_replace = &mraa_ftdi_ft4222_gpio_read_replace;
498     func_table->gpio_write_replace = &mraa_ftdi_ft4222_gpio_write_replace;
499     func_table->gpio_interrupt_handler_replace = &mraa_ftdi_ft4222_gpio_interrupt_handler_replace;
500 }
501
502
503 mraa_board_t*
504 mraa_ftdi_ft4222()
505 {
506     mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
507     if (sub_plat == NULL)
508         return NULL;
509     mraa_boolean_t haveGpio = mraa_ftdi_ft4222_detect_io_expander();
510     int pinIndex = 0;
511     numUsbGpio = haveGpio ? numI2cGpioExapnderPins : 0;
512     int numUsbPins = numUsbGpio + 2; // Add SDA and SCL
513     sub_plat->platform_name = PLATFORM_NAME;
514     sub_plat->phy_pin_count = numUsbPins;
515     sub_plat->gpio_count = numUsbGpio;
516     mraa_pininfo_t* pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * numUsbPins);
517     if (pins == NULL) {
518         return NULL;
519     }
520     sub_plat->pins = pins;
521
522     // Virtual gpio pins on i2c I/O expander.
523     mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
524     for (pinIndex = 0; pinIndex < numUsbGpio; ++pinIndex) {
525         char name[8];
526         sprintf(name, "Pin%d", pinIndex);
527         strncpy(sub_plat->pins[pinIndex].name, name, 8);
528         sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
529         sub_plat->pins[pinIndex].gpio.mux_total = 0;
530     }
531
532     int bus = 0;
533     sub_plat->i2c_bus_count = 1;
534     sub_plat->def_i2c_bus = bus;
535     sub_plat->i2c_bus[bus].bus_id = bus;
536
537     // i2c pins (these are virtual, entries are required to configure i2c layer)
538     mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
539     strncpy(sub_plat->pins[pinIndex].name, "SDA", 8);
540     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
541     sub_plat->pins[pinIndex].i2c.mux_total = 0;
542     sub_plat->i2c_bus[bus].sda = pinIndex;
543     pinIndex++;
544     strncpy(sub_plat->pins[pinIndex].name, "SCL", 8);
545     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
546     sub_plat->pins[pinIndex].i2c.mux_total = 0;
547     sub_plat->i2c_bus[bus].scl = pinIndex;
548
549     // Set override functions
550     mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
551     if (func_table == NULL) {
552         return NULL;
553     }
554     mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
555     if (haveGpio)
556         mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
557
558     sub_plat->adv_func = func_table;
559     return sub_plat;
560 }