ESP32 C Example for controlling GPIO

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Mathijs
Posts: 1
Joined: Wed Apr 01, 2020 11:33 am

ESP32 C Example for controlling GPIO

Post by Mathijs » Wed Apr 01, 2020 11:39 am

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!

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32 C Example for controlling GPIO

Post by jimmo » Wed Apr 01, 2020 1:55 pm

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?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ESP32 C Example for controlling GPIO

Post by jimmo » Wed Apr 01, 2020 2:15 pm

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.

User avatar
Klaas
Posts: 4
Joined: Tue Dec 15, 2020 11:23 am

Re: ESP32 C Example for controlling GPIO

Post by Klaas » Tue Dec 15, 2020 2:51 pm

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.

Post Reply