μs timing for pins

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
cabalist
Posts: 7
Joined: Thu Jun 02, 2016 7:37 pm

μs timing for pins

Post by cabalist » Wed Jan 18, 2017 3:01 am

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!

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: μs timing for pins

Post by Roberthh » Wed Jan 18, 2017 6:13 am

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.

cabalist
Posts: 7
Joined: Thu Jun 02, 2016 7:37 pm

Re: μs timing for pins

Post by cabalist » Wed Jan 18, 2017 6:27 am

This is gold. Thanks you so much. :)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: μs timing for pins

Post by pythoncoder » Wed Jan 18, 2017 4:35 pm

There is support for inline assembler if you need very tight timing: https://github.com/micropython/micropython/issues/2695.
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: μs timing for pins

Post by Roberthh » Wed Jan 18, 2017 7:35 pm

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)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: μs timing for pins

Post by pythoncoder » Thu Jan 19, 2017 7:23 am

Interesting. Viper is evidently very efficient :D
Peter Hinch
Index to my micropython libraries.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: μs timing for pins

Post by Roberthh » Thu Jan 19, 2017 8:01 am

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.

Post Reply