Create a native C library for use in micropython for stm32

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Krasn4ck
Posts: 10
Joined: Wed Feb 10, 2021 5:46 pm
Location: Baikonur

Create a native C library for use in micropython for stm32

Post by Krasn4ck » Wed Feb 24, 2021 5:27 am

Hello, I intend to create some libraries to connect to read and write an external eeprom and another to display text on a tft screen, I have some experience in C, I have been looking at the code with which micropython was written and there are some things I understand, but not others, in this case the micropython documentation I have been reviewing but I understood a little more with the source code, could you recommend me some part of the source code in which I can understand a little more about the structure of an external library in C and the coding standard that is used.

Thank you very much for your answers.

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

Re: Create a native C library for use in micropython for stm32

Post by stijn » Wed Feb 24, 2021 7:04 am

When you say 'library', is it possible you actually just mean 'code'? Or do you really mean 'library' as used in C lingo i.e. a 'static library' or a 'dynamic library'?
Can you provide some more focus for the question: what exactly do you want to know? Have you read http://docs.micropython.org/en/latest/d ... dules.html? Also see the .c files in the extmod/ directories, they are good examples of how to integrate external code into MicroPython.

Krasn4ck
Posts: 10
Joined: Wed Feb 10, 2021 5:46 pm
Location: Baikonur

Re: Create a native C library for use in micropython for stm32

Post by Krasn4ck » Wed Feb 24, 2021 7:20 am

stijn wrote:
Wed Feb 24, 2021 7:04 am
When you say 'library', is it possible you actually just mean 'code'? Or do you really mean 'library' as used in C lingo i.e. a 'static library' or a 'dynamic library'?
Can you provide some more focus for the question: what exactly do you want to know? Have you read http://docs.micropython.org/en/latest/d ... dules.html? Also see the .c files in the extmod/ directories, they are good examples of how to integrate external code into MicroPython.
to see if I can understand, it would be a static library written in C that can be used in micropython with the import statement, so that the functions contained within these can be used in micropython.

I have also reviewed the documentation, only that the language barrier has made me a little difficult.

Thank you :)

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

Re: Create a native C library for use in micropython for stm32

Post by stijn » Wed Feb 24, 2021 7:42 am

The link about user C modules can do that exactly.

However wanting a static library only makes things more complicated than needed (need a separate build step in which you manually provide all correct include paths, qstr processing, ....): unless you have very good reasons for it, I suggest you just add the code to MicroPython directly.

Copy examples/usercmodule/cexample, add your .c files in that directory and in micropython.mk and you're settled.

Krasn4ck
Posts: 10
Joined: Wed Feb 10, 2021 5:46 pm
Location: Baikonur

Re: Create a native C library for use in micropython for stm32

Post by Krasn4ck » Wed Feb 24, 2021 8:06 am

stijn wrote:
Wed Feb 24, 2021 7:42 am
The link about user C modules can do that exactly.

However wanting a static library only makes things more complicated than needed (need a separate build step in which you manually provide all correct include paths, qstr processing, ....): unless you have very good reasons for it, I suggest you just add the code to MicroPython directly.

Copy examples/usercmodule/cexample, add your .c files in that directory and in micropython.mk and you're settled.
Thank you.

Now that you have cleared my doubts very well and I read the documentation you sent me a little more carefully, the C module can be created specifically for the stm32 port in this case as the specific pins of a stm32f407xx mcu are manipulated, do you think it is possible?

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

Re: Create a native C library for use in micropython for stm32

Post by stijn » Wed Feb 24, 2021 9:29 am

I think so, but I don't really know the stm port so not sure. But if it exposes C methods, i.e. there are .h files which allow to manipulate pins, then yes you can just use them in your code.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Create a native C library for use in micropython for stm32

Post by pythoncoder » Wed Feb 24, 2021 10:03 am

Do you actually need to code this in C? There are MicroPython libraries for accessing EEPROM chips and for displaying text on TFT screens which work fast enough for most purposes, except maybe gaming.

For example micropython_eeprom and nano-gui along with other micro GUIs for touch displays. And those are just my efforts - there are others if you look e.g. in the wiki.
Peter Hinch
Index to my micropython libraries.

Krasn4ck
Posts: 10
Joined: Wed Feb 10, 2021 5:46 pm
Location: Baikonur

Re: Create a native C library for use in micropython for stm32

Post by Krasn4ck » Wed Feb 24, 2021 9:51 pm

pythoncoder wrote:
Wed Feb 24, 2021 10:03 am
Do you actually need to code this in C? There are MicroPython libraries for accessing EEPROM chips and for displaying text on TFT screens which work fast enough for most purposes, except maybe gaming.

For example micropython_eeprom and nano-gui along with other micro GUIs for touch displays. And those are just my efforts - there are others if you look e.g. in the wiki.
The library for the tft screen is specifically for the ST7735 model, the library that exists in uPython has problems to be implemented because it has very large delays when updating information.

Post Reply