• Hmm... I was sure I'd posted this up a few days ago, but maybe I forgot to click submit:

    function saveToSD(filename) {
      var addr = process.memory().flash_code_start;
      var len = process.memory().flash_start + process.memory().flash_length - addr;
      var f = E.openFile(filename,"w");
      var block = new Uint32Array(512);
      addr -= 4;
      while (len>0) {
        block = block.map(function(v){return peek32(addr+=4);});
        f.write(block.buffer);
        len -= 2048;
      }
      f.close();
    }
    
    function loadFromSD(filename) {
      var addr = process.memory().flash_code_start;
      var len = process.memory().flash_start + process.memory().flash_length - addr;
      var rom = require("STM32F1Flash");
      rom.unlock();
      console.log("Erasing...");
      for (var i=0;i<len;i+=2048) {
        rom.erase(addr+i);
      }
      console.log("Writing...");
      var f = E.openFile(filename,"r");
      var block = new Uint32Array(512);
      addr -= 4;
      while (len>0) {
        console.log("0x"+(addr+4).toString(16));
        new Uint8Array(block.buffer).set(f.read(2048));
        block.forEach(function(v){rom.write(addr+=4,v);});
        len -= 2048;
      }
      f.close();
    }
    

    So:

    • Get your program as you want it
    • Call save()
    • Load this script into Espruino and run saveToSD('myfile.bin');

    Then to load in a new Espruino:

    • Load the script and run loadFromSD('myfile.bin')

    So if you wanted to allow Espruino to auto-update, you'd make sure that the code that you saved on to flash contained something like this:

    function loadFromSD(filename) {
      var addr = process.memory().flash_code_start;
      var len = process.memory().flash_start + process.memory().flash_length - addr;
      var rom = require("STM32F1Flash");
      rom.unlock();
      console.log("Erasing...");
      for (var i=0;i<len;i+=2048) {
        rom.erase(addr+i);
      }
      console.log("Writing...");
      var f = E.openFile(filename,"r");
      var block = new Uint32Array(512);
      addr -= 4;
      while (len>0) {
        console.log("0x"+(addr+4).toString(16));
        new Uint8Array(block.buffer).set(f.read(2048));
        block.forEach(function(v){rom.write(addr+=4,v);});
        len -= 2048;
      }
      f.close();
    }
    
    function onInit() {
      if (MYGPIO.read()) {
        LED1.set();
        loadFromSD('update.bin');
        LED1.reset();    
        LED2.set();    
      }
    }
    

    and then the process is:

    • Insert card
    • connect MYGPIO to 3.3v
    • reset Espruino
    • Wait until the red LED goes out and the green one lights
    • connect MYGPIO to 0v
    • reset Espruino
About

Avatar for Gordon @Gordon started