I2C device

I have device using I2C protocol,
and want to set its registries via the gadgeteeer,
but sew too many classes for i2C devices become me confused…

  • which class is the best (if there is one…)?

  • in microsoft.spot.hardware- I sew the don’t ask for pin’s parameters- how they configure the device?

  • when I want to read- the class already execut before the asking for reading or only try to get data from the slave, and I need ask it before reading?


which socket it is configure to?, which pins?

I don’t understand how to use it

What board are you using? Is it a Gadgeteer project?

I use FEZ Spider as mainboard,
my I2C device is not ghi module,
but I connect it to the suit pins via the data-sheet of the module.

If you are using hardware I2C you need to connect you module to the socket that has letter “I”.

Pins 8 and 9 are I2C pins on the socket:

That what I did,
but in the FEZ spider there are 4 socket with the letter "I’,
so i connect it for example to socket no. 3,
but in the ctor of the, or the execute function of I2CDevice there is no parameter of socket number or pins?

Can you please show your code?

void ProgramStarted()
var switchUsbSocket = Socket.GetSocket(3, true, null, null);
//where can I configure with this variable?
var I2CConfig = new I2CDevice.Configuration(0x7f, 1000);
var I2C = new I2CDevice(I2CConfig);
byte[] b = new byte[2];
var xActions = new I2CDevice.I2CTransaction[2];
xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x00, 0x00 });
xActions[1] = I2CDevice.CreateReadTransaction(b);
I2C.Execute(xActions, 1000);
catch (Exception x)
throw new Exception(“exception thrown.”, x);

OK, so some of your questions are not really relevant so I’ll try to help out on some of the underlying concepts.

I2C is a bus. So the pins exposed on each socket are the same physical pins… plugging a device into one socket should be no different to plugging it into any other socket. (and to verify the pinout, you can look at the Spider schematic PDF and see that they are all using IO11 and IO12)

In a Gadgeteer sense, Gadgeteer extends the logical (software) I2C bus design away from what the core netmf definition is, so in the ctor of the GTI I2C bus they use the socket - for example, the Seeed Gyro module is an I socket device, check out this source code:


And you can see it’s ctor looks like:

Socket socket = Socket.GetSocket(socketNumber, true, this, null);
// Init I2C
i2c = new GTI.I2CBus(socket, 0x68, 100, this);

So if you’re using the GTI interface (which you can see in http://gadgeteer.codeplex.com/SourceControl/latest#Main/GadgeteerCore/Gadgeteer42/Interfaces/I2CBus.cs ) you can use the socket differently, otherwise you can use the non-Gadgeteer approach like the I2C example that Andre pointed you to.

hope that helps

thanks, think I understand
using this class-

I don’t sure myself about read and write,
when reading- I have to create before write transaction for bring the address?

I have to bting the address with the r/w bit, I mean: when I create read transaction, to address 0x00:[0x01]-for the right bit is the r\w bit? ot the I2C interface create it itself?

I think I’l understand it clearly if you can bribg here an simple example of write to 0x05h address, and read from this address to check.

thanks a lot!

Hi, Chaya. You can use the I2CDevice.WriteRead() method to read the value stored in a specific register address. You can take a look at the I2C transactions in my codeshare entry for an example:

Check out the VideoOutModulePlainNETMF.Program.VideoOutConfigVGA800x600() method. There are plenty of write transactions followed by a write/read from a specific register at the end of the method. I use SoftwareI2C to allow me to use any digital I/O as I2C pins instead of the dedicated hardware ones.

so I changed the code to:
void ProgramStarted()
var switchUsbSocket = Socket.GetSocket(3, true, null, null);
var i2c = new GTI.I2CBus(switchUsbSocket, 0x50, 100, null);
byte[] b = new byte[1];
int i = i2c.WriteRead(new byte[] {0x00}, b, 1000);
Debug.Print(b[0].ToString() + i);
but after runing- both b and i equals 0,
what should be the issue now?

void ProgramStarted()
var switchUsbSocket = Socket.GetSocket(3, true, null, null); 
var i2c = new GTI.I2CBus(switchUsbSocket, 0x50, 100, null);
byte[] b = new byte[1];
int i = i2c.WriteRead(new byte[] {0x00}, b, 1000);
Debug.Print(b[0].ToString() + i);

that is better? :slight_smile:

@ Chaya - Do you have a link to the datasheet for your I2C device by any chance?

What Architect is pointing out is that 100% of faults like this with I2C devices comes down to using the incorrect address. NetMF uses the 7-bit address, not a modified 8-bit read/write address, so for your device you will get 0xA0 and 0xA1 as the read/write addresses pushed to the wire because you’ve used the address of 0x50. If you need 0x50 and 0x51 on the wire, then the correct address for the device in question is 0x28.

that is the datasheet:
http://www.ti.com/lit/ds/symlink/tusb8040a.pdf .

the address in the ctor- need for the biggest address I using, or the biggest address the daclared in the datasheet?

If you are connecting to A17 and B19 then this is not an I2C bus slave. It is only I2C when enabled for the external EEPROM and the device is the master.

When enable as a slave, it is in SMBus mode and although this is similar to I2C, the command structure is different and I am not sure that the NETMF implementation will work with this. I’ll wait and see if anyone else has used it for SMBus before?

I don’t know about the SMBus,
but the default configuration,
and that is what in my device as I checked,
it is configure to I2C mode (it is changing bottun for SMBus mode…)

so what it can be?

When you configure it for I2C using SMBUSz pin, the device will expect that there is an external I2C EEPROM chip is connected and the device will try to read the configuration from it.

I didn’t explain clearly…
http://www.ti.com/lit/ug/sllu163/sllu163.pdf -
that is mydevice,
it based decleration about I2C registers is in-