L Ja
L Ja

Reputation: 1506

Master and slave transceivers

For a uni assignment I need to use I2C/TWI to be able to communicate with an slave device both ways. The master needs to receive and transmit, and same for the slave.

However, I am lost on how to approach this. I am aware the master has a master receiver mode, but since I don't exactly know when the master needs to be reading or writing, I don't know how to approach this.

Could anyone bump me in the right direction here?

Upvotes: 1

Views: 156

Answers (1)

David Grayson
David Grayson

Reputation: 87406

It might help to study other I2C slave devices that can send and receive data like the LSM6DS33 acceleromter/gyro and this library for it.

To receive data from the slave device, the master has to initiate an I2C read transfer. There is a bit in the first byte of the transfer that the master must set to a particular value to indicate whether it is a read transfer or a write transfer. Depending on how your device works, the master might send a write transfer before the read transfer in order to specify the address of a register to read from, but this is not universally required.

There are many ways for the master to decide when to do the read. Some I2C devices have interrupt pins that will drive low or high to indicate there is new data to be read. The master could read a register that indicates when data is ready. Or the master could just continuously read the data. The right method depends on the details of what you're doing. But keep in mind that the read is always initiated by the master, for your slave device needs to have some mechanism for storing the data until the master is ready to read it.

Upvotes: 1

Related Questions