Share variable between C and Micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 5
Joined: Wed Mar 31, 2021 7:11 am

Share variable between C and Micropython

Post by kdelport » Mon Apr 19, 2021 2:39 pm

Hello everyone,

I succeed to compile micropython as static library on a stm32h7.

Now, I try to share variable between my C application and micropython interpreter like this :

- Create variable in micropyhton
- Do calculation on variable
- Get this variable on my C application

Which function can I call to realize this example ?

Thanks for any help ?

Posts: 615
Joined: Thu Apr 24, 2014 9:13 am

Re: Share variable between C and Micropython

Post by stijn » Mon Apr 19, 2021 6:06 pm

Bit hard to answer without more concrete information; essentially in C every MicroPython variable is going to be of type mp_obj_t. But how to best get to them depends on what/how you want to use this. I.e. are you implementing a custom type in C which getts exposed via a module? Then look at examples/usercmodule for instance. Do you just need one variable? You could use mp_load_global() from runtime.h.

Post Reply