hago
hago

Reputation: 317

Difference between SPI and I2C read/write access

I have this following question. What is the difference between the read/write operations when using SPI and I2C protocols. For example I have 2 different sensors one interfaced through SPI and another interfaced through I2C.

So I2C read is as follows:

  1. Send the 7 bit slave address followed by setting the read bit
    which will identify the right slave
  2. Send another 7 bit address of the particular register inside the slave device followed by read bit.
  3. Then how do we read the contents? Store it in a variable by using '=' operator in C?

For SPI read:

  1. Set the Chip select pin to enable the slave.

  2. Send the read command to the slave to say that it is read operation.

  3. Then how do we read the contents? Store it in a variable by using '=' operator in C?

Am I right with the sequence? Or Am I missing something? Please clarify. Thanks

Upvotes: 0

Views: 489

Answers (1)

akshaybharad
akshaybharad

Reputation: 13

I2C is half duplex. Meaning, master needs to provide 8 clocks after register address is sent and listen to data line. This can be done by three ways

  1. Polling - Look into the driver and check if it is polling for a particular status register after sending address. If yes, there should be an input argument with Rx buffer address. You can read that register for read values.
  2. Interrupt - If driver has configured interrupt for Rx complete, You can read data inside ISR
  3. DMA - Here driver must have configured a particular channel as Rx channel and configured a DMA complete interrupt. This interrupt will be hit after data is transferred.

SPI works in full duplex mode. but still slave cant respond immediately after recieving the address. Typically it requires 8 cycles again to respond. Above said 3 modes exists for SPI as well.

Whether you can read the values using = operator ? It depends on driver implementation. But most probably not. Typically input arguments like RX buffer and Transfer size are passed. If its synchronous/Blocking call, you should be able to read the content of the same buffer after the read/transfer call. Otherwise you have to read it in ISR or after some delay

Upvotes: 0

Related Questions