native and viper decorators on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: native and viper decorators on ESP32

Post by Roberthh » Mon Jan 15, 2018 9:13 pm

The commit with thes assembler was this one:
https://github.com/micropython/micropyt ... d2db3ff4a3
The code for the toggle loop is here:

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)

mortbauer
Posts: 1
Joined: Wed Jan 24, 2018 7:20 am

Re: native and viper decorators on ESP32

Post by mortbauer » Wed Jan 24, 2018 7:31 am

I tried to use this on wipy, and also looked into the vanilla micropython code and it seems this has only been added for ESP8266.

Would be great however to also have this on ESP32

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

Re: native and viper decorators on ESP32

Post by Roberthh » Thu Jan 25, 2018 6:15 am

Would be great however to also have this on ESP32
Agreed. But this option is also available on Pyboard, maybe general the SMT32 branch.

__deets__
Posts: 23
Joined: Sun Aug 20, 2017 4:50 pm

Re: native and viper decorators on ESP32

Post by __deets__ » Fri Feb 02, 2018 2:26 pm

Just a short heads up: yes it works, thank you. The speed gains were measurable, however the overall system performance is still not where I need it. So I consider going hybrid. I've seen dhylands fork and will try to replicate that.

mosi
Posts: 28
Joined: Tue Oct 07, 2014 12:07 am

Re: native and viper decorators on ESP32

Post by mosi » Thu Mar 15, 2018 10:19 am

1) Compiled micropython loboris on ESP32 WROVER
2) Enabled support for inline xtensa assembler in mpconfigport.h
#define MICROPY_EMIT_INLINE_XTENSA (1)
3) tried this simple code, ( nop() operation is not supported it seems )

fail.
Any ideas?

>>> @micropython.asm_xtensa
... def adcw(a2,a3):
... sub(a2, a2, a3)
...
>>> adcw(2,3)
Guru Meditation Error: Core 1 panic'ed (InstrFetchProhibited)
. Exception was unhandled.
Core 1 register dump:
PC : 0x3f813ae0 PS : 0x00060730 A0 : 0x800ec894 A1 : 0x3ffb8130
A2 : 0x3f8137f0 A3 : 0x00000002 A4 : 0x3f813ae0 A5 : 0x3ffb822c
A6 : 0xffffffc0 A7 : 0x00000001 A8 : 0x800f1595 A9 : 0x3ffb8100
A10 : 0x00000002 A11 : 0x00000003 A12 : 0x00000002 A13 : 0x00000002
A14 : 0x00000000 A15 : 0x00000000 SAR : 0x0000000c EXCCAUSE: 0x00000014
EXCVADDR: 0x3f813ae0 LBEG : 0x400996ac LEND : 0x400996b7 LCOUNT : 0x00000000

Backtrace: 0x3f813ae0:0x3ffb8130 0x400ec891:0x3ffb8150 0x400fa8c3:0x3ffb8170 0x400f1452:0x3ffb8210 0x400ec891:0x3ffb8280 0x400ec8be:0x3ffb82a0 0x400de455:0x3ffb82c0 0x400de6ed:0x3ffb8370 0x400d7ba0:0x3ffb83b0

CPU halted.

yafengabc
Posts: 8
Joined: Thu Mar 08, 2018 8:38 am

Re: native and viper decorators on ESP32

Post by yafengabc » Fri Mar 30, 2018 2:43 pm

this is a awesome function!
I tested this function on my esp8266 board

import time
import machine
import micropython
machine.freq(160000000)
def loop1():
t2=0
for i in range(5):
t=time.ticks_us()
for i in range(100):
pass
t2=time.ticks_us()
print(t2-t)
time.sleep(5)

@micropython.native
def loop2():
t2=0
for i in range(5):
t=time.ticks_us()
for i in range(100):
pass
t2=time.ticks_us()
print(t2-t)
time.sleep(5)
print("loop1")
loop1()

print("loop2")
loop2()

and the result:
loop1
2353
2367
2345
2384
2345
loop2
159
158
159
159
159

14x faster and better real-time performance!

Unfortunately, eps32 firmware does not have this feature,hope that esp32 can increase this function very much

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

Re: native and viper decorators on ESP32

Post by pythoncoder » Sat Mar 31, 2018 8:55 am

Native and Viper are fast, but I'm wary of empty loops as benchmarks. I have known optimising compilers be clever enough to optimise them out of existence and generate no code. I don't know if MicroPython does this but I always make the loop do something, such as increment a global.

Incidentally it's best practice to use utime.ticks_diff rather than subtraction to difference times, for reasons you'll find in the docs.
Peter Hinch
Index to my micropython libraries.

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: native and viper decorators on ESP32

Post by OutoftheBOTS_ » Sat Mar 31, 2018 12:43 pm

Ok this is all new to me, I hadn't heard of native or viper emitters.

I am trying to get my head around exactly what they do and how they work. Am I right in thinking the tell the compiler to compile the code different that makes it run faster??

If so why don't we only compile in this faster way?? What's the down side and where is it at it's most advantage??

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: native and viper decorators on ESP32

Post by loboris » Sat Mar 31, 2018 3:08 pm

OutoftheBOTS_ wrote:
Sat Mar 31, 2018 12:43 pm
Ok this is all new to me, I hadn't heard of native or viper emitters.
Look at documentation, The Native code emitter.

Not supported on ESP32.

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

Re: native and viper decorators on ESP32

Post by Roberthh » Sat Mar 31, 2018 3:32 pm

But even where it is supported, it supports only a subset of MicroPython, native more than viper. Native code runs in average twice as fast as the standard emitter, viper code is about 10-20 timer faster than standard Python code. Some discussion was in that post for ESP8266
viewtopic.php?f=16&t=2766&hilit=toggle and this one for PyBoard viewtopic.php?f=2&t=1349

Post Reply