Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Controlling Multiple I2C devices

howto i2c iic NetMF 4.3

  • Please log in to reply
8 replies to this topic

#1 NameOfTheDragon

NameOfTheDragon

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationCanterbury, Kent, UK

Posted 20 March 2015 - 11:33 PM

I need to control multiple I2C devices on the same bus. This seems like a reasonable requirement since I2C is inherently designed as a multi-master bus. However, when I try to create two devices, I get an exception on creating the second device. Example:

            var i2c1 = new I2CDevice(new I2CDevice.Configuration(0x60, 10));
            var i2c2 = new I2CDevice(new I2CDevice.Configuration(0x7B, 40));

I get an InvalidOperationException on the second line.

 

Looking into the firmware source, it looks like it is because I2CDevice is trying to reserve the output pins twice, which fails. Surely, they don't expect me to create a new instance of I2CDevice for each and every transaction? That just seems mega inefficient.

 

Any I2C users out there had similar problem? What's the right way to manage multiple devices?

 

 



#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 21 March 2015 - 05:16 AM

I never tried I2C on Netduino, but it sounds like other ports, which have to be disposed to be reused. This is surely a elegant yet reliable solution, but -agree- not so efficient.

Did you try to swap the configuration instead of creating many instances of I2CDevice?


Biggest fault of Netduino? It runs by electricity.

#3 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 22 March 2015 - 01:56 AM

Absolutely can be done, instantiate multiple Configurations and just set device.Config = configX.  



#4 NameOfTheDragon

NameOfTheDragon

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationCanterbury, Kent, UK

Posted 22 March 2015 - 10:43 PM

Ok thanks for the suggestions. I have tried setting the configuration property and that works, so I have a way forward. I can't help feeling that this is a leaky abstraction though, because an I2CDevice is not the same thing as the entire bus; the pins should belong to the bus, not the device. At least I have a workaround.

--Tim



#5 brianmakabro

brianmakabro

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationLouisville, KY

Posted 22 March 2015 - 11:45 PM

I think it may have been done that way because of the "Netduino as master-only."  It's very similar to the way MS did their Point-Of-Sale architecture.  It's really not very intuitive, at first. (IMHO, of course.)  But then since ND can only be the master, it IS the I2CDevice - and the slaves are all just so many connections.



#6 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 23 March 2015 - 08:05 AM

Tim, I'm 100% with you, but abstraction comes at a cost and Micro Framework tries to solve high-level problems on (relatively) very poor hardware.

Swapping the config is surely cheap in term of computation, but of course leaks in term of abstraction. However, you might think the I2Device as the "I2C logic driver", and the various configs as "instances" of this driver. Such a viewpoint comes maybe a little closer to the abstract-hardware model.

Good luck!


  • NameOfTheDragon likes this
Biggest fault of Netduino? It runs by electricity.

#7 NameOfTheDragon

NameOfTheDragon

    Advanced Member

  • Members
  • PipPipPip
  • 112 posts
  • LocationCanterbury, Kent, UK

Posted 25 March 2015 - 07:12 AM

The more I think about it, the more I feel that it is a disastrous design. Consider what you have to do to have different devices running on multiple threads accessing an I2C device!! (which I do).

 

I have come up with a tentative solution by simply wrapping the I2CDevice class in something a bit more intelligent, I am still testing it to see if it has any impact on performance. I will post something if and when I have a viable solution.

 

--Tim



#8 EddieGarmon

EddieGarmon

    Member

  • Members
  • PipPip
  • 24 posts

Posted 25 March 2015 - 03:32 PM

I have done the same, but went as far as a true interface for a bus and another for a device. now I can plug in a mock bus for driver testing, and a spot bus for working on a netduino.

 

https://github.com/E...er/Hardware/I2C



#9 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 28 March 2015 - 03:40 AM

Hi,

 

I might be missing something, but there are many wrapper classes available for supporting multiple devices on the I2C bus e.g. 

 

Netduino wiki

 

http://wiki.netduino...ass.ashx?HL=i2c

 

or

 

NetMF Toolbox see multiII2C.cs

 

http://netmftoolbox.codeplex.com

 

I am using an enhanced version of the wiki sampe to access multiple devices on the same I2C bus

 

 

@KiwiBryn

blog.devmobile.co.nz







Also tagged with one or more of these keywords: howto, i2c, iic, NetMF 4.3

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.