native and viper decorators on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
honza.klu
Posts: 3
Joined: Sun Sep 03, 2017 4:57 pm

native and viper decorators on ESP32

Post by honza.klu » Sun Jan 07, 2018 12:06 pm

I tried to use @micropython.native and @micropython.viper decorators on esp32 build of micropython, but it is apperently not enabled. Does anyone have idea how to make it work?

I found out that one of these define should be probably enabled to enable these features.

#define MICROPY_EMIT_NATIVE (MICROPY_EMIT_X64 || MICROPY_EMIT_X86 || MICROPY_EMIT_THUMB || MICROPY_EMIT_ARM || MICROPY_EMIT_XTENSA)

I tried to enable MICROPY_EMIT_ARM or MICROPY_EMIT_XTENSA, but compilation fail with this error:

./../py/nativeglue.c:158:5: error: 'nlr_push' undeclared here (not in a function)

I think this root problem is that nlr_push is macro, not function so it can't be placed into array (void *const mp_fun_table[MP_F_NUMBER_OF])

#define nlr_push(buf) (nlr_push_tail(buf), setjmp((buf)->jmpbuf))
Last edited by honza.klu on Sun Jan 07, 2018 5:41 pm, edited 2 times in total.

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 » Sun Jan 07, 2018 5:29 pm

I rather think these are currently only supported on STM processors.
Peter Hinch
Index to my micropython libraries.

honza.klu
Posts: 3
Joined: Sun Sep 03, 2017 4:57 pm

Re: native and viper decorators on ESP32

Post by honza.klu » Sun Jan 07, 2018 5:33 pm

It is a pity, but It explain why it can't be enabled (-:
Thank you for your response.

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 » Sun Jan 07, 2018 7:38 pm

The viper emitter is also supported by the ESP8266 port.

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

Re: native and viper decorators on ESP32

Post by __deets__ » Sun Jan 14, 2018 5:02 pm

Roberthh wrote:
Sun Jan 07, 2018 7:38 pm
The viper emitter is also supported by the ESP8266 port.
I've just flashed esp8266-20171101-v1.9.3.bin but no native nor viper decorations are available.

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 » Sun Jan 14, 2018 5:37 pm

That's strange, because I'm using it. What is the result with that simple test case:

Code: Select all

# simple test case viper
@micropython.viper
def add(a,b):
    return a+b
# simple test case native
@micropython.native
def sub(a,b):
    return a-b
The RAM memory for native and viper code is by default VERY limited. You can change that, but have to build a new image, which reserved extra space in flash. But even then the size is limited, because is has to reside in the lower 1 MByte of flash.

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

Re: native and viper decorators on ESP32

Post by __deets__ » Sun Jan 14, 2018 8:38 pm

Here you go:

Code: Select all

MicroPython v1.9.3-8-g63826ac5c on 2017-11-01; ESP module with ESP8266
Type "help()" for more information.
>>> from micropython import *
>>> native
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'native' is not defined
>>> viper
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'viper' is not defined

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 » Sun Jan 14, 2018 9:13 pm

@__deets__ It does not work that way. micropython is a specific built-in module in MicroPython, which contains a few methods: ['__name__', 'const', 'opt_level', 'mem_info', 'qstr_info', 'stack_use', 'alloc_emergency_exception_buf', 'heap_lock', 'heap_unlock', 'kbd_intr', 'schedule']. If you are interested aboutu these, look into the docs.
The function decorator @micropython.viper does not specifiy one of these methofs. I suggest taht you simply try to run the small sample I gave you. If that passes (it defines the dummy functions add and sub), it means that native and viper code is supported. What you not see is how it is processed. Instead of bytecode, machine code is generated fo the body of these functions. You can tell by the speed, if for instance you write a function which toggles a port. Example:

Code: Select all

def toggle():
    import machine
    pdsckPin = machine.Pin(4, machine.Pin.OUT, value=0)
    isr = machine.disable_irq()
    do_toggle()
    machine.enable_irq(isr)

@micropython.viper
def do_toggle():

    GPIO_OUT = ptr32(0x60000300) # GPIO Base register
    for i in range(100):
        GPIO_OUT[1] = 0x10 # set bit 4
        GPIO_OUT[2] = 0x10 # clear bit 4
This code toggles Pin 4 100 times. If you want to see the difference, write the same with pure python and watch is.

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 » Mon Jan 15, 2018 8:00 am

Some interesting observations there, Robert. Firstly that Viper and Native are supported on ESP8266 and ESP32. Secondly that the names viper and native don't appear in dir(micropython) - in the case of a normal decorator you would expect to see them. In fact you can use @micropython.native without first importing the module. :!:
The RAM memory for native and viper code is by default VERY limited.
Do you have a rough idea what this means in terms of LOC?
Peter Hinch
Index to my micropython libraries.

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:31 am

Hello Peter,
I do not know whether native/viper is supported in the ESP32 port. I just used that in the ESP8266 version. But you can tell yourself, at least, whether the decorators are accepted. I did not mention, that the esp8266 port also supports assembly code. I can send some examples tonight. Somewhere in one of the commit statements of the repository is also a documentation.
As of code size: The available RAM is like 500 bytes, as I remember. That would be only two handful source lines. You have to try. if the code is too large, you'll get an error message. There was the discussion to add more space between the firmware and the files system in flash, which could be extended with the RESERVED_SECS setting in flashbdev.py. It is somewhere buried in the forum or the github discussion trails, and I forgot the details. Native/viper/assymbly is anhow most interesting if the code runs from RAM. Under that aspect, the ESP32 might offer a larger code size.
Edit: more info here: http://docs.micropython.org/en/latest/e ... e_location

Post Reply