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

I2CDevice extension


  • Please log in to reply
22 replies to this topic

#1 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 November 2010 - 08:26 PM

WARNING: This is NOT an official build by Chris and his team. If you have any I2C device that does not work due to missing Repeated Start condition in the communication, and you know how to use MFDeploy to update the firmware, and you are willing to help, this may be for you.

Attached is a zip archive that contains managed library CW.NETMF.I2C.dll and native part in firmware, which extends the existing I2C functionality by adding WriteRead(...) methods to I2CDevice class. Passing I2COptions.RepeatStart as the value of options parameter causes the Repeated Start condition to be issued, the content of writeBuffer is used as Internal Address and the communication happens in Master Receiver Mode (as described in the datasheet section 29.5.4). Recommended steps:

  • Unzip attached archive,
  • Use MFDeploy to flash the firmware,
  • Wire your I2C device (pull-up resistors are mandatory on TWI lines),
  • In your project add reference to CW.NETMF.I2C.dll,
  • In your source code add 'using CW.NETMF.Hardware' and WriteRead(...) calls where necessary,
  • Build, Deploy and run the application.
There is a sample application included in the archive, the usage of WriteRead method is similar to SPI.WriteRead:

// Melexis MLX90614 Thermometer (supported clock 10..100 kHz)
I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(0x5A, 50));

const int timeout = 100;
var buffer = new byte[2];

// The following code does not work, due to missing
// Repeated Start between Write and Read transactions
//var wTx = I2CDevice.CreateWriteTransaction(new byte[] { 0x07 } );
//var rTx = I2CDevice.CreateReadTransaction(buffer);
//var r = i2c.Execute(new I2CDevice.I2CTransaction[] { wTx, rTx }, timeout);

// The following code uses the new extension method with Repeated Start condition
// Read T_Obj1 from RAM address 0x07
var result = i2c.WriteRead(new byte[] { 0x07 }, buffer, timeout, I2COptions.RepeatStart);
if(result == 3)
{
  DumpTemperature(buffer[1], buffer[0]);
}

I would like to thank community member freds for his participation in testing and valuable feedback. The devices tested and reported working are:

Known issues & limitations:

  • The size of Internal Address (writeCount) must be 1, could be extended up to 3 (resp. 2) bytes for 7-bit (resp. 10-bit) slave addressing,
  • Neither writeBuffer nor readBuffer can be null, combinations to produce single Write (null readBuffer) or Read (null writeBuffer) transactions are subject to future revisions,
  • The value of options must be I2COptions.RepeatStart, use transaction classes and Execute method when you don't need Repeated Start,
  • The dll contains definition of ExtensionAttribute, which may cause conflicts if you have already defined it in your code,
  • The firmware included in the archive is based on 4.1.0.3, so it does not include latest changes and bugfixes,
  • The addition to official firmware to be discussed (what are I2C.XXXTransaction classes good for anyway ;-)
Edit: The full source code of both managed library and firmware changes is included in the archive. The feature is enabled in the solution file.

Looking forward to hearing from you.

Attached Files



#2 freds

freds

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts

Posted 08 November 2010 - 10:05 PM

WARNING: This is NOT an official build by Chris and his team. If you have any I2C device that does not work due to missing Repeated Start condition in the communication, and you know how to use MFDeploy to update the firmware, and you are willing to help, this may be for you.

Looking forward to hearing from you.


I have to say I tested two builds from CW2, a verbose debugging message output prototype and a cleaned up release.

It was very easy for me as it worked as expected the very first time. I use it for 8 bit and 16 bit register reads with the SCP1000-D11 pressure sensor.

Code fragment is below:

#define CW2Extension //for writeread I2C operations on the Netdunio board
#if CW2Extension
using CW.NETMF.Hardware;
#endif
        /// <summary>
        /// Read register value
        /// </summary>
        /// <param name="r">register to read 16bits from</param>
        private void read_register16(byte r)
        {
            m_writeBuffer1[0] = r;
            m_transfer_count = 0;
#if (CW2Extension)
            m_I2C_device.WriteRead(m_writeBuffer1, 0, 1, m_readBuffer16, 0, 2, m_timeout, I2COptions.RepeatStart);
#else
            m_transfer_count = m_I2C_device.Execute(m_I2C_WriteRead16, m_timeout);
            if (m_transfer_count != 3)
            {
                throw new InvalidOperationException("Failure to transfer: " + m_transfer_count.ToString() + " bytes transferred");
            }
#endif
        }

Attached Files



#3 osno

osno

    Member

  • Members
  • PipPip
  • 17 posts

Posted 08 November 2010 - 11:47 PM

I'm testing with an 24LC256 EEPROM and I'm getting an ArgumentException using: int bytesTransfered = eeprom.WriteRead(addressBuffer, 0, 2, data, 0, data.Length, timeout, I2COptions.RepeatStart); (where addressBuffer is a byte[2] and data is a byte[] I get from a parameter). I also tried the short version. Am I doing anything wrong? Thanks, J

#4 osno

osno

    Member

  • Members
  • PipPip
  • 17 posts

Posted 08 November 2010 - 11:51 PM

Ok, known issue. I'm using 2-byte addresses, the driver supports 1 byte. Working on it, sorry for the troubles to users reading the conversation.

#5 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 09 November 2010 - 07:27 AM

Ok, known issue. I'm using 2-byte addresses, the driver supports 1 byte.

Yep, unfortunately 2-byte addresses are not supported yet. Thank you for testing parameter validation code, it actually works Posted Image

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 09 November 2010 - 07:28 AM

I forgot to mention that the full source code of both managed library and firmware changes is included in the archive. Edited the original post.

#7 Shaw

Shaw

    Member

  • Members
  • PipPip
  • 14 posts

Posted 21 November 2010 - 07:27 AM

I'm trying to get I2C to work with an ITG-3200 device. However I can't seem to get anything (or much) out of it. I read the datasheet and it does make mention of "repeated start" within the nodes. Does this mean I should be looking at using this I2C driver rather than the standard one? Thanks Shaw

#8 freds

freds

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts

Posted 21 November 2010 - 05:11 PM

I'm trying to get I2C to work with an ITG-3200 device. However I can't seem to get anything (or much) out of it. I read the datasheet and it does make mention of "repeated start" within the nodes. Does this mean I should be looking at using this I2C driver rather than the standard one?

Thanks
Shaw


Sounds like it, no harm in trying it. Though if you need analog input also; CW2 firmware doesn't have the full netduino repertoire.

The ITG-3200 is three axis gyro, whats your end project?

#9 Shaw

Shaw

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 November 2010 - 06:02 AM

Sounds like it, no harm in trying it. Though if you need analog input also; CW2 firmware doesn't have the full netduino repertoire.

The ITG-3200 is three axis gyro, whats your end project?


I tried it... no luck. I'm wondering whether there's something I've done wrong... or blown my chip up or something. I've got a "Logic" logic probe that does I2C decoding and it seems that nothing comes back from the chip - but I can see bits going out from the Netduino.

I was hoping to build an IMU or AHRS or something - just tinkering at the moment. A stabilized video camera mount with tracking and LANC connectivity is another idea I had - so you could put a camcorder on the end of a boom and have it stabilized and aimed by uC and use XBee or similar to wirelessly transmit the commands.

#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 November 2010 - 06:09 AM

I tried it... no luck.


Just to double-check: did you include pull-up resistors in your I2C circuit?

Chris

#11 Shaw

Shaw

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 November 2010 - 12:52 PM

Just to double-check: did you include pull-up resistors in your I2C circuit?

Chris


Hi Chris, I did not include resistors... as I haven't required them for other sensors - however I have done a bit (lots) of reading regarding the use of these breakout boards with Arduino's and people have had mixed results there - some people are saying they need resistors, others are not..

What's the verdict? to pull up or to not pull up that is the question :)

#12 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 November 2010 - 03:56 PM

Hi Shaw, Every I2C circuit needs pull-up resistors. Some breakout boards have them, and some rely on external pull-up resistors. Also, make sure that you're driving the I2C data/clock lines via the voltage (5V or 3.3V) specified by the I2C device. Chris

#13 Shaw

Shaw

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 November 2010 - 04:13 PM

Okay, I did a bit more reading... I think every other chip I have has internal pull up resistors! How amusing. I found this article http://www.robot-ele...C_Tutorial.html and it says only one set is required per bus, so based on that - could I cheat by just putting another sensor on the bus that already has the resistors? Thanks :)

#14 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 November 2010 - 04:17 PM

Okay, I did a bit more reading... I think every other chip I have has internal pull up resistors! How amusing.

I found this article http://www.robot-ele...C_Tutorial.html and it says only one set is required per bus, so based on that - could I cheat by just putting another sensor on the bus that already has the resistors?

First, the reasons that Netduino doesn't have integrated pull-up resistors (other than the weak ones in the Atmel MCU) are: [a] Arduino doesn't have them--and we wanted to maintain shield compatibility; [b] the value of the resistors varies based on voltage used and number of components.

Adding another sensor may work but if it doesn't you'll want to check to see what pull-up resistors your combined circuit requires and what the additional sensor provides.

Does that help?

Chris

#15 Shaw

Shaw

    Member

  • Members
  • PipPip
  • 14 posts

Posted 22 November 2010 - 04:22 PM

Thanks Chris - the perfect answer. If it wasn't 220am I would go and try it out now :). Guess I'll find out tomorrow Looks like that was the case - how annoying :) It's working nicely now. Thanks for the help

#16 Patrick van Es

Patrick van Es

    Member

  • Members
  • PipPip
  • 14 posts

Posted 23 November 2010 - 08:32 PM

I have a display (lcd) equipped with a byvac i2c interface and trying to get it doing something. But all it does is showing me the start screen and nothing else. I think the cause of it is the repeated start thing. So I have two questions: does this firmware also works with the netduino plus? Will this become part of the official firmware? Thanks in advance Patrick

#17 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 24 November 2010 - 08:17 AM

I have a display (lcd) equipped with a byvac i2c interface and trying to get it doing something. But all it does is showing me the start screen and nothing else. I think the cause of it is the repeated start thing.

According to the ByVac documentation, you should be able to communicate with the display using the official firmware (i.e. without the Repeated Start condition). Commands like 'Send LCD Command', 'Send LCD Data', 'Display On' are basically single 'Write' transaction, so they should work fine. The Repeated Start condition is required to perform commands that read data back from the display.

If you are not able to make the 'Write' commands work, please check the wiring (pull-up resistors?), device address, or post the device model number, your schematic and code, so we can have a look...

So I have two questions: does this firmware also works with the netduino plus?

Yes, but it will turn it into Netduino (i.e. there will be no Ethernet and SD card support), but you can always 'recover' by flashing the official Netduino Plus firmware.

#18 Patrick van Es

Patrick van Es

    Member

  • Members
  • PipPip
  • 14 posts

Posted 24 November 2010 - 07:27 PM

Ok, I'm using pull-up resistors on the SCL en SDA to 5v lines. I've divided the device-address by 2 (due to the 7 bit addressing). For the communication i'm using the i2cbus library found here in the community combined with this display controller interface bv4218. The display (4x20) doesn't response on anything. When I'm using the same display with a usb->i2c interface (also from byvac) and hyperterminal the display works as it should. I know i'm doing something wrong but can't find it. I'm having difficulty with the byte sequence to send.

I'm currently not at home so i can't post the code for now... i'll do this later...

#19 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 24 November 2010 - 07:47 PM

I've divided the device-address by 2 (due to the 7 bit addressing).

What do you mean by 'divided by 2'? You should use the address provided in the datasheet, 0x42 - it is already 7-bit (less than 0x7F). Please check the return value of I2CDevice.Execute(...) method, it is the number of transacted bytes - zero indicates the communication does not happen, for example due to invalid slave address.

#20 Patrick van Es

Patrick van Es

    Member

  • Members
  • PipPip
  • 14 posts

Posted 25 November 2010 - 08:23 AM

:) Still learning every day :) . I give it a try today (this evening) and post my result here. Thanks CW2




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.