I2C, save(), onInit(), Internal Error - issues

Posted on
  • Hi, I'm really enjoying playing with the Espruino but recently had a small issue, mainly due to still learning to adapt from classical µC programming style. I figure this may help someone else who Googles for the keywords I did.

    I got the NFC module running well, and then wanted it to persist after reboot so typed save(). When it rebooted the I2C interface couldn't be brought up (INTERNAL ERROR: Timeout on I2C...). It seems Espruino tries to restore the state when you saved it rather than re-run the program again, which was weird to me but I get it now.

    So I figured I'd use delay() to give the NFC I2C module enough time to init, but of course there isn't a delay() function because this isn't how Espruino is supposed to work :)

    Anyway, I found putting some code in an onInit() function with the I2C init code in there, it will get run every time it powers on. This may seem obvious but onInit() is not yet documented in the Espruino reference as far as I can see.

  • Thanks - someone else mentioned about the lack of info on onInit - I'll try and add it to the documentation.

    Which command for I2C did you have to run in onInit? If you write I2C1.setup({scl:B6,sda:B7}); then Espruino is supposed to automatically remember that and run it at power on (even without onInit).

  • Unfortunately that doesn't seem to work for me. On Espruino 1.58, the following code example works:

    function onInit() {
      digitalWrite(LED3,0); //reset blue LED
      I2C1.setup({scl:B6, sda:B7});
    }
    
    onInit();
    
    var nfc = require("PN532").connect(I2C1);
    print(nfc.getVersion());
    nfc.SAMConfig(); // start listening
    
    setInterval(function() {
      nfc.findCards(function(card) {
        print("Found card "+card);
        card = JSON.stringify(card);
        if (card=="[4,238,254,40,117,69,0]") digitalWrite(LED3,1);
      });
    }, 1000);
    

    If I unwrap the onInit() function so the I2C1.setup() is run inline at the start, as soon as I type save() the Espruino reboots and I get the I2C errors. And every time it starts up after that.

    It does seem to set it up though:

    >dump()
    var nfc = {
      "i2c":{
        "_options":{"scl":B6,"sda":B7}
      }
    };
    I2C1.setup({"scl":B6,"sda":B7});
    digitalWrite(B6,0);
    pinMode(B6,"opendrain");
    digitalWrite(B7,0);
    pinMode(B7,"opendrain");
    
  • In fact further testing showed that in my example which works, if I press the reset button it continues to work. But as soon as I cut the power and reapply, it fails. If I execute nfc.SAMConfig again, it will start working.

    So the only way I've got it to work 100% is to run it all in onInit()

  • Right - well it's expected that you'd have to run SAMConfig in onInit - after all, the NFC doesn't remember anything.

    However you shouldn't need I2C.setup - thanks for your dump() command output - it looks to me like this is a bug in Espruino (the digitalWrite would end up resetting the pins after they'd been set up for I2C).

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

I2C, save(), onInit(), Internal Error - issues

Posted by Avatar for thomc @thomc

Actions