2
votes

I have a doubt regarding read operation in I2C, bit banging. The protocol which I am following for read as below: Start-slave address with write-ack-register loc-ack-stop. ...... Start-slave address with read-ack-read data-stop. I am reading data as FFh which is wrong. My doubt is, before sending the another start, do in need to send stop or can continue the another start for reading data without stop, which actually is a repeated start. Does sending a stop bit or not makes any difference. Also can someone tell what can be the possible reason if data read is FFh. But I can guarantee that write operation is successful, after seeing the scope shot. Please guide me.

Thanks

2
Please any one reply. - user123456
It depends on the slave device you're talking to. Many devices require a repeated-START (no STOP) in order to read registers: write the register number, repeated START, read the data. My experience is that with some devices, it will work OK with the STOP in between while others require the repeated-START for the internal state machine to work correctly. You need to read the data sheet. - DoxyLover
ok thanks.. My datasheet says STOP should be given before Start, but it doesnt work. Will give a try with Repeated Start. - user123456
What is your slave device? Please link to the datasheet. - DoxyLover

2 Answers

6
votes

The i2c protocol works like this

WRITE:
  1. send START
  2. write slave address with last bit as WRITE ACCESS(0)
  3. write sub-address: this is usually the address of the register you what to write to; if not applicable skip to 4.
  4. write data
  5. send STOP

Each byte you write to the slave device should be answered with an ACK if the operation was successful.

READ:
  1. send START
  2. write slave address with last bit as WRITE ACCESS(0)
  3. write sub-address: this is usually the address of the register you what to read from
  4. send START (this is a second start condition - a restart)
  5. write slave address with last bit as READ ACCESS(1)
  6. read data
  7. send STOP

All write and read operations (except the last read) are answered with a ACK if successful.

So in the case of a restart you don't send a second Stop.

As far as the 0xFF read result is concerned, you need to check the datasheet of the device, but some will return this value if the data you are trying to read is not available, yet!

Hope this helps.

1
votes

I just had this issue and found the reason: if you receive FFh in reading all the time, you are missing the repeated start.