Writing new C API into micropython firmware
-
- Posts: 35
- Joined: Wed Apr 27, 2022 7:22 am
Writing new C API into micropython firmware
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
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
Re: Writing new C API into micropython firmware
The easiest way is with "User C Modules". See https://docs.micropython.org/en/latest/ ... ython.htmlFiras_Baccouri wrote: ↑Wed Jun 15, 2022 8:45 amI want to implement a C API into the micropython firmware and then using it from the python software ,
-
- Posts: 35
- Joined: Wed Apr 27, 2022 7:22 am
Re: Writing new C API into micropython firmware
Thank you Jimmo for your reply ,
yeah I have figured that and I am into it .
yeah I have figured that and I am into it .
Re: Writing new C API into micropython firmware
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?
Re: Writing new C API into micropython firmware
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
-
- Posts: 35
- Joined: Wed Apr 27, 2022 7:22 am
Re: Writing new C API into micropython firmware
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.
I really need to know how to deal with that because I do need to write the module in C .
Thank you in advance.
Re: Writing new C API into micropython firmware
Damien & COs musing are a bit above my pay grade. What about Zerynth, have you had any experience of that?
Re: Writing new C API into micropython firmware
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.Firas_Baccouri wrote: ↑Tue Jun 21, 2022 5:22 amI really need to know how to deal with that because I do need to write the module in C .
It's then up to the function to convert them into the C data types you need, using e.g. mp_obj_get_int
-
- Posts: 35
- Joined: Wed Apr 27, 2022 7:22 am
Re: Writing new C API into micropython firmware
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 .
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 .
Re: Writing new C API into micropython firmware
This has been proposed in https://github.com/micropython/micropython/pull/8089Firas_Baccouri wrote: ↑Wed Jun 22, 2022 6:54 amI 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 .
But if you just want the full 32 bits then cast to uint32_t