2

I have an i2c device that i would like to rreverse engineer. Using the oringal master of the device here is a snapshot from the logical analyzer

enter image description here

I would like to replace the orginal master of the device with an arduino, the device has an interrupt pin that when detected it will do the trasaction above.

So far what i have figuerd out is the address is 0x53 (the first byte). I have also figured out by interactive with the device that the second,third, and last byte are data from the slave device.

How do i write an arduino code to simulate this transaction? Here is the code i have written and the response i got is not quite the same

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Wire. setClock(100000)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(0x53, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

enter image description here

3
  • 2
    Are you sure that you should get the same answer everytime you request data from the device? And what happens with the real master before that read transaction? Often I2C devices work like "master write to tell the device what data you want to have --> master read to actually get that data". So if there is a master write cycle before the read transaction that can be important. Before trying to recreate the master with an Arduino I would try to understand the protocol. There is no easy way around that. Depending on what you expect from the device search for patterns in the data stream.
    – chrisl
    Oct 22, 2021 at 11:15
  • @chrisl i can assure that there is no write cycle, before each transaction or even the very first transaction during power-up. That is not difficult to check in a logic analyzer. And yes i have recorded all the possible oputs of the device( there are 11). Its just keeps reapting with the only different thing is the second,third, and last byte depending how you interact with the device. How do i add a bit of delay between each byte?
    – DrakeJest
    Oct 22, 2021 at 11:29
  • 1
    For introducing a delay you would need to change the Wire library. Though I doubt that this would help. The delay is introduced by the master (since if the slave would do the clock stretching here the Arduino couldn't overcome this). Why would the slave need it? It perfectly works in the communication. In I2C when the slave is too slow for the master the communication normally totally breaks apart, mostly resulting in a blocked I2C bus. Also you can see in the first picture, that the slave is releasing SDA line right after the acknowledgement pulse. That suggest, that it isn't too slow.
    – chrisl
    Oct 22, 2021 at 17:28

1 Answer 1

1

Customised Protocol

As has been mentioned in the comments, it may be that the controller and target are communicating using a customised protocol for which you will need to modify the Wire library and/or the TWI library.

Modifying the TWI library is not easy because it uses a very convoluted mixture of ISR code and non-ISR polling code.

I've re-written the TWI library so that you can use standard burst-read and burst-write protocols in either ISR mode or polling mode, or build your own protocol using building-block functions. It uses the watchdog timer to detect timeouts when the bus freezes due to an error.

Terminology

The terminology of my TWI library conforms to NXP Semiconductors recently released v7 of the I2C-bus specification which has:

Updated the terms "master/slave" to "controller/target" throughout to align with MIPI I3C specification and NXP's Inclusive Language Project

UM10204, I2C-bus specification and user manual, v7, 2021-10-01, Table 1, Revision history

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.