I2C slave?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
tuupola
Posts: 54
Joined: Sun Sep 17, 2017 12:10 am
Contact:

I2C slave?

Post by tuupola » Fri Oct 20, 2017 6:20 pm

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?

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: I2C slave?

Post by loboris » Sat Oct 21, 2017 6:41 pm

I2C slave is supported in esp-idf.
I'm planning to add hardware I2C slave support in my Micropython port the next month.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C slave?

Post by devnull » Sun Nov 19, 2017 6:47 am

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

User avatar
tuupola
Posts: 54
Joined: Sun Sep 17, 2017 12:10 am
Contact:

Re: I2C slave?

Post by tuupola » Sun Nov 19, 2017 8:05 am

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.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C slave?

Post by devnull » Thu Jan 20, 2022 9:19 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C slave?

Post by pythoncoder » Thu Jan 20, 2022 1:43 pm

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.
Peter Hinch
Index to my micropython libraries.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C slave?

Post by devnull » Fri Jan 21, 2022 4:50 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C slave?

Post by pythoncoder » Fri Jan 21, 2022 9:46 am

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 ;)
Peter Hinch
Index to my micropython libraries.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: I2C slave?

Post by devnull » Fri Jan 21, 2022 9:54 am

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.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: I2C slave?

Post by pythoncoder » Fri Jan 21, 2022 10:12 am

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.
Peter Hinch
Index to my micropython libraries.

Post Reply