I2c slave

Posted on
  • Any chance of adding I2c slave capabilities? Would be handy to control other espruino's from a master espruino.

  • Afraid not. I2C (as with SPI) tends to require an almost instant response to any data that's sent, and it's not really suitable for Espruino where executing the JS code to get a response might take something like 0.5ms.

    If you want to connect Espruinos together I'd recommend using the USART. You can put the transmit line into open drain mode and use a resistor to pull it up, then you can connect a whole bunch of them together. You can even send commands as normal JS if you want to.

  • I think the only hope of implementing something like that with the Pico would be to have a C implementation of the typical I2C interface with a number of registers - and have that read data from the registers or write it to them, and then fire off JS callbacks... which is very limited, so I'm not sure it would be very useful.

    On the other hand... what about clock stretching? Is there a maximum time you're allowed to stretch the clock for? I mean, the Espruino I2C wrapper seems to wait for eons before declaring the attempt at communication to have timed out...

  • When I looked at the timeout length, I don't think there was any specific spec - but 1ms seemed the one everyone went for - so it's doable, but a bit close and I guess it could end up being unreliable.

    Good idea about the 'common' I2C slave mode. We could do a similar one with SPI slave as well. Just something like an array of arrays that could be indexed for reading - and writes could just go via a callback.

  • Just added a bug for it here

  • I have done the UART path before. Was never very happy with it.

  • Why not? What didn't work properly?

    Out of interest, why do you think I2C would be better?

  • When dealing with 1 to many the I2c addressing made life so much simpler.

    I have had some success with the uart methods in 1 to many on a few projects that required it but it was never a fun adventure. ;-)

  • I'm actually doing something at the moment with one to many via UART... Data rate isn't a big deal, so actually I'm using the console. Each slave has:

    var ID = 1234;
    function onInit() {
      Serial1.setup(9600, { tx:B6, rx:B7 });
      pinMode(B6, "af_opendrain");

    and when I want to send data from the master I do:

    Serial1.println("myFunction()\n"); // global
    Serial1.println("if(ID==1234)myFunction(­)\n"); // just one

    Understandably that isn't ideal in all cases though. Thing is, with I2C it's basically just sending a byte first, which is the address. It shouldn't be too hard to do that with the UART too, I guess the only thing you'd really want to do is to have a good way of determining what the start of frame was.

  • What about ';' as start and a '\n' as end chars for a communication frame via UART?

  • You could use "\x03\x10" for start frame with "\n" at the end if you wanted - as that disables echo for the line being sent, so basically means you have no need to configure the device's console.

  • Cool, so let's send something like

    \x03\x10run({"cmd":"test"})\n to talk to all devices


    \x03\x10if(myID==4711)run({"cmd":"test"}­)\n to talk to a single device with id 4711.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

I2c slave

Posted by Avatar for cwilt @cwilt