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 <sys/time.h>
29 #include <errno.h>
30 #include "linux/i2c-dev.h"
31 #include "common.h"
32 #include "ftd2xx.h"
33 #include "libft4222.h"
34 #include "usb/ftdi_ft4222.h"
35
36 #define PLATFORM_NAME "FTDI FT4222"
37 #define I2CM_ERROR(status) (((status) &0x02) != 0)
38 #define PCA9672_ADDR 0x20
39 #define PCA9545_ADDR 0x70
40 #define PCA9672_PINS 8
41 #define PCA9545_BUSSES 4
42 #define GPIO_PORT_IO_RESET GPIO_PORT2
43 #define GPIO_PORT_IO_STATUS GPIO_PORT3
44
45 static FT_HANDLE ftHandleGpio = (FT_HANDLE) NULL; //GPIO Handle
46 static FT_HANDLE ftHandleI2c = (FT_HANDLE) NULL; //I2C/SPI Handle
47 static FT_HANDLE ftHandleSpi = (FT_HANDLE) NULL; //I2C/SPI Handle
48 static GPIO_Dir pinDirection[] = {GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT, GPIO_OUTPUT};
49 static int bus_speed = 400;
50 static int numFt4222GpioPins = 4;
51 static int numI2cGpioExpanderPins = 8;
52 static int numI2cSwitchBusses = 4;
53 static int currentI2cBus = 0;
54
55 mraa_result_t
mraa_ftdi_ft4222_init()56 mraa_ftdi_ft4222_init()
57 {
58 mraa_result_t mraaStatus = MRAA_ERROR_NO_RESOURCES;
59 FT_DEVICE_LIST_INFO_NODE* devInfo = NULL;
60 FT_STATUS ftStatus;
61 DWORD numDevs = 0;
62 int i;
63 int retCode = 0;
64
65 ftStatus = FT_CreateDeviceInfoList(&numDevs);
66 if (ftStatus != FT_OK) {
67 syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus);
68 goto init_exit;
69 }
70
71 devInfo = calloc((size_t) numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE));
72 if (devInfo == NULL) {
73 syslog(LOG_ERR, "FT4222 allocation failure.\n");
74 goto init_exit;
75 }
76
77 ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
78 syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs);
79 if (ftStatus != FT_OK) {
80 syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int) ftStatus);
81 goto init_exit;
82 }
83 if (numDevs < 2) {
84 syslog(LOG_ERR, "No FT4222 devices connected.\n");
85 goto init_exit;
86 }
87 if(numDevs > 2) {
88 syslog(LOG_ERR, "CNFMODE not supported. Valid modes are 0 or 3.\n");
89 goto init_exit;
90 }
91
92 // FIXME: Assumes just one physical FTDI device present
93 DWORD locationIdI2c = 0;
94 DWORD locationIdGpio = 0;
95 if (devInfo[0].Type == FT_DEVICE_4222H_0)
96 locationIdI2c = devInfo[0].LocId;
97 if (devInfo[1].Type == FT_DEVICE_4222H_0)
98 locationIdGpio = devInfo[1].LocId;
99
100 if (locationIdI2c == 0) {
101 syslog(LOG_ERR, "FT_GetDeviceInfoList contains no I2C controllers\n");
102 goto init_exit;
103 }
104
105 if (locationIdGpio == 0) {
106 syslog(LOG_ERR, "FT_GetDeviceInfoList contains no GPIO controllers\n");
107 goto init_exit;
108 }
109
110 ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationIdI2c, FT_OPEN_BY_LOCATION, &ftHandleI2c);
111 if (ftStatus != FT_OK) {
112 syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus);
113 goto init_exit;
114 }
115
116 ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationIdGpio, FT_OPEN_BY_LOCATION, &ftHandleGpio);
117 if (ftStatus != FT_OK) {
118 syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus);
119 goto init_exit;
120 }
121
122 FT4222_SetSuspendOut(ftHandleGpio, 0);
123 FT4222_SetWakeUpInterrupt(ftHandleGpio, 0);
124 ftStatus = FT4222_GPIO_Init(ftHandleGpio, pinDirection);
125 if (ftStatus != FT_OK) {
126 syslog(LOG_ERR, "FT4222_GPIO_Init failed (error %d)\n", (int) ftStatus);
127 mraaStatus = MRAA_ERROR_NO_RESOURCES;
128 goto init_exit;
129 }
130
131 // Tell the FT4222 to be an I2C Master by default on init.
132 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandleI2c, bus_speed);
133 if (FT4222_OK != ft4222Status) {
134 syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
135 goto init_exit;
136 }
137
138 ft4222Status = FT4222_I2CMaster_Reset(ftHandleI2c);
139 if (FT4222_OK != ft4222Status) {
140 syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
141 goto init_exit;
142 }
143
144 mraaStatus = MRAA_SUCCESS;
145
146 init_exit:
147 if (devInfo != NULL)
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
mraa_ftdi_ft4222_get_version(unsigned int * versionChip,unsigned int * versionLib)156 mraa_ftdi_ft4222_get_version(unsigned int* versionChip, unsigned int* versionLib)
157 {
158 if (ftHandleI2c != NULL) {
159 FT4222_Version ft4222Version;
160 FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandleI2c, &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, "FT4222_GetVersion failed (error %d)\n", (int) ft4222Status);
168 return MRAA_ERROR_NO_RESOURCES;
169 }
170 } else {
171 syslog(LOG_ERR, "Bad FT4222 handle\n");
172 return MRAA_ERROR_INVALID_HANDLE;
173 }
174 }
175
176
177 /******************* Private I2C functions *******************/
178
179 static int
mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle,uint8_t addr,uint8_t * data,int length)180 mraa_ftdi_ft4222_i2c_read_internal(FT_HANDLE handle, uint8_t addr, uint8_t* data, int length)
181 {
182 uint16 bytesRead = 0;
183 uint8 controllerStatus;
184 // syslog(LOG_NOTICE, "FT4222_I2CMaster_Read(%#02X, %#02X)", addr, length);
185 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Read(handle, addr, data, length, &bytesRead);
186 ft4222Status = FT4222_I2CMaster_GetStatus(ftHandleI2c, &controllerStatus);
187 if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
188 syslog(LOG_ERR, "FT4222_I2CMaster_Read failed for address %#02x\n", addr);
189 FT4222_I2CMaster_Reset(handle);
190 return 0;
191 }
192 return bytesRead;
193 }
194
195 static int
mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle,uint8_t addr,const uint8_t * data,int bytesToWrite)196 mraa_ftdi_ft4222_i2c_write_internal(FT_HANDLE handle, uint8_t addr, const uint8_t* data, int bytesToWrite)
197 {
198 uint16 bytesWritten = 0;
199 uint8 controllerStatus;
200 // syslog(LOG_NOTICE, "FT4222_I2CMaster_Write(%#02X, %#02X, %d)", addr, *data, bytesToWrite);
201 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Write(handle, addr, (uint8_t*) data, bytesToWrite, &bytesWritten);
202 ft4222Status = FT4222_I2CMaster_GetStatus(ftHandleI2c, &controllerStatus);
203 if (FT4222_OK != ft4222Status || I2CM_ERROR(controllerStatus)) {
204 syslog(LOG_ERR, "FT4222_I2CMaster_Write failed address %#02x\n", addr);
205 FT4222_I2CMaster_Reset(handle);
206 return 0;
207 }
208
209 if (bytesWritten != bytesToWrite)
210 syslog(LOG_ERR, "FT4222_I2CMaster_Write wrote %u of %u bytes.\n", bytesWritten, bytesToWrite);
211
212 return bytesWritten;
213 }
214
215
216 // Function detects known I2C I/O expanders and returns the number of GPIO pins on expander
217 static int
mraa_ftdi_ft4222_detect_io_expander()218 mraa_ftdi_ft4222_detect_io_expander()
219 {
220 uint8_t data;
221 if(mraa_ftdi_ft4222_i2c_read_internal(ftHandleI2c, PCA9672_ADDR, &data, 1) == 1) {
222 return PCA9672_PINS;
223 }
224 return 0;
225 }
226
227
228 static mraa_boolean_t
mraa_ftdi_ft4222_is_internal_gpio(int pin)229 mraa_ftdi_ft4222_is_internal_gpio(int pin)
230 {
231 return pin < numFt4222GpioPins;
232 }
233
234
235 static mraa_result_t
ftdi_ft4222_set_internal_gpio_dir(int pin,GPIO_Dir direction)236 ftdi_ft4222_set_internal_gpio_dir(int pin, GPIO_Dir direction)
237 {
238 pinDirection[pin] = direction;
239 if (FT4222_GPIO_Init(ftHandleGpio, pinDirection) != FT4222_OK)
240 return MRAA_ERROR_UNSPECIFIED;
241 else
242 return MRAA_SUCCESS;
243 }
244
245 // Function detects known I2C switches and returns the number of busses.
246 // On startup switch is disabled so default bus will be integrated i2c bus.
247 static int
mraa_ftdi_ft4222_detect_i2c_switch()248 mraa_ftdi_ft4222_detect_i2c_switch()
249 {
250 uint8_t data;
251 if(mraa_ftdi_ft4222_i2c_read_internal(ftHandleI2c, PCA9545_ADDR, &data, 1) == 1) {
252 data = 0;
253 return mraa_ftdi_ft4222_i2c_write_internal(ftHandleI2c, PCA9545_ADDR, &data, 1) == 1 ? PCA9545_BUSSES : 0;
254 }
255 return 0;
256 }
257
258
259 static mraa_result_t
mraa_ftdi_ft4222_i2c_select_bus(int bus)260 mraa_ftdi_ft4222_i2c_select_bus(int bus)
261 {
262 if (bus != currentI2cBus) {
263 uint8_t data;
264 if (bus == 0)
265 data = 0;
266 else
267 data = 1 << (bus-1);
268 if (mraa_ftdi_ft4222_i2c_write_internal(ftHandleI2c, PCA9545_ADDR, &data, 1) == 1)
269 currentI2cBus = bus;
270 else
271 return MRAA_ERROR_UNSPECIFIED;
272 }
273 return MRAA_SUCCESS;
274 }
275
276 static int
mraa_ftdi_ft4222_i2c_context_read(mraa_i2c_context dev,uint8_t * data,int length)277 mraa_ftdi_ft4222_i2c_context_read(mraa_i2c_context dev, uint8_t* data, int length)
278 {
279 if (mraa_ftdi_ft4222_i2c_select_bus(dev->busnum) == MRAA_SUCCESS)
280 return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
281 else
282 return 0;
283 }
284
285 static int
mraa_ftdi_ft4222_i2c_context_write(mraa_i2c_context dev,uint8_t * data,int length)286 mraa_ftdi_ft4222_i2c_context_write(mraa_i2c_context dev, uint8_t* data, int length)
287 {
288 if (mraa_ftdi_ft4222_i2c_select_bus(dev->busnum) == MRAA_SUCCESS)
289 return mraa_ftdi_ft4222_i2c_write_internal(dev->handle, dev->addr, data, length);
290 else
291 return 0;
292 }
293
294
295 static void
mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)296 mraa_ftdi_ft4222_sleep_ms(unsigned long mseconds)
297 {
298 struct timespec sleepTime;
299
300 sleepTime.tv_sec = mseconds / 1000; // Number of seconds
301 sleepTime.tv_nsec = (mseconds % 1000) * 1000000; // Convert fractional seconds to nanoseconds
302
303 // Iterate nanosleep in a loop until the total sleep time is the original
304 // value of the seconds parameter
305 while ((nanosleep(&sleepTime, &sleepTime) != 0) && (errno == EINTR))
306 ;
307 }
308
309 static unsigned int
mraa_ftdi_ft4222_get_tick_count_ms()310 mraa_ftdi_ft4222_get_tick_count_ms()
311 {
312 static unsigned int startTick = 0;
313 unsigned int ticks;
314 struct timeval now;
315 gettimeofday(&now, NULL);
316 ticks = now.tv_sec * 1000;
317 ticks += now.tv_usec / 1000;
318 if (startTick == 0)
319 startTick = ticks;
320 return ticks - startTick;
321 }
322
323
324 /******************* I2C functions *******************/
325
326 static mraa_result_t
mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)327 mraa_ftdi_ft4222_i2c_init_bus_replace(mraa_i2c_context dev)
328 {
329 // Tell the FT4222 to be an I2C Master.
330 FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandleI2c, bus_speed);
331 if (FT4222_OK != ft4222Status) {
332 syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status);
333 return MRAA_ERROR_NO_RESOURCES;
334 }
335
336 // Reset the I2CM registers to a known state.
337 ft4222Status = FT4222_I2CMaster_Reset(ftHandleI2c);
338 if (FT4222_OK != ft4222Status) {
339 syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status);
340 return MRAA_ERROR_NO_RESOURCES;
341 }
342
343 syslog(LOG_NOTICE, "I2C interface enabled GPIO0 and GPIO1 will be unavailable.\n");
344 dev->handle = ftHandleI2c;
345 dev->fh = -1; // We don't use file descriptors
346 dev->funcs = I2C_FUNC_I2C; // Advertise minimal i2c support as per
347 // https://www.kernel.org/doc/Documentation/i2c/functionality
348 return MRAA_SUCCESS;
349 }
350
351
352 static mraa_result_t
mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev,mraa_i2c_mode_t mode)353 mraa_ftdi_ft4222_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode)
354 {
355 switch (mode) {
356 case MRAA_I2C_STD: /**< up to 100Khz */
357 bus_speed = 100;
358 break;
359 MRAA_I2C_FAST: /**< up to 400Khz */
360 bus_speed = 400;
361 break;
362 MRAA_I2C_HIGH: /**< up to 3.4Mhz */
363 bus_speed = 3400;
364 break;
365 }
366 return FT4222_I2CMaster_Init(ftHandleI2c, bus_speed) == FT4222_OK ? MRAA_SUCCESS : MRAA_ERROR_NO_RESOURCES;
367 }
368
369
370 static mraa_result_t
mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev,uint8_t addr)371 mraa_ftdi_ft4222_i2c_address(mraa_i2c_context dev, uint8_t addr)
372 {
373 dev->addr = (int) addr;
374 return MRAA_SUCCESS;
375 }
376
377
378 static int
mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev,uint8_t * data,int length)379 mraa_ftdi_ft4222_i2c_read(mraa_i2c_context dev, uint8_t* data, int length)
380 {
381 return mraa_ftdi_ft4222_i2c_read_internal(dev->handle, dev->addr, data, length);
382 }
383
384 static uint8_t
mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)385 mraa_ftdi_ft4222_i2c_read_byte(mraa_i2c_context dev)
386 {
387 uint8_t data;
388 if (mraa_ftdi_ft4222_i2c_context_read(dev, &data, 1) == 1)
389 return data;
390 else
391 return 0;
392 }
393
394
395 static uint16_t
mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev,uint8_t command)396 mraa_ftdi_ft4222_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
397 {
398 uint8_t buf[2];
399 uint16_t data;
400 if (mraa_ftdi_ft4222_i2c_context_write(dev, &command, 1) != 1)
401 return 0;
402 if (mraa_ftdi_ft4222_i2c_context_read(dev, buf, 2) != 2)
403 return 0;
404 data = *(uint16_t*)buf;
405 return data;
406 }
407
408 static int
mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev,uint8_t command,uint8_t * data,int length)409 mraa_ftdi_ft4222_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* data, int length)
410 {
411 if (mraa_ftdi_ft4222_i2c_context_write(dev, &command, 1) != 1)
412 return 0;
413 return mraa_ftdi_ft4222_i2c_context_read(dev, data, length);
414 }
415
416
417 static mraa_result_t
mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev,const uint8_t * data,int bytesToWrite)418 mraa_ftdi_ft4222_i2c_write(mraa_i2c_context dev, const uint8_t* data, int bytesToWrite)
419 {
420 uint16 bytesWritten = mraa_ftdi_ft4222_i2c_context_write(dev, (uint8_t*)data, bytesToWrite);
421 return bytesToWrite == bytesWritten ? MRAA_SUCCESS : MRAA_ERROR_INVALID_HANDLE;
422 }
423
424
425 static mraa_result_t
mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev,uint8_t data)426 mraa_ftdi_ft4222_i2c_write_byte(mraa_i2c_context dev, uint8_t data)
427 {
428 return mraa_ftdi_ft4222_i2c_write(dev, &data, 1);
429 }
430
431
432 static uint8_t
mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev,uint8_t command)433 mraa_ftdi_ft4222_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
434 {
435 const uint8_t reg_addr = command;
436 uint8_t data;
437 if (mraa_ftdi_ft4222_i2c_write(dev, ®_addr, 1) != MRAA_SUCCESS)
438 return 0;
439 if (mraa_ftdi_ft4222_i2c_read(dev, &data, 1) != 1)
440 return 0;
441 return data;
442 }
443
444 static mraa_result_t
mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev,const uint8_t data,const uint8_t command)445 mraa_ftdi_ft4222_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
446 {
447 uint8_t buf[2];
448 buf[0] = command;
449 buf[1] = data;
450 return mraa_ftdi_ft4222_i2c_write(dev, buf, 2);
451 }
452
453 static mraa_result_t
mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev,const uint16_t data,const uint8_t command)454 mraa_ftdi_ft4222_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
455 {
456 uint8_t buf[3];
457 buf[0] = command;
458 buf[1] = (uint8_t) data;
459 buf[2] = (uint8_t)(data >> 8);
460 return mraa_ftdi_ft4222_i2c_write(dev, buf, 3);
461 }
462
463 static mraa_result_t
mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)464 mraa_ftdi_ft4222_i2c_stop(mraa_i2c_context dev)
465 {
466 return MRAA_SUCCESS;
467 }
468
469 /******************* GPIO functions *******************/
470
471 static mraa_result_t
mraa_ftdi_ft4222_gpio_init_internal_replace(mraa_gpio_context dev,int pin)472 mraa_ftdi_ft4222_gpio_init_internal_replace(mraa_gpio_context dev, int pin)
473 {
474 dev->phy_pin = (pin < numFt4222GpioPins) ? pin : pin - numFt4222GpioPins;
475 if (pin < 2) {
476 syslog(LOG_NOTICE, "Closing I2C interface to enable GPIO%d\n", pin);
477
478 /* Replace with call to SPI init when SPI is fully implemented */
479 FT4222_STATUS ft4222Status = FT4222_SPIMaster_Init(ftHandleSpi, SPI_IO_SINGLE, CLK_DIV_4, CLK_IDLE_HIGH, CLK_LEADING, 0x01);
480 if (FT4222_OK != ft4222Status){
481 syslog(LOG_ERR, "Failed to close I2C interface and start SPI (error %d)!\n", ft4222Status);
482 return MRAA_ERROR_NO_RESOURCES;
483 }
484 }
485 return MRAA_SUCCESS;
486 }
487
488 static mraa_result_t
mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev,mraa_gpio_mode_t mode)489 mraa_ftdi_ft4222_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
490 {
491 return MRAA_SUCCESS;
492 }
493
494 static mraa_result_t
mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev,mraa_gpio_edge_t mode)495 mraa_ftdi_ft4222_gpio_edge_mode_replace(mraa_gpio_context dev, mraa_gpio_edge_t mode)
496 {
497 return MRAA_SUCCESS;
498 }
499
500 static int
mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)501 mraa_ftdi_ft4222_gpio_read_replace(mraa_gpio_context dev)
502 {
503 uint8_t pin = dev->pin;
504 if (mraa_ftdi_ft4222_is_internal_gpio(pin)) {
505 // FTDI GPIO
506 BOOL value;
507 FT4222_STATUS ft4222Status = FT4222_GPIO_Read(ftHandleGpio, dev->phy_pin, &value);
508 if (FT4222_OK != ft4222Status) {
509 syslog(LOG_ERR, "FT4222_GPIO_Read failed (error %d)!\n", ft4222Status);
510 return -1;
511 }
512 return value;
513 }
514 else {
515 // Expander GPIO
516 uint8_t mask = 1 << dev->phy_pin;
517 uint8_t value;
518 if (mraa_ftdi_ft4222_i2c_read_internal(ftHandleI2c, PCA9672_ADDR, &value, 1) != 1)
519 return -1;
520 return (value & mask) == mask;
521 }
522 }
523
524
525 static mraa_result_t
mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev,int write_value)526 mraa_ftdi_ft4222_gpio_write_replace(mraa_gpio_context dev, int write_value)
527 {
528 uint8_t pin = dev->pin;
529 if (mraa_ftdi_ft4222_is_internal_gpio(pin)) {
530 // FTDI GPIO
531 FT4222_STATUS ft4222Status = FT4222_GPIO_Write(ftHandleGpio, dev->phy_pin, write_value);
532 if (FT4222_OK != ft4222Status) {
533 syslog(LOG_ERR, "FT4222_GPIO_Write failed (error %d)!\n", ft4222Status);
534 return MRAA_ERROR_UNSPECIFIED;
535 }
536 }
537 else {
538 // Expander GPIO
539 uint8_t mask = 1 << dev->phy_pin;
540 uint8_t value;
541 if (mraa_ftdi_ft4222_i2c_read_internal(ftHandleI2c, PCA9672_ADDR, &value, 1) != 1)
542 return MRAA_ERROR_UNSPECIFIED;
543 if (write_value == 1)
544 value |= mask;
545 else
546 value &= (~mask);
547 if (mraa_ftdi_ft4222_i2c_write_internal(ftHandleI2c, PCA9672_ADDR, &value, 1) != 1)
548 return MRAA_ERROR_UNSPECIFIED;
549 }
550 return MRAA_SUCCESS;
551 }
552
553 static mraa_result_t
mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev,mraa_gpio_dir_t dir)554 mraa_ftdi_ft4222_gpio_dir_replace(mraa_gpio_context dev, mraa_gpio_dir_t dir)
555 {
556 switch (dir) {
557 case MRAA_GPIO_IN:
558 if (mraa_ftdi_ft4222_is_internal_gpio(dev->pin))
559 return ftdi_ft4222_set_internal_gpio_dir(dev->phy_pin, GPIO_INPUT);
560 else
561 return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
562 case MRAA_GPIO_OUT:
563 if (mraa_ftdi_ft4222_is_internal_gpio(dev->pin))
564 return ftdi_ft4222_set_internal_gpio_dir(dev->phy_pin, GPIO_OUTPUT);
565 else
566 return MRAA_SUCCESS;
567 case MRAA_GPIO_OUT_HIGH:
568 if (mraa_ftdi_ft4222_is_internal_gpio(dev->pin)) {
569 if (ftdi_ft4222_set_internal_gpio_dir(dev->phy_pin, GPIO_OUTPUT) != MRAA_SUCCESS)
570 return MRAA_ERROR_UNSPECIFIED;
571 }
572 return mraa_ftdi_ft4222_gpio_write_replace(dev, 1);
573 case MRAA_GPIO_OUT_LOW:
574 if (mraa_ftdi_ft4222_is_internal_gpio(dev->pin)) {
575 if (ftdi_ft4222_set_internal_gpio_dir(dev->phy_pin, GPIO_OUTPUT) != MRAA_SUCCESS)
576 return MRAA_ERROR_UNSPECIFIED;
577 }
578 return mraa_ftdi_ft4222_gpio_write_replace(dev, 0);
579 default:
580 return MRAA_ERROR_INVALID_PARAMETER;
581 }
582 }
583
584
585 static void*
mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)586 mraa_ftdi_ft4222_gpio_interrupt_handler_replace(mraa_gpio_context dev)
587 {
588 #ifdef USE_FT4222_GPIO_TRIGGER
589 // FIXME: Use big buffer; shouldn't be more than this many events to read
590 GPIO_Trigger event_buf[256];
591 int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
592 while (1) {
593 uint16 num_events = 0;
594 FT4222_STATUS status = FT4222_GPIO_GetTriggerStatus(ftHandleGpio, GPIO_PORT_IO_STATUS, &num_events);
595 if (status != FT4222_OK)
596 printf("FT4222_GPIO_GetTriggerStatus failed with code %d\n", status);
597 printf("%u: FT4222_GPIO_GetTriggerStatus Events = %d\n", mraa_ftdi_ft4222_get_tick_count_ms(), num_events);
598 if (num_events > 0) {
599 int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
600 uint16 num_events_read;
601 FT4222_GPIO_ReadTriggerQueue(ftHandleGpio, GPIO_PORT_IO_STATUS, event_buf, num_events, &num_events_read);
602 // printf("%u: FT4222_GPIO_ReadTriggerQueue Events= %d\n", mraa_ftdi_ft4222_get_tick_count_ms(), num_events_read);
603 printf("%u: level = %d\n", mraa_ftdi_ft4222_get_tick_count_ms(), level);
604 if (level != prev_level) {
605 dev->isr(dev->isr_args);
606 prev_level = level;
607 }
608
609 }
610 mraa_ftdi_ft4222_sleep_ms(20);
611 // int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
612 // printf("level = %d\n", level);
613 }
614 #else
615 int prev_level = mraa_ftdi_ft4222_gpio_read_replace(dev);
616 while (1) {
617 int level = mraa_ftdi_ft4222_gpio_read_replace(dev);
618 // MRAA_GPIO_EDGE_BOTH
619 if (level != prev_level) {
620 dev->isr(dev->isr_args);
621 prev_level = level;
622 }
623 mraa_ftdi_ft4222_sleep_ms(100);
624 }
625 #endif
626 return NULL;
627 }
628
629 static void
mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t * func_table)630 mraa_ftdi_ft4222_populate_i2c_func_table(mraa_adv_func_t* func_table)
631 {
632 func_table->i2c_init_bus_replace = &mraa_ftdi_ft4222_i2c_init_bus_replace;
633 func_table->i2c_set_frequency_replace = &mraa_ftdi_ft4222_i2c_frequency;
634 func_table->i2c_address_replace = &mraa_ftdi_ft4222_i2c_address;
635 func_table->i2c_read_replace = &mraa_ftdi_ft4222_i2c_read;
636 func_table->i2c_read_byte_replace = &mraa_ftdi_ft4222_i2c_read_byte;
637 func_table->i2c_read_byte_data_replace = &mraa_ftdi_ft4222_i2c_read_byte_data;
638 func_table->i2c_read_word_data_replace = &mraa_ftdi_ft4222_i2c_read_word_data;
639 func_table->i2c_read_bytes_data_replace = &mraa_ftdi_ft4222_i2c_read_bytes_data;
640 func_table->i2c_write_replace = &mraa_ftdi_ft4222_i2c_write;
641 func_table->i2c_write_byte_replace = &mraa_ftdi_ft4222_i2c_write_byte;
642 func_table->i2c_write_byte_data_replace = &mraa_ftdi_ft4222_i2c_write_byte_data;
643 func_table->i2c_write_word_data_replace = &mraa_ftdi_ft4222_i2c_write_word_data;
644 func_table->i2c_stop_replace = &mraa_ftdi_ft4222_i2c_stop;
645 }
646
647 static void
mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t * func_table)648 mraa_ftdi_ft4222_populate_gpio_func_table(mraa_adv_func_t* func_table)
649 {
650 func_table->gpio_init_internal_replace = &mraa_ftdi_ft4222_gpio_init_internal_replace;
651 func_table->gpio_mode_replace = &mraa_ftdi_ft4222_gpio_mode_replace;
652 func_table->gpio_edge_mode_replace = &mraa_ftdi_ft4222_gpio_edge_mode_replace;
653 func_table->gpio_dir_replace = &mraa_ftdi_ft4222_gpio_dir_replace;
654 func_table->gpio_read_replace = &mraa_ftdi_ft4222_gpio_read_replace;
655 func_table->gpio_write_replace = &mraa_ftdi_ft4222_gpio_write_replace;
656 func_table->gpio_interrupt_handler_replace = &mraa_ftdi_ft4222_gpio_interrupt_handler_replace;
657 }
658
659
660 mraa_board_t*
mraa_ftdi_ft4222()661 mraa_ftdi_ft4222()
662 {
663 mraa_board_t* sub_plat = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
664 if (sub_plat == NULL)
665 return NULL;
666 numI2cGpioExpanderPins = mraa_ftdi_ft4222_detect_io_expander();
667 int pinIndex = 0;
668 int numUsbGpio = numFt4222GpioPins + numI2cGpioExpanderPins;
669 int numI2cBusses = 1 + mraa_ftdi_ft4222_detect_i2c_switch();
670 int numUsbPins = numUsbGpio + 2 * (numI2cBusses-1); // Add SDA and SCL for each i2c switch bus
671 mraa_pincapabilities_t pinCapsI2c = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 1, 0, 0 };
672 mraa_pincapabilities_t pinCapsI2cGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 1, 0, 0 };
673 mraa_pincapabilities_t pinCapsGpio = (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 };
674
675 sub_plat->platform_name = PLATFORM_NAME;
676 sub_plat->phy_pin_count = numUsbPins;
677 sub_plat->gpio_count = numUsbGpio;
678 mraa_pininfo_t* pins = (mraa_pininfo_t*) calloc(numUsbPins,sizeof(mraa_pininfo_t));
679 if (pins == NULL) {
680 return NULL;
681 }
682 sub_plat->pins = pins;
683
684 int bus = 0;
685 sub_plat->i2c_bus_count = numI2cBusses;
686 sub_plat->def_i2c_bus = bus;
687 sub_plat->i2c_bus[bus].bus_id = bus;
688
689 // I2c pins (these are virtual, entries are required to configure i2c layer)
690 // We currently assume that GPIO 0/1 are reserved for i2c operation
691 strncpy(sub_plat->pins[pinIndex].name, "IGPIO0/SCL0", MRAA_PIN_NAME_SIZE);
692 sub_plat->pins[pinIndex].capabilites = pinCapsI2cGpio;
693 sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
694 sub_plat->pins[pinIndex].gpio.mux_total = 0;
695 sub_plat->pins[pinIndex].i2c.mux_total = 0;
696 sub_plat->i2c_bus[bus].scl = pinIndex;
697 pinIndex++;
698 strncpy(sub_plat->pins[pinIndex].name, "IGPIO1/SDA0", MRAA_PIN_NAME_SIZE);
699 sub_plat->pins[pinIndex].capabilites = pinCapsI2cGpio;
700 sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
701 sub_plat->pins[pinIndex].gpio.mux_total = 0;
702 sub_plat->pins[pinIndex].i2c.mux_total = 0;
703 sub_plat->i2c_bus[bus].sda = pinIndex;
704 pinIndex++;
705
706 // FTDI4222 gpio
707 strncpy(sub_plat->pins[pinIndex].name, "INT-GPIO2", MRAA_PIN_NAME_SIZE);
708 sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
709 sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
710 sub_plat->pins[pinIndex].gpio.mux_total = 0;
711 pinIndex++;
712 strncpy(sub_plat->pins[pinIndex].name, "INT-GPIO3", MRAA_PIN_NAME_SIZE);
713 sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
714 sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
715 sub_plat->pins[pinIndex].gpio.mux_total = 0;
716 pinIndex++;
717
718 // Virtual gpio pins on i2c I/O expander.
719 int i;
720 for (i = 0; i < numI2cGpioExpanderPins; ++i) {
721 snprintf(sub_plat->pins[pinIndex].name, MRAA_PIN_NAME_SIZE, "EXP-GPIO%d", i);
722 sub_plat->pins[pinIndex].capabilites = pinCapsGpio;
723 sub_plat->pins[pinIndex].gpio.pinmap = pinIndex;
724 sub_plat->pins[pinIndex].gpio.mux_total = 0;
725 pinIndex++;
726 }
727
728 // Now add any extra i2c busses behind i2c switch
729 for (bus = 1; bus < numI2cBusses; ++bus) {
730 sub_plat->i2c_bus[bus].bus_id = bus;
731 sub_plat->pins[pinIndex].i2c.mux_total = 0;
732 snprintf(sub_plat->pins[pinIndex].name, MRAA_PIN_NAME_SIZE, "SDA%d", bus);
733 sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
734 sub_plat->i2c_bus[bus].sda = pinIndex;
735 pinIndex++;
736 snprintf(sub_plat->pins[pinIndex].name, MRAA_PIN_NAME_SIZE, "SCL%d", bus);
737 sub_plat->pins[pinIndex].capabilites = pinCapsI2c;
738 sub_plat->pins[pinIndex].i2c.mux_total = 0;
739 sub_plat->i2c_bus[bus].scl = pinIndex;
740 pinIndex++;
741 }
742
743
744 // Set override functions
745 mraa_adv_func_t* func_table = (mraa_adv_func_t*) calloc(1, sizeof(mraa_adv_func_t));
746 if (func_table == NULL) {
747 return NULL;
748 }
749 mraa_ftdi_ft4222_populate_i2c_func_table(func_table);
750 mraa_ftdi_ft4222_populate_gpio_func_table(func_table);
751
752 sub_plat->adv_func = func_table;
753 return sub_plat;
754 }
755