{"id":124,"date":"2012-10-23T18:37:41","date_gmt":"2012-10-23T07:37:41","guid":{"rendered":"https:\/\/mindbleach.com\/words\/?p=124"},"modified":"2012-10-25T10:27:03","modified_gmt":"2012-10-24T23:27:03","slug":"i2c-and-device-limits","status":"publish","type":"post","link":"https:\/\/mindbleach.com\/words\/2012\/10\/23\/i2c-and-device-limits\/","title":{"rendered":"I2C and Device Limits"},"content":{"rendered":"<p>SparkFun&#8217;s Peter Dokter has an interesting <a title=\"According to Pete - SPI and I2C\" href=\"http:\/\/www.sparkfun.com\/news\/980\" target=\"_blank\">video<\/a> talking about SPI and i2c comms. i2c in particular is a great bus mechanism, due to the microprocessor only needing TWO wires to control (almost) any number of devices.<\/p>\n<p>Each device on an i2c network has a 7-bit address, so a single network theoretically supports up to 128 slave devices.In practice though, the limit is much lower. i2c slave chips often support only 8 different bus addresses, no more than 8 of that chip can be attached to the same i2c network. Others like the <a href=\"http:\/\/cache.freescale.com\/files\/sensors\/doc\/data_sheet\/MPL3115A2.pdf\" target=\"_blank\">MPL3115A2<\/a> aren&#8217;t configurable at all! Some people really do want more than one altimeter&#8230;<\/p>\n<p><em>You <strong>could<\/strong> have more than one chip to an address, it just means that the chips change in lockstep with each other, and any returned values are likely to be garbage. (being ANDed together by the open-drain bus)<\/em><\/p>\n<p>Enter the i2c <a href=\"http:\/\/www.nxp.com\/documents\/data_sheet\/PCA9547.pdf\" target=\"_blank\">PCA9547<\/a> multiplexer. It allows you to have multiple i2c &#8216;channels&#8217;, and decide which one is active at any one time. That way, you could for instance have one MPL3115A2 altimeter on each channel, for a total of 8 in your system instead of just one. Ironically, the multiplexer suffers from the same address issues as the other chips; You can fit only 8 multiplexers on an i2c bus.<\/p>\n<p><em>Or you can be sneaky&#8230;<\/em> What&#8217;s the maximum number of uniquely controllable 1-address i2c devices you can put on a multiplexed i2c network?<\/p>\n<h1>Step 0:<\/h1>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/alt.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-125\" title=\"altimeter\" src=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/alt.png\" alt=\"altimeter\" width=\"190\" height=\"159\" \/><\/a><\/p>\n<p>One bus, one device. Not much fun.<\/p>\n<h1>Step 1:<\/h1>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/single.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-128\" title=\"Single\" src=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/single-300x247.png\" alt=\"Single multiplexer, 8 devices\" width=\"300\" height=\"247\" srcset=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/single-300x247.png 300w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/single.png 369w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Let&#8217;s add a PCA9547 multiplexer! Now we have 8 switchable channels, and 8 individually controllable devices.<\/p>\n<p>But we can do better.<\/p>\n<h1>Step 2:<\/h1>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/parallel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-127\" title=\"parallel\" src=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/parallel.png\" alt=\"\" width=\"806\" height=\"306\" srcset=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/parallel.png 806w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/parallel-300x113.png 300w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/parallel-500x189.png 500w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a>Each multiplexer can be given one of 8 addresses in the space 1110xxx. In shorthand, we&#8217;ll call them 000 to 111. So, we can attach up to 8 multiplexers to any one bus.<\/p>\n<p>8 multiplexers \u00d7 8 channels = 64 devices, that each get a whole bus channel to themselves!<\/p>\n<p>We can do better.<\/p>\n<h1>Step 3:<\/h1>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-129\" title=\"tree\" src=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree.png\" alt=\"\" width=\"804\" height=\"841\" srcset=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree.png 804w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree-286x300.png 286w\" sizes=\"(max-width: 804px) 100vw, 804px\" \/><\/a><\/p>\n<p>Not only can we attach multiple multiplexers to one bus, we can attach a multiplexer to another multiplexer. In this simple tree configuration, the controller is connected to a multiplexer with address <em>000<\/em>. Each of <em>000<\/em>&#8216;s 8 channels is connected to a multiplexer with address <em>001<\/em>. <em>(For the same reason that we can have multiple devices with the same address if they&#8217;re not simultaneously connected, we can put same-address multiplexers on different channels.)<\/em><\/p>\n<p>So, going down the tree, there are:<br \/>\n1 of <em>000<\/em><br \/>\n8 of <em>001<\/em><br \/>\n64 of <em>010<\/em><em><\/em><br \/>\n&#8230; and so on, down to;<br \/>\n16,777,216 of <em>111<\/em><br \/>\nAnd each <em>111<\/em> multiplexer has 8 channels, so we have 134,217,728 individually selectable bus segments, and can support that many single-address devices. Should be enough?<\/p>\n<p>At this scale, we&#8217;d need 19 MILLION multiplexers, which not counting the net routing, the pull-up resistors, the power requirements, or the devices we&#8217;re supporting, would require (according to the package size) a circuit board with an area of more than 300 square metres.<\/p>\n<p>We can do better!<\/p>\n<h1>Step 4:<\/h1>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-132\" title=\"tree_extra\" src=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2.png\" alt=\"\" width=\"1348\" height=\"788\" srcset=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2.png 1348w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2-300x175.png 300w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2-1024x598.png 1024w, https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/tree_extra2-500x292.png 500w\" sizes=\"(max-width: 1348px) 100vw, 1348px\" \/><\/a><em>(It becomes unwieldy at this stage to try and draw even collapsed collections of multiplexers, so for the above, imagine that boxes with plural labels indicate multiple chips, and thick connections represent the 1:8 fan-out discussed previously.)<\/em><\/p>\n<p>Reading the PCA9547 datasheet, it mentions that you can <strong>also<\/strong> disable the multiplexer entirely so that <strong>no<\/strong> channels are selected. This allows another fun technique. In the above diagram, the left-most column is the tree structure show in the previous step. The second column is a slightly smaller tree starting at <em>001<\/em>, and so-on.<\/p>\n<p>By disabling all top-level multiplexers, then only enabling the multiplexers needed, all of the devices in all the trees can be accessed. The diagram only shows the first generation of extra multiplexers, this process can be recursively applied to each sub-tree.<\/p>\n<p>Eg to access a channel in multiplexer 101 \u2192 3 \u2192 1 \u2192 7:<\/p>\n<ol>\n<li>Disable all multiplexers<\/li>\n<li>Enable 101, select channel 3<\/li>\n<li>Enable 110, select channel 1<\/li>\n<li>Enable 111, select channel 7<br \/>\n(At this stage, multiple 110 and 111 multiplexers are &#8216;on&#8217;. We need to switch them off)<\/li>\n<li>Disable 101<\/li>\n<li>Disable 111<\/li>\n<li>Disable 110<\/li>\n<li>Re-enable 101. Now only the correct path is open, and the device can be accessed.<\/li>\n<\/ol>\n<p><a href=\"https:\/\/mindbleach.com\/words\/wp-content\/uploads\/2012\/10\/g10029.png\" target=\"blank\">Diagram here<\/a><\/p>\n<p>This adds <strong>19 million<\/strong> extra devices on top of the <strong>134 million<\/strong> we get from the simpler tree in the previous step. Now, that&#8217;s enough?<\/p>\n<p>A few more can be added through further recursion, but there are diminishing returns.<\/p>\n<h1>Step 5:<\/h1>\n<p>Left as an exercise for the reader. What if you have multiple i2c multiplexers with the same address in sequence? If initially active, sending &#8216;disable&#8217; would probably switch all of them off. And then sending &#8216;enable&#8217; would activate only the first one&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SparkFun&#8217;s Peter Dokter has an interesting video talking about SPI and i2c comms. i2c in particular is a great bus mechanism, due to the microprocessor only needing TWO wires to control (almost) any number of devices. Each device on an &hellip; <a href=\"https:\/\/mindbleach.com\/words\/2012\/10\/23\/i2c-and-device-limits\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-124","post","type-post","status-publish","format-standard","hentry","category-electronics"],"_links":{"self":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/124"}],"collection":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":5,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/posts\/124\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindbleach.com\/words\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}