{"id":428,"date":"2013-06-11T22:12:13","date_gmt":"2013-06-12T05:12:13","guid":{"rendered":"http:\/\/www.robogourmet.com\/?p=428"},"modified":"2013-06-11T22:21:54","modified_gmt":"2013-06-12T05:21:54","slug":"communicating-with-i2c-devices-with-the-linkm","status":"publish","type":"post","link":"http:\/\/www.robogourmet.com\/?p=428","title":{"rendered":"Communicating with I2C devices with the LinkM"},"content":{"rendered":"<p><a href=\"http:\/\/thingm.com\/fileadmin\/thingm\/images\/photos\/tm_linkm_black_small.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/thingm.com\/fileadmin\/thingm\/images\/photos\/tm_linkm_black_small.jpg\" width=\"300\" height=\"225\"\/><\/a><br \/>\nI was recently looking for a USB to I2C adapter and mostly I found rather expensive ($100-300) development kits which tend to be large-ish boards. Considering I2C communication can be done with a fairly basic microcontroller, I thought these were poor solutions. I eventually tripped across the <a href=\"http:\/\/thingm.com\/products\/linkm.html\">LinkM<\/a>, which is, on the surface, a USB controller\/programmer for the BlinkM programmable LED devices. However, the BlinkM uses I2C as it&#8217;s communications bus and the LinkM is essentially a USB to I2C adapter. It&#8217;s pretty cheap ($30) and small, with a nice plastic housing and a simple female header connection. All the code is <a href=\"https:\/\/code.google.com\/p\/linkm\/\">open source<\/a>, from the client-side API to the firmware on the LinkM microcontroller. In addition, the client-side code is cross platform, in C, Java and Processing, working on Mac OS X, Windows, and Linux. Since the the USB interface is implemented as a <a href=\"http:\/\/en.wikipedia.org\/wiki\/USB_human_interface_device_class\">simple HID device<\/a>, it&#8217;s probably pretty easy to write code for any platform, really.<\/p>\n<p>That said, while the software can talk to generic I2C devices, it&#8217;s really geared towards supporting the BlinkM. As such, there is little documentation about how to talk to generic I2C devices. The simplest thing to to is compile the linkm-tool application located in the <a href=\"https:\/\/code.google.com\/p\/linkm\/source\/browse\/#svn%2Ftrunk%2Fc_host\">c_host folder<\/a>. Note that this depends on <a href=\"https:\/\/code.google.com\/p\/linkm\/source\/browse\/#svn%2Ftrunk%2FbootloadHID%2Fcommandline\">linkmbootloadlib<\/a>, so you&#8217;ll need to build that first.<\/p>\n<p>Windows notes:<\/p>\n<ol>\n<li>If compiling on a Windows platform, you&#8217;ll need MinGW &#8211; the LinkM developers recommend the <a href=\"http:\/\/tdm-gcc.tdragon.net\/\">TDM-GCC compiler suite<\/a>, which integrates MinGW and GCC.<\/li>\n<li>In the Makefile for linkmbootloadlib, you&#8217;ll need to comment\/remove line 98: <code>rm -f lib$(PROGNAME).a # FIXME<\/code>. You could replace it with the proper del command as well.<\/li>\n<\/ol>\n<p>Once you have linkm-tool compiled you can do the following:<\/p>\n<ol>\n<li>\nScan the I2C bus:<br \/>\n<code>linkm-tool -v --i2cscan<\/code>\n<\/li>\n<li>\nRead\/Write to an I2C device:<br \/>\n<code>linkm-tool -v --linkmcmd \"0x01,&lt;# bytes to write&gt;,&lt;# bytes to read&gt;,&lt;write byte 1&gt;,&lt;write byte 2&gt;,...,&lt;write byte n&gt;\"<\/code><br \/>\nFor example, if you need to write &#8220;0x50,0x11,0x20&#8221; (I2C address 0x50, command 0x11, data 0x20):<br \/>\n<code>linkm-tool -v --linkmcmd \"0x01,3,0,0x50,0x11,0x20\"<\/code><br \/>\nOr to read 3 bytes: &#8220;0x50,0x12&#8221; (I2C address 0x50, command 0x12):<br \/>\n<code>linkm-tool -v --linkmcmd \"0x01,2,3,0x50,0x12\"<\/code>\n<\/li>\n<\/ol>\n<p>This is fine for simple testing, later when you put together an app, you can simply include hiddata.h and call the relevant functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently looking for a USB to I2C adapter and mostly I found rather expensive ($100-300) development kits which tend to be large-ish boards. Considering I2C communication can be done with a fairly basic microcontroller, I thought these were poor solutions. I eventually tripped across the LinkM, which is, on the surface, a USB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,25],"tags":[],"class_list":{"0":"post-428","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-tech-tips","7":"category-technology"},"_links":{"self":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/428"}],"collection":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=428"}],"version-history":[{"count":18,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions"}],"predecessor-version":[{"id":446,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=\/wp\/v2\/posts\/428\/revisions\/446"}],"wp:attachment":[{"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=428"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robogourmet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}