I2C communication using FTDI FT4232H
The FT4232H from FTDI is a versatile chip that can act as 4 UART ports, 4 serial communication ports (I2C, SPI or JTAG) or up to 32 digital I/O lines.
The code described here is a code to use port 0 of this chip as I2C controller.
FTDI does not supply code for I2C communication, all they give is an application note for connecting I2C serial EEPROM using this chip and their own D2xx library which is not open source.
This code uses libftdi which is a free implementation of all FTDI functions for FT4232H or FT2232 chips.
libftdi is available in most Linux distributions. This library can be used on MS-Windows too, so theoreticaly one could compile this code for MS-Windows system. I do not have access to MS-Windows system so I could not check it. If someone compiles the code for MS-Windows, I would be happy to receive it and include a link to it from this page (with reference to the author of course).
The code described here contains two command line utilities:
- i2csend - send bytes over i2c bus
- i2cget - receive bytes over i2c bus
Examples:
Sending bytes 0x07 0xaa 0x55 to address 0x20:
# i2csend 0x20 0x07 0xaa 0x55
Receiving to bytes from address 0x20:
# i2cget 0x20 2
Note that both commands must be run as root, this is a requirement of libftdi.
Compiling the code:
Before compiling the code, install libftdi development files. On Debian based Linux distributions (Debian, Ubuntu, Linux mint), this is done by the command: sudo apt-get install libftdi-dev
For more information on compiling and using the code, refer to the README file in the code.
This code is distributed under GPL version 3.0 and above. The license itself is given in the file COPYING distributed with the source.
For downloading the code: i2cftdi.tar.gz
function RecursiveDelete($num) { global $commentstbl; $query = "SELECT num FROM $commentstbl WHERE ancestor='$num'"; $result = DoQuery($query, __LINE__); if($result->num_rows == 0) return; /* end of recursive call */ while($line = $result->fetch_assoc()) { $n = $line['num']; $query = "DELETE FROM comments WHERE num=$n"; $newresult = DoQuery($query, __LINE__); RecursiveDelete($n); } } function GetTimeStr($hour, $min, $sec, $month, $day, $year) { global $timeoffset; $time = mktime($hour, $min, $sec, $month, $day, $year, 0); $time += ($timeoffset * 3600); /* add time offset in seconds */ $str = date("d/m/Y H:i:s", $time); return $str; } /* | AddLinks: | Replace URL's in string with links */ function AddLinks($string) { $string = preg_replace("/(^|[^=\"\/])\b((\w+:\/\/|www\.)[^\s<]+)". "((\W+|\b)([\s<]|$))/i", "$1$2$4", $string); return preg_replace("/href=\"www/i", "href=\"http://www", $string); // $txt = preg_replace( "/(?\s]+)/i", "\\0", $txt ); // " } /* | Replace special codes in messages */ function SpecialCodes($str) { $str = stripslashes($str); $str = str_replace('<', '<', $str); $str = str_replace('|code|', '', $str);
$str = str_replace('|CODE|', ' ', $str);
$str = str_replace('|ECODE|', '
", $str); $str = AddLinks($str); return $str; } /* | DisplayContents: | Show a string of section body, parsing dot commands | currently only one command is supported | .C that will start or stop code section */ function DisplayContents($contents) { $incode = 0; $contents = SpecialCodes($contents); $a = explode('
', $contents); foreach($a as $val) { if($val[0] == '.') { /* this is a command */ if($val[1] == 'C') { if(!$incode) { print "
\n"; } } if($incode) { while($incode) { print "