<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>xGoat &#187; linux</title>
	<atom:link href="http://xgoat.com/wp/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://xgoat.com/wp</link>
	<description>Robert Spanton's musings.</description>
	<lastBuildDate>Sat, 29 May 2010 12:45:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>FET430 Firmware Already Fixed</title>
		<link>http://xgoat.com/wp/2009/03/29/fet430-firmware-already-fixed/</link>
		<comments>http://xgoat.com/wp/2009/03/29/fet430-firmware-already-fixed/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 03:28:44 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[uif]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/?p=660</guid>
		<description><![CDATA[I just had a very quick look into what could be done to fix the FET430 UIF firmware loading issue I wrote about before. The issue looks like it was fixed in this commit. That&#8217;s in Linux 2.6.29, which was released 6 days ago. No work for me to do there then&#8230;]]></description>
			<content:encoded><![CDATA[<p>I just had a very quick look into what could be done to fix the FET430 UIF firmware loading issue I <a href="http://xgoat.com/wp/2008/11/26/uif-firmware-breakage/" >wrote about before</a>.  The issue <i>looks</i> like it was fixed <a href="http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=05a3d9050ad7bb791b9ba306165ea98e6d9e9da2" onclick="javascript:urchinTracker ('/outbound/article/git.kernel.org');">in this commit</a>.  That&#8217;s in Linux 2.6.29, which was released 6 days ago.</p>
<p>No work for me to do there then&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2009/03/29/fet430-firmware-already-fixed/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Location sensitive ssh &#8220;tunnelling&#8221;</title>
		<link>http://xgoat.com/wp/2008/06/11/location-sensitive-ssh-tunnelling/</link>
		<comments>http://xgoat.com/wp/2008/06/11/location-sensitive-ssh-tunnelling/#comments</comments>
		<pubDate>Wed, 11 Jun 2008 08:38:57 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/?p=156</guid>
		<description><![CDATA[I wanted to always be able to shell to a machine within the Uni network. The Uni network has a firewall that stops incoming requests to most machines. There&#8217;s a machine that all undergrads can shell to, which I normally use netcat combined with the ssh ProxyCommand setting. However, it&#8217;s a little silly to divert [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to always be able to shell to a machine within the Uni network.  The Uni network has a firewall that stops incoming requests to most machines.  There&#8217;s a machine that all undergrads can shell to, which I normally use netcat combined with the ssh ProxyCommand setting.  However, it&#8217;s a little silly to divert all traffic through another machine when I&#8217;m in the network.  So, meet the new script I use in the ProxyCommand:
</p>
<pre>
#!/bin/bash
HOST=$1

got=`ifconfig eth1 | egrep -o "inet addr:152.78.[0-9]{1,3}\\.[0-9]{1,3}"`
got+=`ifconfig eth0 | egrep -o "inet addr:152.78.[0-9]{1,3}\\.[0-9]{1,3}"`

if [[ "$got" == "" ]]
then
    ssh uglogin.ecs.soton.ac.uk nc $HOST 22
else
    nc $HOST 22
fi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/06/11/location-sensitive-ssh-tunnelling/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>UIF Fixing</title>
		<link>http://xgoat.com/wp/2008/03/24/uif-fixing/</link>
		<comments>http://xgoat.com/wp/2008/03/24/uif-fixing/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 23:25:27 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[msp430]]></category>
		<category><![CDATA[mspgcc]]></category>
		<category><![CDATA[uif]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/03/24/uif-fixing/</guid>
		<description><![CDATA[I spent today working on a bug in the driver for the TI UIF MSP430 programmer. It stopped initialising properly in 2.6.24, but it worked in 2.6.23. I did a git-bisect between those versions to find the commit that induced the fault. I narrowed the search a bit by telling git-bisect to work on commits [...]]]></description>
			<content:encoded><![CDATA[<p>I spent today working on a bug in the driver for the TI UIF MSP430 programmer.  It stopped initialising properly in 2.6.24, but it worked in 2.6.23.  I did a git-bisect between those versions to find the commit that induced the fault.  I narrowed the search a bit by telling git-bisect to work on commits only in drivers/usb/, as I hypothesized that the bug was induced somewhere in there.</p>
<p>About 10 builds and 20 reboots later, I found the commit in which the problem was happening, and then read some stuff about USB etc (the LWN <a href="http://lwn.net/Kernel/LDD3/" onclick="javascript:urchinTracker ('/outbound/article/lwn.net');">device drivers</a> book proved invaluable yet again) and subsequently generated <a href='http://xgoat.com/wp/wp-content/0001-usb-serial-ti_usb_3410_5052-correct-tusb3410-endp.patch'>a patch</a>.  I&#8217;ve sent it to (what I think are) the right places.</p>
<p>If you can&#8217;t wait for the next kernel release (if it passes review&#8230;), then you can rebuild the ti_usb_3410_5052 module by downloading this <a href="http://xgoat.com/patches/msp430-debugger/uif-fix-0.01.tar.gz" >tarball</a>, untarring it and then running &#8220;make&#8221; in the resulting directory, and then &#8220;make install&#8221; as root.  You will need enough of your kernel&#8217;s sources hanging around to do this.  In Fedora, these are provided in the kernel-devel package.</p>
<p><b>Update (5th April &#8217;08):</b> The patch has made its way into Linus&#8217;s tree, so I think it&#8217;ll be in 2.6.25.</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/03/24/uif-fixing/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>F9 Alpha Kernel oops reporting</title>
		<link>http://xgoat.com/wp/2008/03/01/f9-alpha-kernel-oops-reporting/</link>
		<comments>http://xgoat.com/wp/2008/03/01/f9-alpha-kernel-oops-reporting/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 23:15:53 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/03/01/f9-alpha-kernel-oops-reporting/</guid>
		<description><![CDATA[I downloaded the Fedora Alpha 9 live CD ISO and ran it on my desktop. It&#8217;s got PolicyKit and PackageKit, which are pretty cool. Just after I&#8217;d logged in, I was greeted with this box: I clicked &#8220;Yes&#8221; (I&#8217;d click &#8220;always&#8221;, but this was on a Live CD so it wouldn&#8217;t really have meaning), and [...]]]></description>
			<content:encoded><![CDATA[<p>I downloaded the <a href="http://fedoraproject.org/en/get-prerelease" onclick="javascript:urchinTracker ('/outbound/article/fedoraproject.org');">Fedora Alpha 9 live CD</a> ISO and ran it on my desktop.  It&#8217;s got PolicyKit and PackageKit, which are pretty cool.  Just after I&#8217;d logged in, I was greeted with this box:</p>
<p><center><a href='http://xgoat.com/wp/wp-content/oops.png' title='Oops'><img src='http://xgoat.com/wp/wp-content/oops.png' alt='Oops' /></a></center></p>
<p>I clicked &#8220;Yes&#8221; (I&#8217;d click &#8220;always&#8221;, but this was on a Live CD so it wouldn&#8217;t really have meaning), and then it popped up with:</p>
<p><center><a href='http://xgoat.com/wp/wp-content/oops-1.png' title='Oops Sent'><img src='http://xgoat.com/wp/wp-content/oops-1.png' alt='Oops Sent' /></a></center></p>
<p>Pretty cool.  It didn&#8217;t ask me for any of my details, which I think is cool.  Going to <a href="http://kerneloops.org/" onclick="javascript:urchinTracker ('/outbound/article/kerneloops.org');">kerneloops.org</a> reveals that it exists to track which crash signatures occur the most.</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/03/01/f9-alpha-kernel-oops-reporting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Audio conversion in GNOME</title>
		<link>http://xgoat.com/wp/2008/02/14/graphical-audio-conversion-in-gnome/</link>
		<comments>http://xgoat.com/wp/2008/02/14/graphical-audio-conversion-in-gnome/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 23:34:44 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[GNOME]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/02/14/graphical-audio-conversion-in-gnome/</guid>
		<description><![CDATA[I walked to the shops earlier. I took my mp3 player with me, which I haven&#8217;t used in a while. I keep all local copies of my (new) music in Ogg Vorbis or FLAC, which means that when I transfer to my mp3 player, I have to convert the tracks. Previously, I&#8217;ve bashed together a [...]]]></description>
			<content:encoded><![CDATA[<p>I walked to the shops earlier.  I took my mp3 player with me, which I haven&#8217;t used in a while.  I keep all local copies of my (new) music in <a href="http://www.vorbis.com/" onclick="javascript:urchinTracker ('/outbound/article/www.vorbis.com');">Ogg Vorbis</a> or <a href="http://flac.sourceforge.net/" onclick="javascript:urchinTracker ('/outbound/article/flac.sourceforge.net');">FLAC</a>, which means that when I transfer to my mp3 player, I have to convert the tracks.  Previously, I&#8217;ve bashed together a shell script that does the conversion, but today was different.  Today I got graphical.</p>
<p>I had a quick search for audio converters for GNOME, and found <a href="http://www.juripakaste.fi/audioformat/" onclick="javascript:urchinTracker ('/outbound/article/www.juripakaste.fi');">AudioFormat</a> and <a href="http://www.diffingo.com/content/view/13/47/lang,en/" onclick="javascript:urchinTracker ('/outbound/article/www.diffingo.com');">audio-convert-mod</a>.  audio-convert-mod is in the <a href="http://www.fedoraproject.org/" onclick="javascript:urchinTracker ('/outbound/article/www.fedoraproject.org');">Fedora</a> repos, so I used that.  It was surprisingly enjoyable.  It automatically detected the encoders and decoders that were available on my system:</p>
<div style="text-align:center"><a href='http://xgoat.com/wp/wp-content/screenshot-features.png' title='audio-convert-mod: Installed Codecs'><img src='http://xgoat.com/wp/wp-content/screenshot-features.png' alt='audio-convert-mod: Installed Codecs' /></a></div>
</p>
<p>The program takes the form of a wizard, which first asks for the files to convert, then the format to convert to, and the destination directory.  Then it converts them.  That&#8217;s it, no hassle.</p>
<p><i>R</i>.</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/02/14/graphical-audio-conversion-in-gnome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sendkey: Automated ssh key setup</title>
		<link>http://xgoat.com/wp/2008/02/13/sendkey-automated-ssh-key-setup/</link>
		<comments>http://xgoat.com/wp/2008/02/13/sendkey-automated-ssh-key-setup/#comments</comments>
		<pubDate>Wed, 13 Feb 2008 22:38:51 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/02/13/sendkey-automated-ssh-key-setup/</guid>
		<description><![CDATA[I sent my &#8220;sendkey&#8221; script to Ivor. This script automates the injection of one&#8217;s ssh public key into a remote host&#8217;s authorized_keys file (to allow password-less login). It didn&#8217;t work very well when Ivor ran it. I&#8217;ve now updated the script: #!/bin/bash KEY=`cat ~/.ssh/id_rsa.pub` ssh $1 bash &#60;&#60;EOF mkdir -p ~/.ssh chmod u=rwx,g=,o= ~/.ssh echo [...]]]></description>
			<content:encoded><![CDATA[<p>I sent my &#8220;sendkey&#8221; script to Ivor.  This script automates the injection of one&#8217;s ssh public key into a remote host&#8217;s authorized_keys file (to allow password-less login).  It didn&#8217;t work very well when Ivor ran it.  I&#8217;ve now updated the script:</p>
<pre>
#!/bin/bash                                                                     

KEY=`cat ~/.ssh/id_rsa.pub`

ssh $1 bash &lt;&lt;EOF
mkdir -p ~/.ssh
chmod u=rwx,g=,o= ~/.ssh
echo $KEY &gt;&gt; ~/.ssh/authorized_keys
chmod u=rw,g=,o= ~/.ssh/authorized_keys
EOF
</pre>
</p>
<p>It can be run like this:</p>
<pre>
% ./sendkey remoteusername@remotehost
</pre>
</p>
<p><b>15/02/2007 Update:</b> I fixed the script so that it made the .ssh directory first&#8230; kind of important.</p>
<p><b>29/02/2007 Update:</b> <a href="http://users.ecs.soton.ac.uk/kpz/" onclick="javascript:urchinTracker ('/outbound/article/users.ecs.soton.ac.uk');">Klaus</a> pointed out that it might be useful to write how to generate one&#8217;s ssh keys:</p>
<pre>
/usr/bin/ssh-keygen -t rsa -f ~/.ssh/id_rsa -N ''
</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/02/13/sendkey-automated-ssh-key-setup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I2C Device udev rule</title>
		<link>http://xgoat.com/wp/2008/01/29/i2c-device-udev-rule/</link>
		<comments>http://xgoat.com/wp/2008/01/29/i2c-device-udev-rule/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 00:24:30 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[i2c]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[udev]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/01/29/i2c-device-udev-rule/</guid>
		<description><![CDATA[I just wrote a udev rule that would make any userspace i2c devices be owned by the i2c group: KERNEL=="i2c-[0-9]*", GROUP="i2c" (You can stick that in a file in /etc/udev/rules.d if you&#8217;re using Fedora).]]></description>
			<content:encoded><![CDATA[<p>I just wrote a udev rule that would make any userspace i2c devices be owned by the i2c group:</p>
<pre>
KERNEL=="i2c-[0-9]*", GROUP="i2c"
</pre>
<p>(You can stick that in a file in /etc/udev/rules.d if you&#8217;re using Fedora).</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/01/29/i2c-device-udev-rule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A file IO monitoring utility: iomon</title>
		<link>http://xgoat.com/wp/2008/01/21/a-file-io-monitoring-utility-iomon/</link>
		<comments>http://xgoat.com/wp/2008/01/21/a-file-io-monitoring-utility-iomon/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 20:06:42 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[iomon]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/2008/01/21/a-file-io-monitoring-utility-iomon/</guid>
		<description><![CDATA[Recently I needed to log the calls to read() and write() calls that a program made, including the data that went through them. I hacked together a small program, called &#8220;iomon&#8221;. It runs on Linux (or at least on my Fedora 8 install anyway). What makes it exciting is that I can use it without [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I needed to log the calls to read() and write() calls that a program made, including the data that went through them.  I hacked together a small program, called &#8220;iomon&#8221;.  It runs on Linux (or at least on my Fedora 8 install anyway).  What makes it exciting is that I can use it without modifying the program I&#8217;m monitoring.</p>
<p>It uses features of the dynamic linker (the thing that&#8217;s resposible for loading the shared libraries that a program needs during execution &#8211; see <span class="mono">man ld.so</span>) to <b>interpose</b> a monitoring function between a call to a library function and the actual library function.  When read(), write() or open() are called, an event is logged.</p>
<p>You can get iomon by checking it out like so:</p>
<pre>
git clone http://xgoat.com/proj/fiu/iomon/iomon.git/
</pre>
<p>You can build it by just running &#8220;make&#8221;:</p>
<pre>
% cd iomon
% make
</pre>
<p>You might need to install the glib 2.0 headers (in Fedora, they&#8217;re in the glib2-devel package).
</p>
<h3>Example Usage</h3>
<p>I thought it might be useful to demonstrate how to use it with an example.  After building iomon, build the following C program (also available <a href="http://xgoat.com/proj/fiu/iomon/example/1/test.c" >here</a>) using gcc:</p>
<pre>
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;

int main( int argc, char** argv )
{
        int f, g;

        umask( 000 );
        f = open("test-file1", O_RDWR | O_CREAT);
        g = open("test-file2", O_RDWR | O_CREAT);

        write(f, "test", 4);
        write(g, "second file", 11);

        return 0;
}
</pre>
<p>This is the program that we&#8217;re going to monitor.  As you can probably see, the program just opens a file called &#8220;test-file1&#8243; and another called &#8220;test-file2&#8243;.  It then writes &#8220;test&#8221; and &#8220;second file&#8221; to the first and second file respectively.  Not really a very useful program, but it will suffice for this demo.
</p>
<p>The next thing to do is set the LD_PRELOAD environment variable to contain iomon.so.  The dynamic linker needs to be able to find the shared object file, and so the easiest thing to do is to put the full path into LD_PRELOAD:</p>
<pre>
% cd iomon
% export LD_PRELOAD=`pwd`/iomon.so
</pre>
<p>Now every time that you run a program in this shell, iomon.so will be loaded first.  At the moment iomon will default to dumping all the read, write and open calls to standard output in raw format.  I suggest that you don&#8217;t run anything in this mode&#8230;  iomon is configured through the IOMON environment variable.  This takes a list of arguments just like any command line program:</p>
<pre>
% IOMON="--help" cat /dev/null
Usage:
  iomon [OPTION...] 

iomon

Help Options:
  -?, --help         Show help options

Application Options:
  -f, --file         File to monitor access to.
  -l, --log          Log file
  -t, --text-log     Log in text, instead of binary
  --times            Include times in the log
</pre>
<p>There&#8217;s some helpful information on how to use it.  I&#8217;ll explain the arguments in a little more detail:</p>
<ul>
<li><b class="mono">-f FILE, &#45;&#45;file FILE</b><br/> Instead of logging all file access, just log access to FILE.  iomon will monitor calls to open() to get the file descriptor assigned to FILE, and will only log calls to read() and write() that use that file descriptor.  At the moment, iomon only supports one file descriptor per file (patches welcome!).</li>
<li><b class="mono">-l FILE, &#45;&#45;log FILE</b><br/> Send log output to FILE, rather than standard output.</li>
<li><b class="mono">-t &#45;&#45;text-log</b><br/> This makes the log human-readable.  All data is converted into readable hex strings before writing to the log file.</li>
<li><b class="mono">&#45;&#45;times</b><br/> Prepend every log event with a timestamp.  The timestamps are relative to the first interposed function call.  <i>Only implemented for text-based output (&#45;&#45;text-log)</i></li>
</ul>
<p>Right!  Now we can run the test program:</p>
<pre>
% IOMON="-t --times -f test-file2" ./test
0.000016 open: 74 65 73 74 2D 66 69 6C 65 32
0.000138 write: 73 65 63 6F 6E 64 20 66 69 6C 65
</pre>
<p>And there we see the call to open for test-file2, followed by the filename in hex.  Then there&#8217;s the data sent to it through write() also in hex.  The logged call to open() happened 16&mu;s after the first call to open(), and the write() call we were interested in happened 122&mu;s later.
</p>
<p>Hopefully I&#8217;ll be writing about why I needed this utility later.</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2008/01/21/a-file-io-monitoring-utility-iomon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using I2C from userspace in Linux</title>
		<link>http://xgoat.com/wp/2007/11/11/using-i2c-from-userspace-in-linux/</link>
		<comments>http://xgoat.com/wp/2007/11/11/using-i2c-from-userspace-in-linux/#comments</comments>
		<pubDate>Sun, 11 Nov 2007 02:55:48 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[i2c]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/?p=98</guid>
		<description><![CDATA[I&#8217;ve been using various I2C things in Linux for the past year and a bit, and I&#8217;ve learnt a few things about it from that. Here I hope to collate some of this information. The Basics Linux has an i2c subsystem. This provides methods for interacting with I2C clients connected to the same bus as [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using various I<sup>2</sup>C things in Linux for the past year and a bit, and I&#8217;ve learnt a few things about it from that.  Here I hope to collate some of this information.</p>
<p><span id="more-98"></span></p>
<h1>The Basics</h1>
<p>
Linux has an i2c subsystem.  This provides methods for interacting with I<sup>2</sup>C clients connected to the same bus as the machine running Linux.  As far as I know, Linux can only be a master at the moment.  If you&#8217;ve got an I<sup>2</sup>C client that needs to interact with your Linux system then you&#8217;ve got two options.  The first is to write a driver as a Linux kernel module.  This is (surprisingly) straightforward but isn&#8217;t what I&#8217;m going to focus on here.  The second method is to use the &#8220;i2c-dev&#8221; kernel module.  This lets you interact with your devices from userspace and is what I&#8217;ll be covering here.  If you want to write a kernel driver, then there are good sources of information for this lurking around the <a href="http://www.linuxjournal.com/article/7136" onclick="javascript:urchinTracker ('/outbound/article/www.linuxjournal.com');">place</a> &#8211; also have a look in Documentation/i2c/writing-clients in the Linux source.</p>
<h2>Preparing  your system</h2>
<p>
The first thing to do is to make sure that the kernel that you&#8217;re going to be running this program under has the &#8220;I2C device interface&#8221; driver either compiled in or as a module.  The Linux build config option for this is CONFIG_I2C_CHARDEV.  The second thing that you need to make sure you have is the i2c-dev.h header file from the <a href="http://www.lm-sensors.org/" onclick="javascript:urchinTracker ('/outbound/article/www.lm-sensors.org');">lm-sensors</a> project.  For some reason, it took me a whlie to realise that this header file needed to come from there.  Download their tarball and find the header somewhere in there.  This header file contains some inline functions that wrap around the ioctls of the /dev/i2c-N device files that the i2c-dev kernel driver creates.
</p>
<p>
When one has both the i2c-dev module and the kernel module for the I2C adapter loaded, a device file called /dev/i2c-0 (or /dev/i2c-1, /dev/i2c-2 etc.) will be created.  It&#8217;s important to note that it will only be created if you&#8217;re running udev.  You might need to run:</p>
<pre>
% mknod /dev/i2c-0 c 89 0
</pre>
<p>Which will create the device file.
</p>
<h2>Opening the device</h2>
<p>In your program, you&#8217;ll need to open the device file for the i2c adapter:</p>
<pre>
        int fd;
        fd = open( "/dev/i2c-0", O_RDWR );
</pre>
<p>After checking for opening errors, the next stage is to set the address of the I2C slave that we want to communicate with:</p>
<pre>
#define ADDRESS 0x38

...

        if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 )
        {
                fprintf( stderr, "Failed to set slave address: %m\n" );
                return 2;
        }
</pre>
<p>To get that I2C_SLAVE constant you'll need to include the i2c-dev.h file I wrote about above.
</p>
<h2>Operations</h2>
<p>
So now that we've set the address, then only thing that remains is to actually interact with the device.  There are two ways of doing this:</p>
<ol>
<li>Using read() and write().  Every time you do one of these an entire I2C transaction takes place (i.e. start bit, address, data, stop).</li>
<li>Using the wrapper functions that i2c-dev.h provides.</li>
</ol>
<p>The read() and write() method is fairly straightforward so I won't go over it here.  i2c-dev.h provides many functions to do various i2c transactions.  I'll go over them in a moment - first I'll provide an example of how to write a single byte to the I2C client:</p>
<pre>
    if( i2c_smbus_write_byte( fd, 0xAA ) < 0 )
        fprintf( stderr, "Failed to write 0xAA to I2C device: %m\n" );
</pre>
<p>This example writes the byte 0xAA to whatever address was configured using the I2C_SLAVE ioctl.  All of the functions in i2c-dev.h will return a number less than 0 upon failure.  errno will get set accordingly.
</p>
<h2>SMBus functions</h2>
<p>SMBus is a protocol that defines a set of ways for using an I<sup>2</sup>C bus.  i2c-dev.h provides a function for each of these operations.  The best place to read about these is in the SMBus <a href="http://www.smbus.org/specs/smbus20.pdf" onclick="javascript:urchinTracker ('/outbound/article/www.smbus.org');">spec</a> in section 5.5 "Bus Protocols" - it has nice pictures.</p>
<ul>
<li><b>i2c_smbus_write_quick</b>( int file, __u8 value)</li>
<li><b>i2c_smbus_read_byte</b>(int file)</li>
<li><b>i2c_smbus_write_byte</b>(int file, __u8 value)</li>
<li><b>i2c_smbus_read_byte_data</b>(int file, __u8 command)</li>
<li><b>i2c_smbus_write_byte_data</b>(int file, __u8 command,  __u8 value)</li>
<li><b>i2c_smbus_read_word_data</b>(int file, __u8 command)</li>
<li><b>i2c_smbus_write_word_data</b>(int file, __u8 command, __u16 value)</li>
<li><b>i2c_smbus_process_call</b>(int file, __u8 command, __u16 value)</li>
<li><b>i2c_smbus_read_block_data</b>(int file, __u8 command, __u8 *values)</li>
<li><b>i2c_smbus_write_block_data</b>(int file, __u8 command, __u8 length, __u8 *values)</li>
<li><b>i2c_smbus_read_i2c_block_data</b>(int file, __u8 command, __u8 *values)</li>
<li><b>i2c_smbus_write_i2c_block_data</b>(int file, __u8 command, __u8 length, __u8 *values)</li>
<li><b>i2c_smbus_block_process_call</b>(int file, __u8 command, __u8 length, __u8 *values)</li>
</ul>
<p>Given the SMBus spec and the list of functions, it's quite easy to decide what each of them does.  If you're in any doubt, then look for comments in i2c-dev.h around the function.  Some of the functions above read data that's less than 32 bits in length yet return a 32 bit number.  This is so that an error can be returned.  If the returned value is 0 or above then just cast it into a type of the correct size.
</p>
<h1>Gotchas</h1>
<p>
During development, there were a few things that caught me out.  I also introduced a couple of other people into using this interface and they got caught out by some things too.  Hopefully I'll assist some others by listing those problems here.
</p>
<h3>The i2c-dev.h header file</h3>
<p>
The i2c-dev.h header file comes from the <a href="http://www.lm-sensors.org/" onclick="javascript:urchinTracker ('/outbound/article/www.lm-sensors.org');">lm-sensors</a> project.  You'll need to grab this from their source.
</p>
<h3>I<sup>2</sup>C addresses are 7 bits wide</h3>
<p>Apart from the rare 10-bit I<sup>2</sup>C addressing that some devices employ, all I<sup>2</sup>C addresses are 7 bits wide.  Until I discovered the error of my ways, I would write addresses down as the 7-bit number shifted left by one bit to create an 8-bit word.  Basically all I<sup>2</sup>C devices that do address encoding/detection (including the Linux Kernel) take the address as a 7-bit number. </p>
<p>So if your I2C address was all '1's,  you would write it as 0x7F - <b>not</b> 0xFE.</p>
<h3>Load the modules</h3>
<p>There's almost always this short period just after I've booted a LInux device in which I'll try to use I<sup>2</sup> but nothing will work.  Then I remember that I need to load the right modules - the one for the I<sup>2</sup>C controller itself and i2c-dev for the userspace interface.  I know it's simple, but for some reason I always forget.</p>
<h3>Use Checksums</h3>
<p>Always use checksums.  <a href="http://www.smbus.org/" onclick="javascript:urchinTracker ('/outbound/article/www.smbus.org');">SMBus</a> already defines a method of generating checksums.  They're also supported by the Linux kernel.  You can enable them using an ioctl:<br />
<code><br />
        if( ioctl( fd, I2C_PEC, 1) < 0)<br />
        {<br />
                fprintf( stderr, "Failed to enable PEC\n");<br />
                return -1;<br />
        }<br />
</code><br />
Disable them by changing the argument to the ioctl to 0.
</p>
<h3>Block transfers</h3>
<p>After painstakingly implementing SMBus block read/write on my I<sup>2</sup>C client, I discovered that the SMBus  block transfers weren't supported by the I<sup>2</sup>C driver for my machine's adapter.  So always check that the I<sup>2</sup>C driver supports the types of transfer that you want to do using the I2C_FUNCS ioctl.</p>
<h3>The Linux kernel documentation</h3>
<p>
The documentation in the /Documentation/i2c directory of the Linux source is a good source of information about using I<sup>2</sup>C under Linux.
</p>
<h1>Examples</h1>
<p>
You want examples?  Ok.  My <a href="http://www.studentrobotics.org/" onclick="javascript:urchinTracker ('/outbound/article/www.studentrobotics.org');">Student Robotics</a> colleagues and I have been writing various <a href="http://svn.studentrobotics.org/slug/utils/" onclick="javascript:urchinTracker ('/outbound/article/svn.studentrobotics.org');">test utilities</a> that you might like to have a look at.  I'm not guaranteeing that they work of course!
</p>
<p>I hope this was helpful.  I'd be most interested in hearing any feedback that you have.</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2007/11/11/using-i2c-from-userspace-in-linux/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>I2C Adapters</title>
		<link>http://xgoat.com/wp/2007/10/23/i2c-adapters/</link>
		<comments>http://xgoat.com/wp/2007/10/23/i2c-adapters/#comments</comments>
		<pubDate>Tue, 23 Oct 2007 17:51:28 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[electronics]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Robotics]]></category>

		<guid isPermaLink="false">http://xgoat.com/wp/?p=95</guid>
		<description><![CDATA[Since our robotics kit uses an I2C bus, it would be really handy for me to have a USB-to-I2C adapter. With the addition of a simple RJ11 adapter, I&#8217;d be able just plug in a module and start hacking on it without the annoying set-up times, that involve sorting out the Slug and power board [...]]]></description>
			<content:encoded><![CDATA[<p>Since our <a href="http://www.studentrobotics.org/" onclick="javascript:urchinTracker ('/outbound/article/www.studentrobotics.org');">robotics</a> kit uses an I2C bus, it would be really handy for me to have a USB-to-I2C adapter.  With the addition of a simple RJ11 adapter, I&#8217;d be able just plug in a module and start hacking on it without the annoying set-up times, that involve sorting out the Slug and power board combo.  If Student Robotics had several of these adapters, then any member could just grab a module and start hacking on some code.</p>
<h2>i2c-tiny-usb:</h2>
<p>I&#8217;ve just come across Till Harbaum&#8217;s <a href="http://www.harbaum.org/till/i2c_tiny_usb/index.shtml" onclick="javascript:urchinTracker ('/outbound/article/www.harbaum.org');">i2c-tiny-usb</a> design.  Rather surprisingly this uses a relatively cheap ATtiny45 Atmel AVR and nothing else.  It bitbangs the USB protocol.  Apparently, this works rather well.  What&#8217;s even more exciting, is that there&#8217;s a kernel driver available for the i2c-tiny-usb.  This makes it behave as a proper Linux i2c bus device, so interacting with it from software couldn&#8217;t be simpler.  This board can be an I2C master with clock speeds of up to 50KHz.</p>
<h2>OSIF</h2>
<p>The  <a href="http://www.headfuzz.co.uk/?q=USBtoI2C" onclick="javascript:urchinTracker ('/outbound/article/www.headfuzz.co.uk');">Open Source InterFace</a> (OSIF) is part of the <a href="http://www.openservo.com/" onclick="javascript:urchinTracker ('/outbound/article/www.openservo.com');">OpenServo</a> project, and I believe was designed by Barry Carter.  Carter took the i2c-tiny-usb design, and adapted it to use an AVR with more pins, and upped the maximum I2C clock speed to 400KHz.  Furthermore, the board has support for serial (I think UART), 6 GPIO lines and an ADC channel.  Barry Carter <a href="http://www.robotfuzz.com/" onclick="javascript:urchinTracker ('/outbound/article/www.robotfuzz.com');">sells</a> the OSIF for £20 a board.
</p>
<p>Carter also provides a kernel driver for the OSIF as well :-D</p>
<p>I bought an OSIF earlier today.  I like the way that the OSIF uses work from two previous open hardware projects to create <emph>and sell</emph> the results.  More on how it goes when I get it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://xgoat.com/wp/2007/10/23/i2c-adapters/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
