Wrapper for existing C-Library

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
EqBooster
Posts: 9
Joined: Mon Jul 16, 2018 11:21 am

Wrapper for existing C-Library

Post by EqBooster » Mon Jul 16, 2018 12:15 pm

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

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: Wrapper for existing C-Library

Post by SpotlightKid » Mon Jul 16, 2018 10:11 pm

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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Wrapper for existing C-Library

Post by stijn » Tue Jul 17, 2018 8:06 am

EqBooster wrote:
Mon Jul 16, 2018 12:15 pm
I had some trouble with datatype convertions... How do convert between mp_uint_t and uint32_t for example?
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.
I also found a Micropython-Wrap api (https://github.com/stinos/micropython-wrap) but this only works for Unix and Windows right?
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.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Wrapper for existing C-Library

Post by mattyt » Tue Jul 17, 2018 12:45 pm

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.

EqBooster
Posts: 9
Joined: Mon Jul 16, 2018 11:21 am

Re: Wrapper for existing C-Library

Post by EqBooster » Tue Jul 17, 2018 1:06 pm

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

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Wrapper for existing C-Library

Post by dhylands » Tue Jul 17, 2018 4:24 pm

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:

Code: Select all

import gc
gc.disable()
then you would need to do something like this from C:

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);
      }
    }
Instead of using a module object, you can also call mp_load_attr with a python object (i.e. instance of a class)

EqBooster
Posts: 9
Joined: Mon Jul 16, 2018 11:21 am

Re: Wrapper for existing C-Library

Post by EqBooster » Wed Jul 18, 2018 8:11 am

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 :)

Post Reply