gpio icon indicating copy to clipboard operation
gpio copied to clipboard

Fast Pulse

Open pacmac opened this issue 11 years ago • 5 comments

Hi;

Using a scope to monitor the pin, I seem to be unable to get a pulse faster than 10ms using the following code, but I really need the pulse width to be in microseconds rather than milliseconds, hence the setTimeout value of 0, which I was hoping would last a few clock cycles. Even if I remove the SetTimeout() function, the pulse width is still 10ms.

there is a 1K pullup on gpio.4.

The following code is intended to pull gpio4 low for 500us, then release it and then read whether the connected device has pulled the line low (presence) and log the result to the console, however I am unable to get a pulse width less than 10ms, any ideas why ??

var dq;
function dqinit(cb){
    dq = gpio.export(4, {
       ready: function() {
          dq.set(0);
          setTimeout(function() {
            dq.set();
            dq = gpio.export(4, {
               direction: "in",
               ready: function() {
                    cb(dq.value); 
               }
            });         
          },0);
       }
    }); 
}

dqinit(function(val){
    console.log(val);
});

pacmac avatar Aug 21 '13 02:08 pacmac

You actually just need to export the header once. In the ready callback, run dq.set(0, function(){ dq.set(1, function(){ cb(dq.value); }); }); I'm on my iPad so it's a bit hard to type, hope that makes sense.

EnotionZ avatar Aug 24 '13 07:08 EnotionZ

Most browsers (including Chrome) has a minimum countdown around 5-8ms with setTimeout(..., 0), which is considered an artifact of some kind of complicated theory and is yet to be resolved.

I don't know if V8 has fixed this, better use the internal Timer class in nodejs IMO.

I have a fork that takes an array of 1 and 0s for SPI bit banging, which pipes them directly into C for a way more reasonable output.

@EnotionZ Do you think µs preciseness can be done in javascript?

vicary avatar Sep 03 '13 12:09 vicary

@vicary you can. Use process.hrtime() or the microtime module. You can run as fast as your cpu clock does + T(V8 runloop) + T(your code).

kilianc avatar Sep 14 '13 21:09 kilianc

@vicary the bottleneck is probably the fact that we're using node's filesystem to write header values. I bet if we wrote a node c++ module that wraps the wiringPi library, we'd get faster timing. As @kilianc said, we can take a high resolution (nanosecond) timestamp with process.hrtime() to validate.

EnotionZ avatar Sep 19 '13 04:09 EnotionZ

I am not very sure about how gpiolib.c works, I guess it bypasses the filesystem writes? If that's true, can nodejs works directly with that layer instead of writing through the filesystem?

vicary avatar Sep 19 '13 05:09 vicary