Writing new C API into micropython firmware

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Firas_Baccouri
Posts: 35
Joined: Wed Apr 27, 2022 7:22 am

Writing new C API into micropython firmware

Post by Firas_Baccouri » Wed Jun 15, 2022 8:45 am

Hello guys ,
I want to implement a C API into the micropython firmware and then using it from the python software ,
could anyone help me about :
*Where exactly should I inject the C code inside the micropython firmware
*how could I use it with the python software .
Thank you in advance

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Writing new C API into micropython firmware

Post by jimmo » Mon Jun 20, 2022 5:11 am

Firas_Baccouri wrote:
Wed Jun 15, 2022 8:45 am
I want to implement a C API into the micropython firmware and then using it from the python software ,
The easiest way is with "User C Modules". See https://docs.micropython.org/en/latest/ ... ython.html

Firas_Baccouri
Posts: 35
Joined: Wed Apr 27, 2022 7:22 am

Re: Writing new C API into micropython firmware

Post by Firas_Baccouri » Mon Jun 20, 2022 10:35 am

Thank you Jimmo for your reply ,
yeah I have figured that and I am into it .

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: Writing new C API into micropython firmware

Post by KJM » Tue Jun 21, 2022 1:13 am

Just curious if anybody knows of a way to access C code without having to recompile upython? Maybe something like this https://github.com/micropython/micropyt ... /index.rst technique for adding assembler to a .py file?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Writing new C API into micropython firmware

Post by jimmo » Tue Jun 21, 2022 1:42 am

KJM wrote:
Tue Jun 21, 2022 1:13 am
Just curious if anybody knows of a way to access C code without having to recompile upython?
The problem here is that to access C code you need
a) the actual c code to be linked into the firmware
b) a way to resolve the symbols at runtime

See some history (specific to accessing ESP32 IDF functions) here:
https://github.com/micropython/micropython/issues/5458
https://github.com/micropython/micropython/pull/5653
https://github.com/micropython/micropython/pull/5682

Firas_Baccouri
Posts: 35
Joined: Wed Apr 27, 2022 7:22 am

Re: Writing new C API into micropython firmware

Post by Firas_Baccouri » Tue Jun 21, 2022 5:22 am

Jimmo , do you know how can I use the C data types with my C module , it looks strange but I am into building my own SPI (SPI HandMade) so I need to use some uint8_t but I couldn´t do that because I need to initiate the functions I have built with MP_DEFINE_CONST_FUN_OBJ_X(obj_name, fun_name) and that function can´t have a function as a parameter that takes an uint as a parameter (initialization of 'void * (*)(void *, void *)' from incompatible pointer type 'void * (*)(uint8_t *, void *)' ) so I didn´t know how should I porceed with that . I have tried to use an array of mp_int_t but it doesn´t suit too because it will be considered as a double pointer and that function (MP_DEFINE_CONST_FUN_OBJ_X(obj_name, fun_name) ) expect a function that take a void * as a parameter .
I really need to know how to deal with that because I do need to write the module in C .
Thank you in advance.

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: Writing new C API into micropython firmware

Post by KJM » Wed Jun 22, 2022 12:16 am

Damien & COs musing are a bit above my pay grade. What about Zerynth, have you had any experience of that?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Writing new C API into micropython firmware

Post by jimmo » Wed Jun 22, 2022 12:36 am

Firas_Baccouri wrote:
Tue Jun 21, 2022 5:22 am
I really need to know how to deal with that because I do need to write the module in C .
When you write a function that is exposed to Python, the only argument types supported are mp_obj_t (i.e. Python objects). The key is that Python only has "arbritrary precision signed integer" it has no concept of uint8_t etc.

It's then up to the function to convert them into the C data types you need, using e.g. mp_obj_get_int

Firas_Baccouri
Posts: 35
Joined: Wed Apr 27, 2022 7:22 am

Re: Writing new C API into micropython firmware

Post by Firas_Baccouri » Wed Jun 22, 2022 6:54 am

Hey Jimmo ,
actually I have managed to work with mp_obj_t but unfortunately it happened that I get an overflow of converting long int to machine word (i.e I sent 4 bytes of data through my handmade spi but while the integer with python is signed so I faced with same data an overflow the data I sent it more that 2^31 ) , I am wondering if there are a way to use the unsigned integer , or is there a solution to avoid that overflow besides using negative number .

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Writing new C API into micropython firmware

Post by jimmo » Wed Jun 22, 2022 9:45 am

Firas_Baccouri wrote:
Wed Jun 22, 2022 6:54 am
I am wondering if there are a way to use the unsigned integer , or is there a solution to avoid that overflow besides using negative number .
This has been proposed in https://github.com/micropython/micropython/pull/8089

But if you just want the full 32 bits then cast to uint32_t

Post Reply