I2C Slave Mode

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

Re: I2C Slave Mode

Postby rudi » Sat Jul 09, 2016 11:54 pm

Jeisonas wrote:Rudi, this looks very exciting! I would be very grateful if you could share the library, because there is currently no proper way of running an ESP as a slave apart from using UART.


hi

left side ( I2C slave):
ESP8266 with BMP

right side ( I2C master ) :
ESP8266 with OLED

(central ) Master send CMD "doit" to ( kitchen-node) Slave, Slave reads temperature, Master Send CMD "giveme", Slave send data, Master display this on Oled. same ( and others ) is done with SPI..., Modbus, CAN ( MCP2515 ) ..


best wishes
rudi ;-)
Attachments
IMG_5103_small.jpg

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

scargill
Posts: 70
Joined: Sun Nov 02, 2014 8:09 pm

Re: I2C Slave Mode

Postby scargill » Sun Jul 31, 2016 1:22 pm

Did this go anywhere in terms of code for I2c slave? I'm not that hot on interrupts - it really would be nice to have an i2c slave.c and .h file as we do for the masters. Surprised Espressif have not followed up on this and included in the SDK.

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

Re: I2C Slave Mode

Postby rudi » Mon Aug 01, 2016 6:44 am

scargill wrote:Did this go anywhere in terms of code for I2c slave? I'm not that hot on interrupts - it really would be nice to have an i2c slave.c and .h file as we do for the masters. Surprised Espressif have not followed up on this and included in the SDK.


hi pete
yes, after our holidays, i start a fine simple demo and give it to espressif ( stanza ) with documented doings ( pdf )
and there will be then a demo i2c_slave.c and i2c_slave.h. i promissed martin this too.

have a look to a nice user GUI..

Test Demo I2C Master Code:
Master call a counter func,
count is local variable and returned
sendByte is the Byte, that Master send to the Slave ( Address 0x54, WriteCommand )

settings vars:

vars.jpg


settings extern from i2c_master.c:

externs.jpg



user_init()
and
counter func

i2c_master_demo_1.jpg


i2c doings
i2c_master_demo_2.jpg


code explorer errors, warnings:

( see the next post, cause only 5 files are allowed )


and the flowchart generate by click

Code: Select all


/* i2c_master_demo */

#include <osapi.h>
#include <c_types.h>
#include <i2c_master.h>


uint8 addr = 0x54;
uint8 cmdWrite = 0;
uint8 cmdRead = 1;
uint8 ACK = 0;
uint8 sendByte = 0;

uint32 counter()

{
  static uint32 count = 0;

  count++;
  sendByte = count;
  return count;
}

void i2c_talk()

{

  i2c_master_start();
  i2c_master_writeByte(addr+cmdWrite);
  ACK = i2c_master_checkAck();
  if (ACK)
  {
    i2c_master_writeByte(sendByte);
    os_printf("ACK ADDR True\n");
  }
  else
  {
    os_printf("addr does not match\n");
  }
  i2c_master_stop();
}

void i2c_demo_init()

{

  /* FYI this function are extern from driver pack
  void i2c_master_gpio_init(void);
  void i2c_master_init(void);
  #define i2c_master_wait    os_delay_us
  void i2c_master_stop(void);
  void i2c_master_start(void);
  void i2c_master_setAck(uint8 level);
  uint8 i2c_master_getAck(void);
  uint8 i2c_master_readByte(void);
  void i2c_master_writeByte(uint8 wrdata);
  bool i2c_master_checkAck(void);
  void i2c_master_send_ack(void);
  void i2c_master_send_nack(void); */
 
 
  i2c_master_gpio_init();
  os_delay_us(30 * 1000 );
  i2c_master_init();
  os_delay_us(30 * 1000 );
}

void user_rf_pre_init()

{

}

void user_init()

{

  i2c_demo_init();
  while (1)
  {
    os_printf("count: %d\n", counter() );
    i2c_talk(sendByte);
    // os_printf("hello world\n");;
    system_soft_wdt_feed();;
    // os_delay_us(1 * 1000 * 1000);;
    os_delay_us(20 * 1000);;
  }
}




and do the compile + flash if you press on click more..

compile.jpg




the I2C Slave on same way, .. i post the complet here later after the holidays.. and:

and FYI:

this is freeware!

i will post later more details ( gui src, repo, leader, democode, chippack and more.. ) ,
if the esp32 is out this weeks, we are back from holdiays too, so we start the doings

-> ESP8266
-> ESP8285
-> ESP32
-> each next esp that comes out

it is an amazing gui ( freeware! ) - will later write more about this - and steps -
but for this summer pause - you must wait for it a little time.
the preview chip packs was done - for a release the chip packs need a small test process time
and i search beta user for the freeware gui with esp chip packs.

best wishes
rudi ;-)
Last edited by rudi on Mon Aug 01, 2016 7:06 am, edited 1 time in total.

-------------------------------------
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 » Mon Aug 01, 2016 7:01 am

push ( append a file )

code explorer errors, warnings:
code_explorer_warnings.jpg



btw:
and yes
we have support for

-> static locals
-> volatile
-> and more


static-locals.jpg



best wishes
rudi ;-)

-------------------------------------
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 » Mon Aug 01, 2016 8:01 pm

scargill wrote:..
Surprised Espressif have not followed up on this and included in the SDK...


pete, i think - because it is a really simple protocoll (i2c) - we have docu about gpio, interrupts and we have the i2c_master, so we can build simple the i2c_slave by using the standard i2c RFC ( Slave )

but i agree with you, it really would be nice to have an i2c slave.c and .h file as we do for the masters -
but think over, the "player on i2c_slave" must study the i2c protocoll too, because there are steps need to hold on the protocoll rules.

the slave is only a talk member to the master, and does only, when ( clock ) and what the master request. the begin for the slave is, to understand how to handle the master - so the slave can follow the instruction.

there must be a base understand for start and stop sequenze, ( SDA, SCL ) what happend on this and why.
what happend when master : i2c_master_start()
what happend when master : i2c_master_stop()

so we can setup the interrupt ( combine compare with SCL ) on SDA Pin for a START and other interrupt ( combine compare with SCL ) on SDA Pin for the STOP. this is a base understand.

the next interrupt is on the 8 bits DATA on SDA whilest not Stop, this is done 8 times, on 9 bit time slot the master read ACK/LAST example, this is base too. and vola - you have a i2c_slave.c and i2c_slave.h

;-)

the kitchen demo here i posted is only a user doing - when the i2c_slave is used.

the i2c base must be clear, how i2c works, then take the i2c docu, interrupt docu, gpio docu, i am sure - you finnish before i post the demo next time by self. its really easy.

i start with interrupts only on SDA first time, and make a while on SCL,
then i got the info between the reading lines , we must do on SDA + SCL

but i promissed, will post my i2c_slave.c and .h and the demo project next time.


edit: 4 August 2016
preinfo - what comes after the holdiay: DevF

example, here comes a master example flowchart, done with the freeware GUI IDE DevF

I2C_Master_Example_with_DevF.jpg


simple generated code :

Code: Select all


// we include Headers here
   #include <osapi.h>
   #include <c_types.h>
   #include <i2c_master.h>

// global vars
   uint8 addr = 0x54;
   uint8 cmdWrite = 0;
   uint8 cmdRead = 1;
   uint8 ACK = 0;
   uint8 sendByte = 0;


uint32 counter()
{
  static uint32 count = 0;

  count++;
  sendByte = count;
  return count;
}


void i2c_talk()
{

  i2c_master_start();
  i2c_master_writeByte(addr+cmdWrite);
  ACK = i2c_master_checkAck();
  if (ACK)
  {
    os_printf("ACK ADDR True\n");
    i2c_master_writeByte(sendByte);
  }
  else
  {
    os_printf("addr does not match\n");
  }
  i2c_master_stop();
}


void i2c_demo_init()
{

  i2c_master_gpio_init();
  os_delay_us(30 * 1000 );
  i2c_master_init();
  os_delay_us(30 * 1000 );
}


void user_init()
{

  i2c_demo_init();
  while (1)
  {
    os_printf("count: %d\n", counter() );
    i2c_talk(sendByte);
    system_soft_wdt_feed();
    os_delay_us(1 * 1000 * 1000);
  }
}



for this, we need in project folder driver files from sdk:
-> driver / i2c_master.c
-> driver / include / i2c_master.h

and we can share the "sketch" simply with one project file ( likewise arduino )
we can export parts, we can import parts, we can share parts, we can work together on a project...

DevF Install is easy:
download the release project from the leader of the freeware at github to your desktop and expand.
you only must call one exe file for the ide - and make your setups/configs. it comes black to white.
so you can change all color/colour by your self like you like, this setups saved in the registry.

after this, download the language definition file for the esp serie from my github later:
there are then

- esp8266/esp8285
- esp31b
- esp32

and copy the text file to the language folder.

the freeware support just in time:
- chinese
- english
- espanol
- francais
- polish

and later ( i am at work to translate )
- german

links, esp repo, i2c slave and a small how to for start after the holiday.
we start step by step in this from scratch together.
please stand still patient :) found my master just in time :)
and the stuff is great and freeware

( the same is build for the esp32 )

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

Dustupendr
Posts: 2
Joined: Mon Aug 15, 2016 8:33 pm

I2C Slave Mode

Postby Dustupendr » Mon Aug 22, 2016 7:30 pm

Hi,

Anyone knows where can I get the lyrics for "Seven Days Saturday night" by Slave to the Square Wave?

Thanks,

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

Re: I2C Slave Mode

Postby martinayotte » Tue Sep 13, 2016 6:09 am

@rudi , did you got chance to prepare this I2C Slave code for the community ?

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

Re: I2C Slave Mode

Postby rudi » Tue Sep 13, 2016 11:05 pm

@Martin - we back from holiday since yesterday - yes - think in few days ( latest this weekend ) you read it here

best wishes
rudi ;-)



edit (14 Sept 01:30 PM ) :
@martin i must edit this a little, sorry:
thinked, that after our holiday there is an ESP32 ( ESP32-D0WDQ6, ESP3212, ESP32 Developer V2, ESP32 Developer version: John Lee ) parcel here, and because i combine this I2C doing with a esp32 fun project ( audio, video sdhc ) - and just in time ( looked in my email office ) there is no parcel with esp32 on the express way - i think it can be a small extra duration perhabs a few days in next week too because in china is mid autumn festival too and there are holidays. not sure they send parcel now before the weekend comes.

But will make it so now, if no parcel comes before next national holiday then i post a small example with i2c_slave.c and i2c_slave.h files, that you can use in yours. hope this helps in the meantime - because there are holdiays do 17 september i will contact on (earliest) 18. Sept (latest + one week ) Stanza with the pdf and source code like i promissed, and we have then docu to the i2c too in the docu thread/forum too.

edit ( 14 Sept 01:40 PM ) :
@martin - i will combine the small example with two ESP8266 Modules.
so if there is no esp32 next week here - we can use this example with esp's what we have.
and can test the i2c slave doing well.
so i make it with one ESP8266 as Master and the other ESP8266 as Slave.

My runing example was with ESP31 ( ESP32 beta ) and ESP8266.

Info:
chin. holidays:
15.09.-17.09.2016 Mid-Autumn Festival
and the next are on:
01.10.-07.10.2016 National

best wishes
rudi ;-)

-------------------------------------
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 » Wed Sep 21, 2016 8:54 am

btw..

- now i try part 1 of 3 to combine with esp32..
got v2 today, v3 is at custom office for clearing, think i get next few days
then we build the I2C "ESP-family" project ;-)

arrived2_txs.jpg


best wishes
rudi ;-)

-------------------------------------
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 » Tue Sep 27, 2016 6:27 pm

- now part 2 of 3 :
ESP32 standalone TCP done
https://twitter.com/eMbeddedHome/status ... 2203046912

next step:
combine with esp8266 as I2C Slave,
read ADC, post on ESP32 WebContent on Client Request

the same is later done with BLE and connected devices.
but this is an other theme and for http://ESP32.com


best wishes
rudi ;-)

btw:
Yes, for makers on ESP8266
there is then the same Standalone TCP for an ESP8266
in combine a ESP8266 as I2C Slave.
Or we build a Network..lets see whats happend next time.

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

Who is online

Users browsing this forum: No registered users and 2 guests