issue with I2C on pico (initialization)

Posted on
  • Hello,
    i'm writing a simple timer using an Adafruit 7 segment display which uses I2C.
    I have made a working prototype using the original kickstarted Espruino and wanted to transfer the design to the pico.
    The display requires initialization which I do in an E.on('init') as well as inline:

    function ledInit() {
      I2C1.setup({scl:B6, sda:B7});
      initAddr = (addr) => {
        I2C1.writeTo(addr, 0x21); // clock on
        I2C1.writeTo(addr, 0x81); // flash
        I2C1.writeTo(addr, 0xE0 | 3); // brightness 15
      };
    
      initAddr(0x70);
    }
    
    E.on('init', function() { //doesn't work on pico
      USB.setConsole();
      digitalPulse(LED2,1,200);
      ledInit();
    }
    ledInit(); //doesn't work on pico
    

    For the pico, i changed the pins to B10 and B3
    The problem is that while i'm on USB, the init works just fine. However, when I save the code and run it with a battery, the LED init fails. I later tried to call the init on BTN1 and that works fine. Something about initial power that's not working. I tried setTimeout to call it and that fails as well.
    I also tried the USB.console trick and that didn't help.

    setTimeout(ledInit,2000); // doesn't work
    setWatch(ledInit, BTN1, { repeat: true, debounce: 50 }); // works but is less than ideal
    

    Any ideas?
    I'm running latest firmware available

  • Just checking - but you also changed I2C1 to I2C2?

    That is a strange one. .. and you're calling setTimeout(ledInit,2000); from E.on('init')?

    Could you add a digitalPulse(LED1 to ledInit so you can make sure that it does get called at the time you think it should be called?

  • Yes, i changed it to I2C2 and no, the timeout was just inline. I will try it inside the init. As it is, I'm still a bit unsure of execution. It seems when I send the code to the device, it gets executed immediately. that does not, however, happen when plugging it in? I thought the code gets parse every time it's powered on.

  • the timeout was just inline

    Yes, that could be your problem.

    Check out http://www.espruino.com/Saving - it should hopefully clear up what happens. By default code is executed as it is uploaded, and save() saves an image of the current intepreter state.

    You can change how it works, but then you miss out on the ability to tweak things on the fly using the left-hand side of the IDE.

  • tried putting timeout in E.on('init') - nothing. it almost seems as if the init function does not get called when the device is plugged into power.
    if i plug it in, connect it to the ide, and then run load(), then it works as intended, but otherwise it doesnt look like it's working. any way to debug?

    in fact, try running this and let me know if it works:

    function ledInit() {
      digitalPulse(LED1,1,200);
      digitalPulse(LED2,1,400);
    }
    
    E.on('init',function() {
      USB.setConsole();
      console.log('ini');
    
      ledInit();
      setTimeout(ledInit,2000);
    });
    

    for me, it only works when i run load() from command line.. on powerup, nothing happens

  • anyone?

  • Try with an extra timeout

    
    function ledInit() {
      digitalPulse(LED1,1,200);
      digitalPulse(LED2,1,400);
    }
    //function onInit() {
    E.on('init',function() {
      setTimeout(function() { // extra timeout
        USB.setConsole();
        console.log('ini');
        ledInit();
        setTimeout(ledInit,2000);
      },1500); // extra timeout (try'd 1000 but did'n work)
    }); // end E.on()
    //} // end onInit()
    
    // onInit(); remove before save
    
    
  • that worked! now can someone explain why? I also noticed that taking the 1500 delay down to 1000 fails as well - does the board inherently have some issue with 'settling' for 1.5 seconds?

  • I do not exactly know, but there something run after your E.on('init',yourAnonymousFunction(){}) that needs the time of 1500+ ms. I usually do not use the E.on('init',...), because it does not full control over what is initialized when, even though in your case it is just one registered (user) function. I use the 'old' onInit() {...} that was always there... and I would be very surprised that that would not run anymore... (@Frida has it in the code... commented: @Frida, did it work for you with old onInit()?)

    function ledInit() {
      digitalPulse(LED1,1,200);
      digitalPulse(LED2,1,400);
    }
    
    function onInit() {
        USB.setConsole();
        console.log('ini');
        ledInit();
        setTimeout(ledInit,2000);
    } // end onInit()
    // onInit();  remove or comment before uploading last time  before save()
    

    If it is not working, the console meddling could could have something to do with it...

    Important (for my use is), that I never save() dynamic initialized values or running timers..., not even watches, even though @Gordon built it the way that it - saving the state of the interpreter, configurations, timers,... are saved and restored on power up - works (...for simple things where no timing issues loom...). Sometimes I take even the port mode settings out of the upload code and put it into the initialization code...

  • i suppose i2c initialization could take longer? any way to make it asynchronous?

  • @allObjects, I allways use onInit(), but yes it needs the time too.

  • E.on('init',...) should run just before onInit, but I didn't think there was anything particularly special about it otherwise.

    What if you remove the USB.setConsole? It could be that trying to set the console device to USB before USB has initialised causes problems?

  • I tried taking out setConsole.. still seems to need a delay. would probably be a good idea to have some sort of an event for 'ready' rather than 'init' if it's an issue of internal processing time to get things going..

  • So it does work with the USB.setConsole removed?

    There's this issue on boards with USB where often people want stuff running as soon as possible, but then that's before USB has initialised properly. It used to be that I'd wait until USB had initialised but then people complained - but good call on the event. It would be a nice way around it.

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

issue with I2C on pico (initialization)

Posted by Avatar for MikeD @MikeD

Actions