Wire.onReceive never getting called, but Wire.onRequest works fine

I have a simple sketch in which I am trying to get my Particle e-series as an I2C slave to emulate a TI BQ20Z95 battery protection IC (long story, but essentially I need Wire to respond as if it’s one of these chips). It is at address 0x0B, and is correctly attached to Wire. I’ve also tried attaching it to Wire1 (and updated the code accordingly) and experienced the same problem.

What happens is Wire.onReceive is called successfully, and it outputs two bytes of data (0x34 0x12) every time a register is requested, but Wire.onRequest is never called, so Wire.read() doesn’t work and I have no idea which register is being requested.

The code: No matter what the master requests for register, it always replies with (0x34 0x12) indicating that Wire.onRequest is not being called. If I change it to Wire.begin(0x0F) (or any wrong address), then it fails to reply at all, so I know that Wire is being set up correctly, and is responding to requests for data.

What could cause Wire.onRequest to never be called? Why can’t I figure out which register is being requested?

SYSTEM_MODE(MANUAL)

unsigned int smbus_response = 0x1234;
char command = 0;

void setup() {
  Wire.onReceive(receiveEvent); // register event
  Wire.onRequest(requestEvent); // register event
  Wire.begin(0x0b); //for external battery spoofing
}

void loop() {
  delay(10);
}

void requestEvent()
{
  Wire.write((uint8_t*)&smbus_response,sizeof smbus_response);
}

void receiveEvent(int howMany)
{
  smbus_response+=1;//never increments
  command = Wire.read();//doesn't get updated
}

This sounds a bit like this previous question

Yes, it does sound like a similar question. I am trying to do the same thing. However, that question, and the library it refers to, still use Wire.onReceive() and build on top of it. Since I can’t even get the lower level onReceive call to work, I don’t have a good reason to believe that the library would work either.

It looks like the problem has to do with the device I was communicating with. It turns out that the SMBUS Battery spec is ever so slightly different from regular I2C. Other people who have been trying similar things are having the same problems, though there seem to be no solutions yet. Clearly the Wire library won’t work without modification to support the SMBUS Battery.

http://forum.arduino.cc/index.php?topic=329264.0
http://forum.arduino.cc/index.php?topic=200951.0

1 Like