Inscribe

I2C Wire Library Problem

8 posts in this topic

Dear 43Oh,

 

First post in 43Oh. Please point out mistakes.

 

I am creating sample applications for a MSP430F5538 board in Energia environment. pins_energia.h has been suitably modified and board integrated into Energia (Version 18 at this time).

 

Sample code to get I2C working (on MPL3115A2 Pressure sensor) has been tested on the ESP12 using Arduino environment . The same code fails in Energia. Code uses only wire.h (i.e., no device specific libraries included). The code is posted below:

#include <Wire.h> // for IIC communication

#define WHO_AM_I   0x0C
#define MPL3115A2_ADDRESS 0x60 // 7-bit I2C address

void setup()
{
  int nDevices = 0;
  byte error,address;
  
  Wire.begin(14,2);  // Mispod-ESP 12E       
  ///Wire.begin();   // Mispigy F5538       
  Serial.begin(115200);  
  Serial.println("MPL3115A2 IIC Tester");

  Serial.println("Scanning...");

  address = 96;
  ///for(address = 1; address < 127; address++ ) 
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found");
  else
    Serial.println("Scan done");
}

void loop()
{
  if(IIC_Read(WHO_AM_I) == 196) 
  {
    Serial.println("MPL3115A2 online!");
  }
  else
  {
    Serial.println("No response - check connections");
  }
  delay(3000);
}

byte IIC_Read(byte regAddr)
{
  // This function reads one byte over IIC
  Wire.beginTransmission(MPL3115A2_ADDRESS);
  Wire.write(regAddr);                      
  Wire.endTransmission(false);       
  Wire.requestFrom(MPL3115A2_ADDRESS, 1); 
  return Wire.read();
}

void IIC_Write(byte regAddr, byte value)
{
  // This function writes one byte over IIC
  Wire.beginTransmission(MPL3115A2_ADDRESS);
  Wire.write(regAddr);
  Wire.write(value);
  Wire.endTransmission(true);
}

Only change in the code for the two platforms is the pin allocation (See comments in code).

 

Address detection works fine as per code in setup(). Logic analyzer results below:

 
ESP12E Scan Result -
post-31170-0-42283700-1488736025_thumb.png
 
Mispigy F5538 Scan Result -
post-31170-0-58759400-1488736028_thumb.png
 

 

 

Multibyte read fails. Results :

 

ESP 12E Who Am I Result -

post-31170-0-99113900-1488736026_thumb.png

 

Mispigy F5538 Who Am I Result -

post-31170-0-02392000-1488736030_thumb.png

 

As seen, the I2C communication is different for the Energia wire library. 

 

Can someone throw some light on this behaviour?

 

Our intent is to use the sketch transparently between the Energia based Mispigy and Arduino based ESP12E, with no tweaking of either environment.

 

Thanks.

Share this post


Link to post
Share on other sites

While I2C is not my strongest area, I'm wondering about the clock in the two situations. Stepping through Energia's wire implementation I have this vague memory that it defaults to a 400k clock??? Could that play a role? And, as often can be the case, could pulldown resistors be playing a role???

Bob

Share this post


Link to post
Share on other sites

@NurseBob -

I have confirmed both SCLs (MSP430 and ESP) are at a nominal 100 KHz. And both instances have the pullups in position.

@bluehash -

Yes, pullups are in place.

@Rei Vilo -

You are right. For Energia, no argument is required for Wire.begin() as a master. However, for ESP8266(Arduino), before using I2C, pins for SDA and SCL need to be set by calling Wire.begin(int sda, int scl), if different from defaults. The code I pasted was the ESP version. Please see the comments on this line.

Thanks for your time.

I am specifically intrigued by the different responses from the two platforms (MSP and ESP) for the same sketch. The last two screen captures show the responses of ESP and MSP, in that order to a WHO_AM_I query.

ESP Sequence - (Write to) Address - Command - (Read From) Address - Response ...... and the response is correct.

MSP Sequence - (Write to) Address - Command - (Write to) Address - (Read From) Address - Response.....and the response is incorrect.

Both these are initiated by the same sketch snippet -

  Wire.beginTransmission(MPL3115A2_ADDRESS);
  Wire.write(regAddr);                      
  Wire.endTransmission(false);       
  Wire.requestFrom(MPL3115A2_ADDRESS, 1); 
  return Wire.read();

The additional (Write to) Address is being injected by the Energia Wire library (in RED above), which is causing the response to fail.

Is this a desired feature or a bug? What would be the right construct to obtain a valid response from the slave?

- Misra

 

Share this post


Link to post
Share on other sites

Actually, the I²C implementation for the ESP8266 differs from the Arduino standard. Energia complies with the Arduino standard.

Quote

You are right. For Energia, no argument is required for Wire.begin() as a master. However, for ESP8266(Arduino), before using I2C, pins for SDA and SCL need to be set by calling Wire.begin(int sda, int scl), if different from defaults. The code I pasted was the ESP version. Please see the comments on this line.

I'm using I²C extensively on LaunchPads and I've never experienced any issues.

  • The MSP430F5538 is not officially supported by Energia. Have you tried with a LaunchPad officially supported by Energia, e.g. MSP430G2553 or MSP430F5529?
  • I don't have any MPL3115A2 at hand, so I can't proceed with tests. Does its data-sheet mention some non-standard usage of I²C protocol?
  • Finally, have you tried to add a delay and change your code to
Wire.beginTransmission(MPL3115A2_ADDRESS);
Wire.write(regAddr);                      
Wire.endTransmission();  // modified       
delay(100);  // added
Wire.requestFrom(MPL3115A2_ADDRESS, 1); 
return Wire.read();
bluehash likes this

Share this post


Link to post
Share on other sites

Thanks, Rei Vilo.

I will try out your code and revert.

5528 vs 5529 - No, I haven't tried on the Launchpad. But, considering that the two cores are identical (and the pins are re-arranged appropriately), I do not expect major differences.

MPL3115A2 data sheet is very 'generic' and brief in its I2C description. I couldn't use it to understand expected behavior for this specific case.

- Misra

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now