-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
esp32: proof of concept for stubs to call platform-dependent functions #5653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Some further thoughts... Creating simple stubs to allow esp-idf functions to be called from native modules means that there is still python->C glue code that someone has to write somewhere. If the native code is complex and calls some esp-idf functions in the middle then there's no real way around having to hand-code the python->C glue. However, if drivers can be written in 99% python code plus 1% of more or less direct calls into esp-idf then it's a pain to have to hand write the glue code. As simple example: the
The current proposal makes it easy to generate the stubs 'cause one doesn't have to provide type information, but what if one could? Then we could generate stubs that are directly callable from python! E.g., suppose
where the tuple specifies the function name, the input types (in
|
There are a lot of things that could be discussed in this context, but let me start with just a few.
It should in principal be possible to do this by just adding the new functions to
IMO this is the "right" way to do it. But instead of changing the firmware's loader/linker (which would get quite complex to support new linking mechanisms) I'd suggest to improve
This is very similar to ffi (see unix ffi module) and what ctypes does. But for the IDF it'd be static. So really what it could be is just an (automatically generated) |
I hope that's a positive statement ;-)
It doesn't work in practice because
👍
Do you have an example for any arch that produces such relocation entries ("a relocation entry to the .mpy that fixes this label up during import"). Or can you point me at a couple of relevant lines of code in mpy_ld or the dyn linker? Just a starting point for me to understand...
I'll take a look at the ffi stuff, but generating an espidf module doesn't sound too complicated for a manually provided list of entry points. I'm not sure how I would extract a list of "all" functions given how everything is spread across include directories... |
In |
@dpgeorge I looked at the code some more and need a decision from you. |
op==5 is already used. Maybe op==126? |
Hmmmm, given https://github.com/micropython/micropython/blob/master/tools/mpy_ld.py#L771-L775 can you tell me how op codes 3..5 can be "already used"? |
dest=2, so 2*2+1=5? |
df20420
to
d08a5c5
Compare
OK, I implemented the relocation using op=126. Seems to work nicely :-) |
I added a partial implementation of generating an espidf module to call the esp-idf functions from python and then implemented |
Closing in favor of #5711 |
Notify based on issue label
This PR is a proof-of-concept for creating stubs that allow dynamically loaded native modules (.mpy files) to call platform-dependent functions. The goal is to make it easy to write native modules that call ESP-IDF functions by just adding the names of the needed functions to a list and is inspired by #5618, #5641, and #5643. Overall the approach taken here functions very similarly to the
mp_fun_table
with the following differences:ports/esp32/plat_relo.py
),More details about the functioning can be found in
ports/esp32/plat_relo.py
. I am assuming that there will be some discussion about merging this stuff with themp_fun_table
.A completely different approach that I did not investigate in detail would be to enhance the dynamic linker in the firmware so it can fix up the targets of the
CALL
instructions, i.e., perform proper linking instead of the whole stub stuff. It shouldn't be difficult because as far as I can tell external calls always use a 32-bit constant stored in a memory word. If that approach is desired I could put together a list of "where do I implement X" questions and then give it a shot.