I2C Slave Mode

Seriga
Posts: 1
Joined: Tue Oct 11, 2016 9:32 am

Re: I2C Slave Mode

Postby Seriga » Tue Oct 11, 2016 9:45 am

Hmmm... So have managed to implement a Slave mode for the ESP8266? There is an example code?

User avatar
martinayotte
Posts: 10
Joined: Tue Nov 04, 2014 4:33 am

Re: I2C Slave Mode

Postby martinayotte » Thu Oct 13, 2016 3:22 am

Rudi, did you got chance to upload your I2CSlave code somewhere ?

DeCoco

Re: I2C Slave Mode

Postby DeCoco » Fri Oct 14, 2016 6:45 pm

Hi all,

I also read this thread and it would be very intersting how you solved the bitbanged I2C slave. I also try it, doing by myself. I just started, without much code yet, but here is my idea:

- interrupt on rising edge SCL and SDA
- if rising edge SDA and SCL = 1 : START condition
- if rising edge SCL: count a counter with each rising edge:

Code: Select all

      switch(rec_cntr) {
         case 0 ... 7:   // read address
            os_printf("read i2c_address, counter = %d\n", rec_cntr);
            break;
         case 8:         // check address, send ack if ok (0) or not (1)
            os_printf("check i2c_address, counter = %d\n", rec_cntr);
            break;
         case 9 ... 16:    // write databyte
            os_printf("write data, counter = %d\n", rec_cntr);
            break;
         case 17:       // read ACK (must be 0)
            os_printf("read ack, counter = %d\n", rec_cntr);
            break;
         case 18:       // read stop
            os_printf("read stop, counter = %d\n", rec_cntr);
            rec_cntr = 0;   // reset rec_countr
            break;
         default: rec_cntr = 0;   // reset rec_cntr
      }
      rec_cntr++;   // increase counter


what do you think?

thanks for your feedback, if I have I'll share it.

masters423

Re: I2C Slave Mode

Postby masters423 » Sun Oct 23, 2016 1:28 am

Any luck with this code? I'm just starting the I2C slave journey along with all of you :D

User avatar
rudi
Posts: 197
Joined: Fri Oct 24, 2014 7:55 pm

Re: I2C Slave Mode

Postby rudi » Sun Oct 23, 2016 5:44 am

DeCoco wrote:
..here is my idea:

- interrupt on rising edge SCL and SDA



how looks your code for this?
how you do interrupt on sda + scl`?



DeCoco wrote:- if rising edge SDA and SCL = 1 : START condition
- if rising edge SCL: count a counter with each rising edge:


what you do if after a start comes a stop?

and

what you do after a start, send address, send byte
comes a start again and no stop..

you do not know, when a start or a stop comes.
you must be flexible.

please register on forum.
anonyom is not good .
i talk then with a wall ;-)

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

User avatar
rudi
Posts: 197
Joined: Fri Oct 24, 2014 7:55 pm

Re: I2C Slave Mode

Postby rudi » Sun Oct 23, 2016 4:38 pm

martinayotte wrote:Rudi, did you got chance to upload your I2CSlave code somewhere ?


edit:

the missunderstand was cleaned now.
lets see what happens.

rudi
Last edited by rudi on Tue Oct 25, 2016 4:13 am, edited 2 times in total.

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

apollo0226
Posts: 3
Joined: Mon Oct 24, 2016 4:22 pm

Re: I2C Slave Mode

Postby apollo0226 » Mon Oct 24, 2016 4:41 pm

Do we get any official support from espressif ?
Or any sample code?
I do need this.

User avatar
rudi
Posts: 197
Joined: Fri Oct 24, 2014 7:55 pm

Re: I2C Slave Mode

Postby rudi » Mon Oct 24, 2016 8:20 pm

apollo0226 wrote:Do we get any official support from espressif ?
Or any sample code?
I do need this.


next time i will post the doing with a smaller base.
yes you will get a sample code here next time
so keep smile - but you have to studdy i2c protokoll first.
there is nothing mysteriose on the code.
and there comes no end solution for your homework.
you have to optimate the code then by your self and needs.

what is need/comes:

GPIO setup for openDrain
if you not know about this, you must studdy first the difference to openDrain

GPIO set valu
Gpio set ACK, NACK
if you not know about this, you must studdy first, how you can give an Low Value to an OpenDrainPin and a high.
to give a LOW by set the gpio to low is not enough!

ISR / Interrupting and comparing
Start, Stop, 7bit, 10bit... Adress, Data
if you not know about this, you must studdy first Interrupting with GPIO
and how you can
- set
- compare
- clear


best wishes
rudi ;-)

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

apollo0226
Posts: 3
Joined: Mon Oct 24, 2016 4:22 pm

Re: I2C Slave Mode

Postby apollo0226 » Mon Oct 24, 2016 10:54 pm

I know how to set the GPIO and ISR.
Now I set the SDA a low active Interrupt.
So when the Interrupt first trigger , it must be start condition, because SCL is high, SDA is low.
So I get the 8 bit and set ACK, this is slave address and R/W bit.
I compare it , is this my slave address? if no , set the NACK and exit, If yes, go to check R/W bit to Read or Write. If Read , get more data.

My question is when do I stop ? Checking SCL and SDA both high (STOP condition) in the SDA ISR ?
Do the SCL need to set to Interrupt ? if yes , active high or low? What do it do in SCL ISR?

If you give us the sample code , it will help a lot.

User avatar
rudi
Posts: 197
Joined: Fri Oct 24, 2014 7:55 pm

Re: I2C Slave Mode

Postby rudi » Tue Oct 25, 2016 3:29 am

apollo0226 wrote:
I know how to set the GPIO and ISR.
Now I set the SDA a low active Interrupt.


sry - you did it not know :)

Code: Select all


if(( (gpio_status>>I2C_SLAVE_SDA_GPIO)& BIT0)&&scl) //SDA trigger and scl high   


you must set to anyedge

Code: Select all


 if( sda )// sda posedge , stop
            {
            ets_printf("stop..\n\0");



and any edge you must handle

Code: Select all


 gpio_pin_intr_state_set(GPIO_ID_PIN(I2C_SLAVE_SDA_GPIO),GPIO_PIN_INTR_NEGEDGE);
 gpio_pin_intr_state_set(GPIO_ID_PIN(I2C_SLAVE_SDL_GPIO),GPIO_PIN_INTR_DISABLE);


and on each handle
you must know, when it is a start and when it is a stop

Code: Select all


else //sda negedge,start
   
         {
            ets_printf("start..\n\0");


you must each edge handle and compare

because you know what is a start
and because you know what is a stop
you can make the procedure like you need.

apollo0226 wrote:So when the Interrupt first trigger , it must be start condition, because SCL is high, SDA is low.


sry no.
it triggers allways, must allways tigger
in the isr then you check
is it a start
if is a start, then check if addr match
if addr match
then check again is it not a stop
then check addr match
if addr match then again
then check again is it not a stop
if you have checked addr
then check it is a write or a read cmd
..

apollo0226 wrote:
So I get the 8 bit and set ACK, this is slave address and R/W bit.



no sry.
you must allway first check is it no stop
you know what stop is
so check
get the pin valu of scl and check against sda,
if sda is high, then it is a stop
apollo0226 wrote:
I compare it , is this my slave address? if no ,
set the NACK and exit, If yes, go to check R/W bit to Read or Write.
If Read , get more data.


sry no :)
you only make ack,
no NACK
nack is allways, when you do no ACK.
ok?
you do not a NACK

NACK is given by the OpenDrain Config
thats the "trick"
you only config the Gpio to OpenDrain
and set the value for it to high ( NACK )
if you not change the pin valu for a ACK
then automatically the master read a NACK
you have not to set the PIN Value for the GPIO to High
its configured high as OpenDrain.
you only change the config for the GPIO if you give a ACK
and this can read the master because it is set as Opendrain too.
ok?

apollo0226 wrote:

a) My question is when do I stop ?
b) Checking SCL and SDA both high (STOP condition) in the SDA ISR ?
c) Do the SCL need to set to Interrupt ? if yes , active high or low? What do it do in SCL ISR?

d) If you give us the sample code , it will help a lot.


a) as master? or as slave?
b) no, first goes sda high, and scl is low at this time, this is a stop, and yes in the ISR.
c) yes - if you do by HW yes, you have to check SDA AND SCL,
you can do a small only by SDA, this was my start too. and make a software ISR - this is the simplest way until you not go faster

but if you go faster, then your software "time stretch" is not detailed enough to handle SDA on right timming.

d) no sir ;-)
you have to learn first how the I2C protokoll works, then come back and we start again step by step.
i know, you can do it. your thinking is not to much wrong on the start and stop. be sure this is in your head on right function,
then come back and we work together i am sure on end of week the base stand here.

come on guy!
you need my example not - i am sure.

;-)

best wishes
rudi ;-)

step 1)
post your code how you do setup opendrain the gpio for I2C slave
this is the first step.
lets go - show me..

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 3 guests