You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Thanks - yes, it does look bad. Just looked at this and it seems to be a problem with the STM32's cache grabbing more data while reading from analog.

    Try poke32(0x40023C00, peek32(0x40023C00)&~256); - you'll have to do it on onInit if you want it to stay working between power cycles - it disables prefetch.

    For instance, with B1 shorted to 3.3v:

    function test(pin) {
      var a = new Uint16Array(1000);
      var mean = 0;
      for (var i=0;i<a.length;i++) {
        a[i] = analogRead(pin)*4096;
        mean += a[i];
      }
      mean /= a.length;
      print("Mean: "+mean);
      var variance = 0;
      for (var i=0;i<a.length;i++) {
        var d = a[i]-mean;
        variance += d*d;
      }
      variance /= a.length;  
      print("Variance: "+variance);
    }
    
    poke32(0x40023C00, peek32(0x40023C00)|256);
    console.log("With prefetch");
    test(B1);
    poke32(0x40023C00, peek32(0x40023C00)&~256);
    console.log("Without prefetch");
    test(B1);
    
    //With prefetch
    //Mean: 4080.495
    //Variance: 309.49997499999
    //Without prefetch
    //Mean: 4091.12
    //Variance: 49.1316
    

    So yeah, massive difference. I need to see how it affects performance before I put it in the actual firmware - I guess it's possible it could just be disabled while doing the analog read, but I wonder whether that would be good enough.

About

Avatar for Gordon @Gordon started