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

Talking to Multiple I2C Devices

I2C Exception

  • Please log in to reply
5 replies to this topic

#1 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 23 May 2015 - 08:20 PM

I am trying to talk to 2 I2C devices on the same I2C bus, one is a TMP102 temperature sensor on address 0x48 and the other is a STM8S set up as an I2C slave with address 0x50.

 

I know that I can talk to both of these independently but what I'm looking to do is to try and have the two devices on the same bus but I am getting an error.

 

My code looks like this:

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using System.Threading;

namespace I2CMaster
{
    public class Program
    {
        public static void Main()
        {
            I2CDevice i2cBus = new I2CDevice(new I2CDevice.Configuration(0x50, 50));
            I2CDevice tmp102 = new I2CDevice(new I2CDevice.Configuration(0x48, 50));
        }
    }
}

The second statement generates an exception:

 

An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.dll

 

What I do not know is if this is expected behaviour, i.e. am I supposed to setup one I2C device, do some stuff with it and then release the I2C device before moving on to the second one.

 

Anyone out there reading from two I2C slaves on the same bus with NETMF ?

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 23 May 2015 - 08:33 PM

Bit of alternative thinking and I got the solution.

 

Set up the I2C bus as follows:

I2CDevice.Configuration stm8s = new I2CDevice.Configuration(0x50, 50);
I2CDevice.Configuration tmp102 = new I2CDevice.Configuration(0x48, 50);
I2CDevice i2cBus = new I2CDevice(stm8s);

Then when you want to read from the different devices just change the config:

i2cBus.Config = stm8s;
int bytesRead = i2cBus.Execute(transactions, 100);
i2cBus.Config = tmp102;
bytesRead = i2cBus.Execute(reading, 100);

Not sure if that's how it supposed to work but I get the right results.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 Frode

Frode

    Advanced Member

  • Members
  • PipPipPip
  • 202 posts
  • LocationNorway

Posted 24 May 2015 - 03:07 AM

Mark, somewhere on the forum is an I2CBus class which will allow you to use multiple I2C devices. Search for I2CBus.cs or similar.

#4 Frode

Frode

    Advanced Member

  • Members
  • PipPipPip
  • 202 posts
  • LocationNorway

Posted 24 May 2015 - 03:09 AM

Here it is: http://forums.netdui...cbus/#entry4156

#5 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 24 May 2015 - 09:40 AM

Thanks for the pointers.

 

If you are interested in what I was doing then there's more information here:

Creating I2C master devices with the STM8S

Creating I2C slave devices using the STM8S

 

The Netduino 3 allowed me to have a known working reference for the I2C protocol.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#6 Frode

Frode

    Advanced Member

  • Members
  • PipPipPip
  • 202 posts
  • LocationNorway

Posted 24 May 2015 - 05:35 PM

Nice - I've also written a TMP102 driver for Netduino (https://github.com/S...ature/TMP102.cs).







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.