Micropython 1.9.3 - 如何将 .py @micropython.native 代码编译成 .mpy?

Micropython 1.9.3 - How to compile .py @micropython.native code into .mpy?

我在 Micropython 1.9.3。我知道如何使用mpy-cross把一个.py变成编译好的python.mpy,可以被Micropython虚拟机执行。

问题是,如果我尝试使用 @micropython.native 进行编译,即将 Python 脚本编译为本机代码而不是字节码,我会得到一个错误:

../../mpy-cross/mpy-cross -o build/frozen_mpy/./frozentest.mpy -s frozentest.py  frozentest.py

ValueError: can only save bytecode

在下面.py

@micropython.native
def native_add(a,b):
    return (a+b)

c = native_add(2342,4542)

问题

是否无法以 .mpy 格式嵌入本机代码?我错过了 mpy-cross/mpconfigport.h 中的某些选项吗?

我唯一改变的是:

#define MICROPY_EMIT_THUMB (0) // changed it to 1

我从 micropython forum 上的某人那里得到了答案:

You cannot. It is an TODO item. If you want to put it into flash memory, you can embed it as frozen source code in some ports. Just put these files in a subdirectory called scripts, like esp8266/scripts or stm32/scripts. But it will still be compiled at import time and consume RAM. Typically, that should not hurt, when this variant of coding is used only for small, time-critical sections of the code.