Page 1 of 1

μs timing for pins

Posted: Wed Jan 18, 2017 3:01 am
by cabalist
Hello,

I am working on an application that will need to send a 5.25μs +/- .25μs pulse to an old piece of hardware.

I have not yet tried this on an oscilloscope but I am curious if anyone has any experience with the timing resolution of writing to esp8266 pins with micropython. On an arduino there is a fair amount of overhead that is worked around. [see here: http://www.billporter.info/2010/08/18/r ... uino-pins/] Are there similar caveats for micropython? Am I expecting too much? Or being too cautious?

Thanks for your insight!

Re: μs timing for pins

Posted: Wed Jan 18, 2017 6:13 am
by Roberthh
Some observations and sample code around that aspect are here: http://forum.micropython.org/viewtopic. ... gle#p16445
Summary: Yes it's possible. The caveat I've seen: If you execute a certain piece of code in ESP8266 the first time, e.g. the first run of a loop, it's execution time can me much slower. In the sample non-viper code, typically the ifrst pulse was substantially longer.

Re: μs timing for pins

Posted: Wed Jan 18, 2017 6:27 am
by cabalist
This is gold. Thanks you so much. :)

Re: μs timing for pins

Posted: Wed Jan 18, 2017 4:35 pm
by pythoncoder
There is support for inline assembler if you need very tight timing: https://github.com/micropython/micropython/issues/2695.

Re: μs timing for pins

Posted: Wed Jan 18, 2017 7:35 pm
by Roberthh
In this toggle test i used also an assembler version, which was not faster, but far less readable.

Code: Select all

def loop():
    import machine
    machine.Pin(4, machine.Pin.OUT, value=0)
    do_loop()
    print("loop done")

@micropython.asm_xtensa
def do_loop():
    movi(a2, 0x60000300) # GPIO Base address
    movi(a5, 0x10) # set/clear bit 4
    movi(a3, 10) # loop count
    movi(a4, 1) # loop decrement
    label(loop_start)
    s8i(a5, a2, 4)
    s8i(a5, a2, 8)
    sub(a3, a3, a4)
    bnez(a3, loop_start)

Re: μs timing for pins

Posted: Thu Jan 19, 2017 7:23 am
by pythoncoder
Interesting. Viper is evidently very efficient :D

Re: μs timing for pins

Posted: Thu Jan 19, 2017 8:01 am
by Roberthh
In my previous test on Pyboard, viper took about two times longer than assembler, mostly becaus it does not keep local values in registers. That is still very good. In this example I guess that the limiting factor is the port access, which seems to be timed to ensure proper signals.