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
}