Hi there,
I want to optimize some of my code using C.
There are some examples already, and this really helped me to already write some of my code in C.
https://github.com/micropython/micropyt ... es/natmod/
The problem is I want to be able to use some pins to use as in- or outputs, but I can't really find how to control the pins from C.
Does anyone here know where to find an example of how to control the pins? Thanks in advance!
ESP32 C Example for controlling GPIO
Re: ESP32 C Example for controlling GPIO
How to provide access to low-level device functionality from native modules is sort of an active area of discussion/development. Especially on ESP32. Because the dynamic linker is so simple (for code size reasons), there are only a very limited set of symbols provided in the main that call be accessed from dynamic native modules.
See https://github.com/micropython/micropython/pull/5711 for some recent discussion about making ESP IDF functionality available.
But, perhaps if all you want to do is control pins then you might be able to do this via the Python-level APIs (i.e. call the methods on the Pin objects as if you were the runtime). Which may not get you the optimisation you were hoping for. Ultimately you want to be able to use mp_load_attr / mp_call_function_... to access the methods etc, but I just checked and mp_load_attr doesn't seem to be available in a dynamic native module.
I guess you could approximate this using mp_load_global to get the getattr function, but that's getting a bit crazy.
I might have to do some more research... In the meantime I guess you could get this working directly in the firmware rather than as a loadable module (e.g. using http://docs.micropython.org/en/latest/d ... dules.html ).
Also, I'm guessing @native / @viper didn't help enough?
See https://github.com/micropython/micropython/pull/5711 for some recent discussion about making ESP IDF functionality available.
But, perhaps if all you want to do is control pins then you might be able to do this via the Python-level APIs (i.e. call the methods on the Pin objects as if you were the runtime). Which may not get you the optimisation you were hoping for. Ultimately you want to be able to use mp_load_attr / mp_call_function_... to access the methods etc, but I just checked and mp_load_attr doesn't seem to be available in a dynamic native module.
I guess you could approximate this using mp_load_global to get the getattr function, but that's getting a bit crazy.
I might have to do some more research... In the meantime I guess you could get this working directly in the firmware rather than as a loadable module (e.g. using http://docs.micropython.org/en/latest/d ... dules.html ).
Also, I'm guessing @native / @viper didn't help enough?
Re: ESP32 C Example for controlling GPIO
I'll find out about getting these added to dynruntime.h But in the meantime you can just add
#define mp_load_attr(obj, attr) mp_fun_table.load_attr(obj, attr)
#define mp_load_method(base, attr, pdest) mp_fun_table.load_method(base, attr, pdest)
to the top of your C file.
#define mp_load_attr(obj, attr) mp_fun_table.load_attr(obj, attr)
#define mp_load_method(base, attr, pdest) mp_fun_table.load_method(base, attr, pdest)
to the top of your C file.
Re: ESP32 C Example for controlling GPIO
I have a similar problem as Mathijs. How to interact with the hardware from a native module (mpy) that can be dynamic loaded?
It seems possible if you include the module in the build of micropython itself. But I cannot include py/gpio.h in a dynamic module?
All examples in the natmod do not interact with hardware.
It seems possible if you include the module in the build of micropython itself. But I cannot include py/gpio.h in a dynamic module?
All examples in the natmod do not interact with hardware.