• Sorry I'm a bit late to this. Basically Espruino's JS execution speed isn't that fast because it's interpreted.

    Looking at the code I've seen I think there are a few things you could do to really improve speed:

    • Inline all the SPI reads/data conversion into one function
    • If you can read all the data from the FIFO with one long SPI read (rather than the ~16 1 byte commands you have currently) then that would be a million times better. It should be possible - that's the whole point of the FIFO really, but I can't see any examples of anyone doing that :(
    • Failing that you can at the very least read 2 bytes at once with something like SPI1.send([FIFO_DATA_OUT_H|READ,0,0],CS)­.slice(1)
    • Add "compiled" at the top of that JS function (http://www.espruino.com/Compilation) - assuming you're just using simple JavaScript that will cause your function to be sent off to the Espruino server, compiled, then uploaded - it should be way faster. It may not actually be needed though with the other changes.

    Because you're doing the same command over and over, you can also use bind which basically works out the arguments and creates a new function that contains them. Here's some code that should be pretty fast

    var FIFO_READ_CMD = SPI1.send.bind(SPI1,[FIFO_DATA_OUT_H|REA­D,0,0],CS);
    var data = new Uint8Array(14);
    var sdata16 = new Int16Array(data.buffer);
    var udata16 = new Uint16Array(data.buffer);
    
    function getData() {
      "compiled"  
      var f = FIFO_READ_CMD;
      data.set(f().slice(1),0);
      data.set(f().slice(1),2);
      data.set(f().slice(1),4);
      data.set(f().slice(1),6);
      data.set(f().slice(1),8);
      data.set(f().slice(1),10);
      data.set(f().slice(1),12);
    }
    
    // call getData and then read signed 16 bit data from sdata16, or unsigned from udata16
    

    But I'm struggling to see the complete code you're using at the moment so I can't be sure this will do exactly what you want. Using the same form you should be able to modify it to do what you need though.

About

Avatar for Gordon @Gordon started