How to call functions from other binary modules via uctypes?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ralienpp
Posts: 3
Joined: Wed Oct 26, 2016 2:05 pm
Contact:

How to call functions from other binary modules via uctypes?

Post by ralienpp » Wed Oct 26, 2016 2:29 pm

I would like to combine Python's expressive syntax with the capabilities of existing libraries written in C. It is clear how I can do that with Python and ctypes, but having examined the documentation of uctypes in Micropython, I did not see any references to calling functions in other modules, it is only a way to define C-like data structures.

Can it be done in principle? Or is this beyond the scope of uctypes? Perhaps there is another way to do this?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: How to call functions from other binary modules via uctypes?

Post by pfalcon » Wed Oct 26, 2016 2:40 pm

We have examples/ folder in the source tree, with examples on various uPy usages, which is useful to consider besides the documentation. https://github.com/micropython/micropyt ... example.py in particular should answer your question.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

ralienpp
Posts: 3
Joined: Wed Oct 26, 2016 2:05 pm
Contact:

Re: How to call functions from other binary modules via uctypes?

Post by ralienpp » Wed Oct 26, 2016 3:03 pm

Thank you for the swift answer. Going through the examples directory I found a promising sample: https://github.com/micropython/micropyt ... example.py

I am posting an excerpt here for the convenience of others who might be looking for the same thing; I tested it successfully on my computer (where I built and ran uPy):
```
import ffi
libc = ffi.open("libc.so.6")
perror = libc.func("v", "perror", "s")
perror("ffi before error")
```

My other question is about the availability of `ffi` in contexts other than the unix port, is it supposed to work in FreeRTOS too? The intention is to run uPy as a task on FreeRTOS and use the functionality of some existing libraries.

I am a beginner in matters of such low-level programming, so I may be asking some silly questions. At this point I am assuming that on FreeRTOS I would be able to run uPy as a task and call functions from other binary modules via ffi.


p.s. Can you double-check the link you posted? It seems to point to this topic.

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: How to call functions from other binary modules via uctypes?

Post by pfalcon » Wed Oct 26, 2016 3:29 pm

Yep, sorry, wrong clipboard paste, I meant the example you found.

Module "ffi" works for systems which support POSIX-compliant dynamically loadable modules. So, the short answer whether it'll work somewhere else, where such capabilities not available is "no". Longer answer is that you may look at making it work somehow, based on your knowledge of what exactly you want to achieve and whether you platform allows for that.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

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

Re: How to call functions from other binary modules via uctypes?

Post by dhylands » Wed Oct 26, 2016 3:35 pm

For the bare-metal ports (like pyboard) you'll need to link the C code into the firmware and then create a python interface to it, like is done for the other modules.

This thread has some examples in it for bare-metal: http://forum.micropython.org/viewtopic.php?f=2&t=1411

Post Reply