ft4222: adds initial support for on-chip GPIO in mode0
[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; //GPIO Handle
41 static FT_HANDLE ftHandle = (FT_HANDLE) NULL; //I2C/SPI Handle
42 static GPIO_Dir pinDirection[] = {GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT};
43 static int bus_speed = 400;
44 static int numI2cGpioExpanderPins;
45
46 mraa_result_t
47 mraa_ftdi_ft4222_init()
48 {
49     mraa_result_t mraaStatus = MRAA_SUCCESS;
50     FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
51     FT_STATUS ftStatus;
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
94     if(numDevs > 2) {
95         syslog(LOG_ERR, "CNFMODE not supported. Valid modes are 0 or 3.\n");
96         mraaStatus = MRAA_ERROR_NO_RESOURCES;
97         goto init_exit;
98     }
99
100     // CNFMODE 0, GPIO interface available.
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         // Disable Suspend and Wake on GPIO2 & GPIO3
110         FT4222_SetSuspendOut(ftHandleGPIO, 0);
111         FT4222_SetWakeUpInterrupt(ftHandleGPIO, 0);
112
113         ftStatus =  FT4222_GPIO_Init(ftHandleGPIO, pinDirection);
114         if (ftStatus != FT_OK) {
115             syslog(LOG_ERR, "FT4222_GPIO_Init failed (error %d)\n", (int) ftStatus);
116             mraaStatus = MRAA_ERROR_NO_RESOURCES;
117             goto init_exit;
118         }
119     }
120
121     // I2C or SPI interface.
122     ftStatus = FT_OpenEx((PVOID)(uintptr_t) devInfo[0].LocId, FT_OPEN_BY_LOCATION, &ftHandle);
123     if (ftStatus != FT_OK) {
124         syslog(LOG_ERR, "FT_OpenEx I2C handle failed (error %d)\n", (int) ftStatus);
125         mraaStatus = MRAA_ERROR_NO_RESOURCES;
126         goto init_exit;
127     }
128
129     // Tell the FT4222 to be an I2C Master by default on init.
130     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed);
131     if (FT4222_OK != ft4222Status) {
132         syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
133         mraaStatus = MRAA_ERROR_NO_RESOURCES;
134         goto init_exit;
135     }
136
137     // Reset the I2CM registers to a known state.
138     ft4222Status = FT4222_I2CMaster_Reset(ftHandle);
139     if (FT4222_OK != ft4222Status) {
140         syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
141         mraaStatus = MRAA_ERROR_NO_RESOURCES;
142         goto init_exit;
143     }
144
145 init_exit:
146     if (devInfo != NULL)
147         ;
148     free(devInfo);
149     if (mraaStatus == MRAA_SUCCESS)
150         syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n");
151     return mraaStatus;
152 }
153
154
155 mraa_result_t
156 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
157 {
158     if (ftHandle != NULL) {
159         FT4222_Version ft4222Version;
160         FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version);
161         if (FT4222_OK == ft4222Status) {
162             *versionChip = (unsigned int) ft4222Version.chipVersion;
163             *versionLib = (unsigned int) ft4222Version.dllVersion;
164             syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", *versionChip, *versionLib);
165             return MRAA_SUCCESS;
166         } else {
167             syslog(LOG_ERR, "libmraa: FT4222_GetVersion failed (error %d)\n", (int) ft4222Status);
168             return MRAA_ERROR_NO_RESOURCES;
169         }
170     } else {
171         syslog(LOG_ERR, "libmraa: bad FT4222 handle\n");
172         return MRAA_ERROR_INVALID_HANDLE;
173     }
174 }
175
176
177 static int
178 mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle, uint8_t addr, uint8_t* data, int length)
179 {
180     uint16 bytesRead = 0;
181     uint8 controllerStatus;
182     // syslog(LOG_NOTICE, "FT4222_I2CMaster_Read(%#02X, %#02X)", addr, length);
183     FT4222_STATUS ft4222Status = FT4222_I2CMaster_Read(handle, addr, data, length, &bytesRead);
184     ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
185     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
186         syslog(LOG_ERR, "FT4222_I2CMaster_Read failed (error %d)\n", (int) ft4222Status);
187         return 0;
188     }
189     return bytesRead;
190 }
191
192 static int
193 mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_t* data, int bytesToWrite)
194 {
195     uint16 bytesWritten = 0;
196     uint8 controllerStatus;
197     // syslog(LOG_NOTICE, "FT4222_I2CMaster_Write(%#02X, %#02X, %d)", addr, *data, bytesToWrite);
198     FT4222_STATUS ft4222Status =
199     FT4222_I2CMaster_Write(handle, addr, (uint8_t*) data, bytesToWrite, &bytesWritten);
200     ft4222Status = FT4222_I2CMaster_GetStatus(ftHandle, &controllerStatus);
201     if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
202         syslog(LOG_ERR, "FT4222_I2CMaster_Write failed (error %d)\n", (int) ft4222Status);
203         return 0;
204     }
205
206     if (bytesWritten != bytesToWrite)
207         syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
208
209     return bytesWritten;
210 }
211
212 // Function detects known I2C expanders and returns the number of GPIO pins on expander
213 static int
214 mraa_ftdi_ft4222_detect_io_expander()
215 {
216     uint8_t data;
217     if(mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &data, 1) == 1) {
218         return 8;
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     syslog(LOG_NOTICE, "I2C interface enabled GPIO0 and GPIO1 will be unavailable.\n");
274
275     dev->handle = ftHandle;
276     dev->fh = -1;              // We don't use file descriptors
277     dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
278                                // https://www.kernel.org/doc/Documentation/i2c/functionality
279     return MRAA_SUCCESS;
280 }
281
282
283 static mraa_result_t
284 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
285 {
286     switch (mode) {
287         case MRAA_I2C_STD: /**< up to 100Khz */
288             bus_speed = 100;
289             break;
290         MRAA_I2C_FAST: /**< up to 400Khz */
291             bus_speed = 400;
292             break;
293         MRAA_I2C_HIGH: /**< up to 3.4Mhz */
294             bus_speed = 3400;
295             break;
296     }
297     return MRAA_SUCCESS;
298 }
299
300
301 static mraa_result_t
302 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
303 {
304     dev->addr = (int) addr;
305     return FT4222_I2CMaster_Init(ftHandle, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
306 }
307
308
309 static int
310 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
311 {
312     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
313 }
314
315 static uint8_t
316 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
317 {
318     uint8_t data;
319     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, &data, 1) == 1)
320         return data;
321     else
322         return 0;
323 }
324
325
326 static uint16_t
327 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
328 {
329     uint8_t buf[2];
330     uint16_t data;
331     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
332         return 0;
333     if (mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, buf, 2) != 2)
334         return 0;
335     data = *(uint16_t*)buf;
336     return data;
337 }
338
339 static int
340 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
341 {
342     if (mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, &command, 1) != 1)
343         return 0;
344     return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
345 }
346
347
348 static mraa_result_t
349 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
350 {
351     uint16 bytesWritten = mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, data, bytesToWrite);
352     return bytesToWrite == bytesWritten ? MRAA_SUCCESS : MRAA_ERROR_INVALID_HANDLE;
353 }
354
355
356 static mraa_result_t
357 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
358 {
359     return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
360 }
361
362
363 static uint8_t
364 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
365 {
366     const uint8_t reg_addr = command;
367     uint8_t data;
368     if (mraa_ftdi_ft4222_i2c_write(dev, &reg_addr, 1) != MRAA_SUCCESS)
369         return 0;
370     if (mraa_ftdi_ft4222_i2c_read(dev, &data, 1) != 1)
371         return 0;
372     return data;
373 }
374
375 static mraa_result_t
376 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
377 {
378     uint8_t buf[2];
379     buf[0] = command;
380     buf[1] = data;
381     return mraa_ftdi_ft4222_i2c_write(dev, buf, 2);
382 }
383
384 static mraa_result_t
385 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
386 {
387     uint8_t buf[3];
388     buf[0] = command;
389     buf[1] = (uint8_t) data;
390     buf[2] = (uint8_t)(data >> 8);
391     return mraa_ftdi_ft4222_i2c_write(dev, buf, 3);
392 }
393
394 static mraa_result_t
395 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
396 {
397     return MRAA_SUCCESS;
398 }
399
400 /******************* GPIO functions *******************/
401
402 static mraa_result_t
403 mraa_ftdi_ft4222_gpio_init_internal_replace(int pin)
404 {
405     if ((pin - numI2cGpioExpanderPins) == 0 || (pin - numI2cGpioExpanderPins) == 1) {
406         syslog(LOG_NOTICE, "Closing I2C interface to enable GPIO%d\n", pin);
407
408         /* Replace with call to SPI init when SPI is fully implemented */
409         FT4222_STATUS ft4222Status = FT4222_SPIMaster_Init(ftHandle, SPI_IO_SINGLE, CLK_DIV_4, CLK_IDLE_HIGH, CLK_LEADING, 0x01);
410         if (FT4222_OK != ft4222Status){
411             syslog(LOG_ERR, "Failed to close I2C interface and start SPI (error %d)!\n", ft4222Status);
412             return MRAA_ERROR_NO_RESOURCES;
413         }
414     }
415     return MRAA_SUCCESS;
416 }
417
418 static mraa_result_t
419 mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
420 {
421     return MRAA_SUCCESS;
422 }
423
424 static mraa_result_t
425 mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev, mraa_gpio_edge_t mode)
426 {
427     return MRAA_SUCCESS;
428 }
429
430 static int
431 mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
432 {
433     uint8_t pin = dev->phy_pin;
434     uint8_t mask = 1 << pin;
435     uint8_t value;
436
437     if(pin >= numI2cGpioExpanderPins) {
438         // FTDI GPIO
439         FT4222_STATUS ft4222Status = FT4222_GPIO_Read(ftHandleGPIO, (pin - numI2cGpioExpanderPins), (BOOL*)&value);
440         if (FT4222_OK != ft4222Status) {
441             syslog(LOG_ERR, "FT4222_GPIO_Read failed (error %d)!\n", ft4222Status);
442             return -1;
443         }
444         return value;
445     }
446     else {
447         // Expander GPIO
448         if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
449             return -1;
450         return (value & mask) == mask;
451     }
452 }
453
454
455 static mraa_result_t
456 mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
457 {
458     uint8_t pin = dev->phy_pin;
459     uint8_t mask = 1 << pin;
460     uint8_t value;
461
462     if(pin >= numI2cGpioExpanderPins) {
463         // FTDI GPIO
464         FT4222_STATUS ft4222Status = FT4222_GPIO_Write(ftHandleGPIO, (pin - numI2cGpioExpanderPins), write_value);
465         if (FT4222_OK != ft4222Status) {
466             syslog(LOG_ERR, "FT4222_GPIO_Write failed (error %d)!\n", ft4222Status);
467             return MRAA_ERROR_UNSPECIFIED;
468         }
469     }
470     else {
471         // Expander GPIO
472         if (mraa_ftdi_ft4222_i2c_read_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
473             return MRAA_ERROR_UNSPECIFIED;
474         if (write_value == 1)
475             value |= mask;
476         else
477             value &= (~mask);
478         if (mraa_ftdi_ft4222_i2c_write_internal(ftHandle, PCA9672_ADDR, &value, 1) != 1)
479             return MRAA_ERROR_UNSPECIFIED;
480     }
481
482     return MRAA_SUCCESS;
483 }
484
485 static mraa_result_t
486 mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
487 {
488     switch (dir) {
489         case MRAA_GPIO_IN:
490             if (dev->phy_pin >= numI2cGpioExpanderPins) {
491                 pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_INPUT;
492                 if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
493                     return MRAA_ERROR_UNSPECIFIED;
494             }
495             return MRAA_SUCCESS;
496         case MRAA_GPIO_OUT:
497             if (dev->phy_pin >= numI2cGpioExpanderPins) {
498                 pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
499                 if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
500                     return MRAA_ERROR_UNSPECIFIED;
501             }
502             return MRAA_SUCCESS;
503         case MRAA_GPIO_OUT_HIGH:
504             if (dev->phy_pin >= numI2cGpioExpanderPins) {
505                 pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
506                 if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
507                     return MRAA_ERROR_UNSPECIFIED;
508             }
509             return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
510         case MRAA_GPIO_OUT_LOW:
511             if (dev->phy_pin >= numI2cGpioExpanderPins) {
512                 pinDirection[dev->phy_pin - numI2cGpioExpanderPins] = GPIO_OUTPUT;
513                 if (FT4222_GPIO_Init(ftHandleGPIO, pinDirection) != FT4222_OK)
514                     return MRAA_ERROR_UNSPECIFIED;
515             }
516             return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
517         default:
518             return MRAA_ERROR_INVALID_PARAMETER;
519     }
520 }
521
522 static void
523 mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)
524 {
525     struct timespec sleepTime;
526
527     sleepTime.tv_sec = mseconds / 1000;              // Number of seconds
528     sleepTime.tv_nsec = (mseconds % 1000) * 1000000; // Convert fractional seconds to nanoseconds
529
530     // Iterate nanosleep in a loop until the total sleep time is the original
531     // value of the seconds parameter
532     while ((nanosleep(&sleepTime, &sleepTime) != 0) && (errno == EINTR))
533         ;
534 }
535
536 static void*
537 mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
538 {
539     int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
540     while (1) {
541         int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
542         // MRAA_GPIO_EDGE_BOTH
543         if (level != prev_level) {
544             dev->isr(dev->isr_args);
545             prev_level = level;
546         }
547         // printf("mraa_ftdi_ft4222_gpio_interrupt_handler_replace\n");
548         mraa_ftdi_ft4222_sleep_ms(100);
549     }
550     return NULL;
551 }
552
553 static void
554 mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t* func_table)
555 {
556     func_table->i2c_init_bus_replace = &mraa_ftdi_ft4222_i2c_init_bus_replace;
557     func_table->i2c_set_frequency_replace = &mraa_ftdi_ft4222_i2c_frequency;
558     func_table->i2c_address_replace = &mraa_ftdi_ft4222_i2c_address;
559     func_table->i2c_read_replace = &mraa_ftdi_ft4222_i2c_read;
560     func_table->i2c_read_byte_replace = &mraa_ftdi_ft4222_i2c_read_byte;
561     func_table->i2c_read_byte_data_replace = &mraa_ftdi_ft4222_i2c_read_byte_data;
562     func_table->i2c_read_word_data_replace = &mraa_ftdi_ft4222_i2c_read_word_data;
563     func_table->i2c_read_bytes_data_replace = &mraa_ftdi_ft4222_i2c_read_bytes_data;
564     func_table->i2c_write_replace = &mraa_ftdi_ft4222_i2c_write;
565     func_table->i2c_write_byte_replace = &mraa_ftdi_ft4222_i2c_write_byte;
566     func_table->i2c_write_byte_data_replace = &mraa_ftdi_ft4222_i2c_write_byte_data;
567     func_table->i2c_write_word_data_replace = &mraa_ftdi_ft4222_i2c_write_word_data;
568     func_table->i2c_stop_replace = &mraa_ftdi_ft4222_i2c_stop;
569 }
570
571 static void
572 mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t* func_table)
573 {
574     func_table->gpio_init_internal_replace = &mraa_ftdi_ft4222_gpio_init_internal_replace;
575     func_table->gpio_mode_replace = &mraa_ftdi_ft4222_gpio_mode_replace;
576     func_table->gpio_edge_mode_replace = &mraa_ftdi_ft4222_gpio_edge_mode_replace;
577     func_table->gpio_dir_replace = &mraa_ftdi_ft4222_gpio_dir_replace;
578     func_table->gpio_read_replace = &mraa_ftdi_ft4222_gpio_read_replace;
579     func_table->gpio_write_replace = &mraa_ftdi_ft4222_gpio_write_replace;
580     func_table->gpio_interrupt_handler_replace = &mraa_ftdi_ft4222_gpio_interrupt_handler_replace;
581 }
582
583
584 mraa_board_t*
585 mraa_ftdi_ft4222()
586 {
587     mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
588     if (sub_plat == NULL)
589         return NULL;
590     numI2cGpioExpanderPins = mraa_ftdi_ft4222_detect_io_expander();
591     int pinIndex = 0;
592     int numUsbPins = numI2cGpioExpanderPins + 4; // Add GPIO0/SDA, GPIO1/SCL, GPIO2, GPIO3
593     sub_plat->platform_name = PLATFORM_NAME;
594     sub_plat->phy_pin_count = numUsbPins;
595     sub_plat->gpio_count = numUsbPins;
596     mraa_pininfo_t* pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * numUsbPins);
597     if (pins == NULL) {
598         return NULL;
599     }
600     sub_plat->pins = pins;
601
602     // Virtual gpio pins on i2c I/O expander.
603     mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
604     for (pinIndex = 0; pinIndex < numI2cGpioExpanderPins; ++pinIndex) {
605         char name[8];
606         sprintf(name, "Pin%d", pinIndex);
607         strncpy(sub_plat->pins[pinIndex].name, name, 8);
608         sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
609         sub_plat->pins[pinIndex].gpio.mux_total = 0;
610     }
611
612     int bus = 0;
613     sub_plat->i2c_bus_count = 1;
614     sub_plat->def_i2c_bus = bus;
615     sub_plat->i2c_bus[bus].bus_id = bus;
616
617     // I2c pins (these are virtual, entries are required to configure i2c layer)
618     mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 1, 0, 0 };
619     strncpy(sub_plat->pins[pinIndex].name, "SCL/GPIO0", 10);
620     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
621     sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
622     sub_plat->pins[pinIndex].gpio.mux_total = 0;
623     sub_plat->pins[pinIndex].i2c.mux_total = 0;
624     sub_plat->i2c_bus[bus].scl = pinIndex;
625     pinIndex++;
626     strncpy(sub_plat->pins[pinIndex].name, "SDA/GPIO1", 10);
627     sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
628     sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
629     sub_plat->pins[pinIndex].gpio.mux_total = 0;
630     sub_plat->pins[pinIndex].i2c.mux_total = 0;
631     sub_plat->i2c_bus[bus].sda = pinIndex;
632     pinIndex++;
633
634     // FTDI4222 gpio
635     strncpy(sub_plat->pins[pinIndex].name, "GPIO2", 8);
636     sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
637     sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
638     sub_plat->pins[pinIndex].gpio.mux_total = 0;
639     pinIndex++;
640     strncpy(sub_plat->pins[pinIndex].name, "GPIO3", 8);
641     sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
642     sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
643     sub_plat->pins[pinIndex].gpio.mux_total = 0;
644
645     // Set override functions
646     mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
647     if (func_table == NULL) {
648         return NULL;
649     }
650     mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
651     mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
652
653     sub_plat->adv_func = func_table;
654     return sub_plat;
655 }