Page 1 of 1

RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 10:30 am
by cyphunk
I found it difficult to understand what is available for using RPi as a I2C slave. The conclusion appears to be to use the work by joan in pigpio for hardware based I2C slave on GPIO19+18. Another option is to bitbang it with software.

The Broadcom hardware has extra pins for I2C slave. These are exposed on B+ RPi's. pinout.xyz shows PIN35/BCM19 has as Alt3 the function SCL and PIN38/BCM20 has as Alt 3 the function MISO. The pin descriptions at elinux show the same.

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 11:35 am
by joan
Re use of GPIO18. That is what the hardware uses.

http://abyz.me.uk/rpi/pigpio/pigs.html#BSCX
The BSC peripheral uses GPIO 18 (SDA) and 19 (SCL) in I2C mode and GPIO 18 (MOSI), 19 (SCLK), 20 (MISO), and 21 (CE) in SPI mode. You need to swap MISO/MOSI between master and slave.

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 2:21 pm
by cyphunk
Thank you joan for the clarification. Could you say if it is possible to setup two instances of a slave? I'm trying to emulate a device that appears to have 2 i2C slaves that show up in i2cdetect (on same bus). I've tried to do this in pigpio but it appears only one instance is ever initiated. If not possible, could you direct me to where in the code I may try to extend this possibility? If I'm misunderstanding something fundamental I apologize :/ Perhaps to do this I need two RPi's for each slave, placed on the same bus

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 2:35 pm
by joan
The BSC hardware only supports being one I2C slave at a time.

You could set it as slave X, receive a message, set it as slave Y, receive a message, etc. However you would have to ensure the master was somehow in sync as if it tried to send to X when the slave was Y the message would fail (no ack from slave address X).

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 2:38 pm
by cyphunk
joan wrote:
Wed Mar 13, 2019 2:35 pm
You could set it as slave X, receive a message, set it as slave Y, receive a message, etc. However you would have to ensure the master was somehow in sync as if it tried to send to X when the slave was Y the message would fail (no ack from slave address X).

I thought to try this but it appears that the first write shown in the saleae logic analysis log does does not create a call back. Is a EVENT_BSC generated for the first Write request, despite there being no data?

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 2:58 pm
by joan
cyphunk wrote:
Wed Mar 13, 2019 2:38 pm
...
I thought to try this but it appears that the first write shown in the saleae logic analysis log does does not create a call back. Is a EVENT_BSC generated for the first Write request, despite there being no data?
...
I don't know. It's ages since I was looking at this area and haven't got any stuff to hand to check with at the moment.

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Wed Mar 13, 2019 5:32 pm
by cyphunk
joan wrote:
Wed Mar 13, 2019 2:58 pm
I don't know. It's ages since I was looking at this area and haven't got any stuff to hand to check with at the moment.
Okay thanks for the guidance in any case.

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Thu Mar 14, 2019 10:24 am
by cyphunk
cyphunk wrote:
Wed Mar 13, 2019 2:38 pm
I've put debug statements everywhere in pigpio.c to try and find where it will read the address off the line: where there is a write to rxBuf, gpios are read, and use of the pin 18. Couldn't find it.
I guess I understand now why when getting a i2c request from the master that has no data this does not result in an event in pigpio. I assume broadcom handles the initial address request

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Thu Apr 11, 2019 7:38 am
by birdwatcher
Hi,
I'm using succesfully GPIO18/19 and BSC on a raspeberry Zero with I2C slave configuration. However I can't get it working to send a value back. So my Arduino is master, sends a value to the RPI -> that works great. How can the RPI now reply and send a value back?

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Sun Jan 12, 2020 4:09 pm
by rameyjm
I am working on the RPI i2c slave on a RPI3 / CM3 module. I have it working for Writing a register to the RPi as an I2C slave from and FT4232H master. The reading of a register works - however the value that is returned by the RPi for the register seems to be updating on a second read..

That is - I write register 0x00 with 0x0A, i read register 0x00 and for the first time i read the previous value. On the second read i get 0x0A - so it works with some bugs. I will be working on it again sometime soon but for now here is the link to the github repo of my current state

https://github.com/rameyjm7/rpi_i2c_slave

It has been 6 months at least since i have looked at it so its not fresh in my memory, but i recently found a project I'd like to include it in.

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Thu Jan 16, 2020 5:56 pm
by peppy.player
Is it possible to connect two Pi Zero modules as I2C slaves to Pi3/Pi4? Will pigpio support it?

Thanks in advance!

Re: RPi as I2C Slave, a summary (2019/03)

Posted: Sat Jan 25, 2020 1:04 am
by peppy.player
peppy.player wrote:
Thu Jan 16, 2020 5:56 pm
Is it possible to connect two Pi Zero modules as I2C slaves to Pi3/Pi4? Will pigpio support it?
Still hoping to get the answer from pigpio experts...

Thanks in advance!