Page 1 of 1

I2C slave mode.

Posted: Sun Nov 25, 2012 12:07 pm
by z80
Dear All,

I've noticed that chibios I2C driver doesn't implement I2C slave mode. I need that mode for my hardware as it consists of several identical MCUs on one I2C bus.

I've implemented I2C slave functionality by myself. Unfortunately, I've done it only for one MCU type: STM32F10XXX as I have only it. And it must be it doesn't work quite well at the moment: it works for separate read and write requests but it doesn't support read after write at the moment due to some reason. Anyway, it seems to me it would be useful to share the code. Slave mode functionality by default is invisible and everything is as without it. But if one defines I2C_USE_SLAVE_MODE to be TRUE i2cSlaveIoTimeout() function could be used

i2c_slave_modification.zip
I2C slave partial implementation.
(14.57 KiB) Downloaded 572 times


At the moment slave IO procedure is asynchronous. It returns immediately after invocation. But I2C works continuously after the very first call. To change IO data it's necessary to call the same procedure with different in and out buffers. I'm not sure it is in the mainstream of "chibios" but I'm ready to adjust it if necessary.

Re: I2C slave mode.

Posted: Sun Nov 25, 2012 6:04 pm
by Giovanni
Hi,

Very interesting, I am moving this thread in the contributions forum, the API looks OK, please let us know if you make more developments about this.

Giovanni

Re: I2C slave mode.

Posted: Mon Nov 26, 2012 10:56 am
by z80
Sure! With pleasure!

Re: I2C slave mode.

Posted: Sun Dec 02, 2012 8:09 pm
by z80
Hi!

I'd like to share some thoughts related to I2C slave implementation. The problem is that with my I2C slave implementation "i2cMasterTransmitTimeout()" only transmits and can't receive anything afterwards. Reading a datasheet I noticed that it is expectable behavior and nothing could be improved. At least it looks so.

Reading documentation for my MCU I again and again pay attention on the following phrase: "To generate the Stop/Restart condition, software must set the STOP/START bit just after reading the second last data byte". This sentence is from "I2C master receiver" section. And stop/restart is mentioned only there.

From this I conclude that STM32F10X hardware "understands" I2C restart only when slave is in transmitting mode. And it also means that "i2cMasterTransmitTimeout()" with it's capability to read after write can't actually do that if slave is based on STM32F10X MCU as slave doesn't understand that (it understands write after read instead).

So it looks like the only possibility to read and write to I2C slave based on STM32F10X MCU is to use separate read and separate write requests. And it does work. I can "read" and "write" or "write" and "read" if both "read" and "write" are separate requests. In other words full requests with START, ADDRESS, DATA and STOP parts. Am I right? And does one have any idea about it?

And I'm sorry if I ask in wrong place!

Re: I2C slave mode.

Posted: Wed Jan 23, 2013 12:50 pm
by z80
I2C slave mode update.
Implemented receive/transmit callback functions in order to know if io takes place and update data buffers.
i2c.zip
I2C slave mode update.
(14.74 KiB) Downloaded 666 times

Re: I2C slave mode.

Posted: Wed Jan 30, 2013 7:48 pm
by z80
Dear all, I'm very sorry for so many fixes of just a couple of files but here is one more update.
Fixed I2C slave transfer mode.
i2c.zip
(15.79 KiB) Downloaded 717 times