Page 1 of 2

I2C slave?

Posted: Fri Oct 20, 2017 6:20 pm
by tuupola
Looking at docs and source it seems ESP32 port does not support I2C slave mode? Pyboard seems to have support. Are there any plans on implementing it in ESP32 port in nearby future?

Re: I2C slave?

Posted: Sat Oct 21, 2017 6:41 pm
by loboris
I2C slave is supported in esp-idf.
I'm planning to add hardware I2C slave support in my Micropython port the next month.

Re: I2C slave?

Posted: Sun Nov 19, 2017 6:47 am
by devnull
I2C slave is supported in esp-idf.
I'm planning to add hardware I2C slave support in my Micropython port the next month.
Hi Loboris;

You mentioned that you were going to add the I2C Slave Mode, is this likely to happen any time soon ?

Thanks

Re: I2C slave?

Posted: Sun Nov 19, 2017 8:05 am
by tuupola
I played around with with esp-idf and created a couple of I2C slaves. They seem to work well. Only problem is though, that since ESP32 has hardware I2C implementation and there is only two I2C busses the slave can only listen at two I2C addresses. I do not know if there is a way around this other than creating a software I2C slave implementation.

In any case I also would be interested this supported in Micropython.

Re: I2C slave?

Posted: Thu Jan 20, 2022 9:19 am
by devnull
Hi;

I know this is a very old post, but you mentioned that you managed to get a slave working on the ESP32 ?

Was this using micropython ??

Or does anybody else have a software implementation for an I2C Slave / Peripheral that they would not mind sharing ??

I dont want to do this on a pyboard.

Re: I2C slave?

Posted: Thu Jan 20, 2022 1:43 pm
by pythoncoder
See this doc. Crucially, from this doc,
The Pyboard or similar STM based boards are currently the only targets supporting I2C slave mode. Consequently at least one end of the interface (known as theInitiator) must be a Pyboard or other board supporting the pyb module. The Responder may be any hardware running MicroPython and supporting machine.

Re: I2C slave?

Posted: Fri Jan 21, 2022 4:50 am
by devnull
Thanks, yes I am aware that the pyboard is the only device with built in slave mode.

But I am referring to a software implementation of this, probably bit banging.

Re: I2C slave?

Posted: Fri Jan 21, 2022 9:46 am
by pythoncoder
I'm not aware of one. This is an issue which crops up fairly regularly. Even the Pyboard's hard slave mode is difficult to use:
There is a timing issue in that the I2C master requires that the slave be ready before it initiates a transfer. Further, in the MicroPython implementation, a slave which is ready will block until the transfer is complete.
I think this concurrency issue would affect software implementations: the master can initiate at any time but the slave needs to be able to get on with other things. There are a few solutions. You might invent another signal, where the master asserts a pin which interrupts the slave and puts it into blocking receive mode, perhaps via micropython.schedule. You might use uasyncio to poll the interface - but this would only work at very low clock rates. Or you might write an interrupt driven slave. Probably the best approach but quite hard to do as each ISR needs to return fast.

The last resort might be to use threading.

An interesting project. Rather akin to soft UARTs, often promised but seldom delivered. Tougher than they look. I don't think I'll be doing it ;)

Re: I2C slave?

Posted: Fri Jan 21, 2022 9:54 am
by devnull
OK, I thought that I would try the pyboard as a slave and esp32 as master:

PYB Slave

Code: Select all

data = i2c_slave.recv(1)
print(data)
ESP32 Master

Code: Select all

i2c_master.writeto(0x0b,b'X')
This works, but how do I go about having the slave reply ?

PYB Slave

Code: Select all

data = i2c_slave.recv(1)
i2c_slave.send(data)
print(data)
ESP32 Master

Code: Select all

print(i2c_master.readfrom_mem(0x0b,0XFF,1))
What I want to do is for the slave to reply to the master with a value that was determined from the byte address that the master sent.

Re: I2C slave?

Posted: Fri Jan 21, 2022 10:12 am
by pythoncoder
The doc I referenced describes a way to make a bidirectional interface between a Pyboard and an I2C master using Pyboard I2C slave mode. The interface behaves like a full duplex stream. All the code is there.