Search code examples
serial-portprotocolsreverse-engineeringi2cspi

Reverse-Engineering Protocol


I have a project I'm working on that involves the optical sensor from a mouse. I've got two communication pins (data and clock) isolated, but I don't have datasheet and I've been bitbanging my head against a wall trying to figure this thing out. I finally got a decent capture of the protocol using my microcontroller - though it's not perfectly in sync, the resolution should be high enough to identify what's going on - each sample is taken, then followed by a ~4us delay

As I'm looking at this data plotted, I see the data line moving sometimes when the clock is not - and the data line appears to move much more consistently than the clock, leading me to believe that my assumptions about which is data and which is clock may have been backward. However, I also see the clock line moving when the data is not - which wouldn't make any sense with my conjecture.

What I'm expecting to see in here - though I'm decreasingly confident that the protocol is set up this way - is some request for information on a specific registered, followed by a return of that information. Perhaps there's a pairing or initialization at the very beginning of the communication as well (which is where this capture starts).

I would really appreciate some help figuring out what is happening here, because I'm fairly new to this - and entirely lost.

Data is found in CSV format here: https://pastebin.com/h9Hx1dyw

The first 50 lines are just raw pasted here because the website won't let me ask my question without some code:

Time Index,Data,Clock
0,0,1
1,1,1
2,1,1
3,1,1
4,1,1
5,1,1
6,1,1
7,1,1
8,1,1
9,1,1
10,1,1
11,1,1
12,1,1
13,1,1
14,1,1
15,1,1
16,1,1
17,1,1
18,1,1
19,1,1
20,1,1
21,1,1
22,1,1
23,1,1
24,1,1
25,1,1
26,1,1
27,1,1
28,1,1
29,1,1
30,1,1
31,1,1
32,1,1
33,1,1
34,1,1
35,1,1
36,1,1
37,1,1
38,1,1
39,1,1
40,1,1
41,1,1
42,1,1
43,1,1
44,1,1
45,1,1
46,1,1
47,1,1
48,1,1
49,1,1
50,1,1

Solution

  • I would use gtkwave to analyze your samples, as a matter of fact I did, it support .vcd files as input, which seems to be easy to generate.

    There's a tool to convert .csv files to .vcd, csv2vcd, but your csv first need some formatting, I used the command awk, to move the first column to the last and transform its value to microseconds "us" (the time 0 though need to be in seconds, suffix 's', or csv2vcd will fail)

    cat input.csv | awk -F',' 'FNR==1{print $2","$3","$1; next}{print $2 "," $3 "," ($1*4)".0 us"}' >output.vcd
    

    output.csv will need to be manually edited then, like that

    "d0","d1","Time"
    0,1,0.0 s
    1,1,4.0 us
    ...
    

    and remove the last line in the file or csv2vcd will fail (d0 and d1 are for example I think you can name them as you like)

    At this point you can load the vcd file into gtkwave, drag the signals into the Time (this is how it works in linux at least) and analyze your samples.

    You can use combine up/down (multiple times) to combine the bits to two bits, a nibble, a byte and so on. I tried with your data but without decoding the protocol (ack/nack/stop/sync/whatever bits) seems useless.

    But the thing is vcd files can be more complex and contain more data, see the screenshots there for examples with decoded data and protocol.

    This could be useful to you, if you're gonna try to decode the protocol, to verify the decoder, see this module decode_i2c.cc from ArduLogic project which create a vcd file from an i2c 2 bit input.