Hello guys,
I am trying to understand how i can extend the micropython firmware with additional modules. I have some existing C-Libraries for the STM32F4 Discovery Board, and my goal is to make it accessible from python. What are possibilities for me to do this? I already successfully extended the firmware with some basic functions using this example: https://github.com/dhylands/micropython ... 57f5d7281b
I had some trouble with datatype convertions... How do convert between mp_uint_t and uint32_t for example?
But I don't really understand how i can use an existing .c file without changing it. I guess i need to write a wrapper who further delegates the methods. Has anyone done something like this and can give me some tips how i should start or where i can find an example?
I also found a Micropython-Wrap api (https://github.com/stinos/micropython-wrap) but this only works for Unix and Windows right?
Can someone help me please or give me a hint how i should start on this? Sorry for all dumb questions but i am new, but i really want to get this working
Thanks in advance
EqBooster
Wrapper for existing C-Library
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: Wrapper for existing C-Library
This video was posted somewhere else on this forum already. The generic title hides the fact that it contains a fairly good and to the point overview of how to wrap a C function as a MicroPython module:
"microPython for ESP32"
linux conf au 2017 - Hobart, Australia
Published on Jan 18, 2017
Nick Moore
https://youtu.be/-MrqCmq3Z5k?t=1069
Note that this talk was given before the repository layout was streamlined and all ports moved into the "ports" directory.
"microPython for ESP32"
linux conf au 2017 - Hobart, Australia
Published on Jan 18, 2017
Nick Moore
https://youtu.be/-MrqCmq3Z5k?t=1069
Note that this talk was given before the repository layout was streamlined and all ports moved into the "ports" directory.
Re: Wrapper for existing C-Library
If you look in the micropython-wrap repository the files topyobj.h and frompyobj.h have pretty much all conversions (also lists and tuples etc), that should give you a pretty good idea.
Since it compiles with gcc, I don't think there should be major problems using it for other ports using gcc. It does require the standard C++ library however, though theoretically it's possible to strip that out.I also found a Micropython-Wrap api (https://github.com/stinos/micropython-wrap) but this only works for Unix and Windows right?
Re: Wrapper for existing C-Library
We recently recorded Damien giving a talk titled Wrap a C module in MicroPython at our June Melbourne MicroPython Meetup. Sounds relevant for what you're asking!
@deshipu also wrote some MicroPython Development Documentation that covered similar ground.
@deshipu also wrote some MicroPython Development Documentation that covered similar ground.
Re: Wrapper for existing C-Library
Thanks a lot for all your responses. I think i start understanding how this works now.
The next thing i was looking for is how to this the other way around (Accessing python obj, calling functions... from C)
I found some examples and tried to implement it myself but i had some trouble. (Especially with return and param values)
The only thing that worked so far was by calling the python function with a callback, implemented like this:
https://github.com/dhylands/micropython ... 57f5d7281b)
But how can i do this without setting the callback variable? How can i directly call python functions from c? I am sorry for these dumb questions and i know there are several examples online already but i have trouble because i am a beginner in this sector and everything is new for me.
Thanks in advance
EqBooster
The next thing i was looking for is how to this the other way around (Accessing python obj, calling functions... from C)
I found some examples and tried to implement it myself but i had some trouble. (Especially with return and param values)
The only thing that worked so far was by calling the python function with a callback, implemented like this:
https://github.com/dhylands/micropython ... 57f5d7281b)
But how can i do this without setting the callback variable? How can i directly call python functions from c? I am sorry for these dumb questions and i know there are several examples online already but i have trouble because i am a beginner in this sector and everything is new for me.
Thanks in advance
EqBooster
Re: Wrapper for existing C-Library
To call python functions from C, you need to get a pointer to the function by looking it up.
If you wanted to call a particular function from a module, then you need to get a module reference, and then lookup the function. For example, to do the equivalent of this python code: then you would need to do something like this from C:
Instead of using a module object, you can also call mp_load_attr with a python object (i.e. instance of a class)
If you wanted to call a particular function from a module, then you need to get a module reference, and then lookup the function. For example, to do the equivalent of this python code:
Code: Select all
import gc
gc.disable()
Code: Select all
mp_obj_t gc_module_obj = mp_module_get(MP_QSTR_gc);
if (gc_module_obj) {
mp_obj_t gc_disable_fn = mp_load_attr(gc_module_obj, MP_QSTR_disable);
if (gc_disable_fn) {
mp_call_function_0(gc_disable_fn);
}
}
Re: Wrapper for existing C-Library
Thanks for your fast response dhylands, i got it working now. I had some stupid mistakes with imports before
My actual goal is to wrap a existing CAN-stack written in C, to use it with micropython. My next goal was to just call python functions out of c. The reason for this is because i don't want to rely on the port and just call the abstract functions from the python CAN module for example.
Thank you for taking your time and i appreciate every kind of help
My actual goal is to wrap a existing CAN-stack written in C, to use it with micropython. My next goal was to just call python functions out of c. The reason for this is because i don't want to rely on the port and just call the abstract functions from the python CAN module for example.
Thank you for taking your time and i appreciate every kind of help