STM32 + FreeRTOS + MicroPython, how?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
mhe
Posts: 4
Joined: Fri Jul 08, 2022 10:19 am

STM32 + FreeRTOS + MicroPython, how?

Post by mhe » Mon Jul 11, 2022 9:46 am

Hello!

This is my first post, hopefully it's in the right section of the forum.

I'm trying to make the combo mentioned in the topic name: STM32 (F407 or F746) + FreeRTOS + MicroPython. The goal at the moment is just to get a simple led blinking task run along with MicroPython in it's own task. The minimal working MicroPython is enough.

I have built the minimal port for the F407 DISCO and it works. I also have an STM32CubeIDE project with FreeRTOS and a couple of led blinking tasks working with the board. The problem is how to combine these.

I have read the short embedding guide at https://github.com/micropython/micropyt ... /embedding but I don't understand enough about these things to get it working on my own, so I'm ask for help :)

I think there are two steps here, and they both are a bit unclear to me:
1. Build MicroPython as a library for the correct MCU (STM32)
2. Include the library correctly into the existing STM32CubeIDE project

All help is greatly appreciated!


P.S. I know that the ESP32 port is made on top of FreeRTOS and I have successfully modified it to get also my own led blinking task running on the ESP32. I also could access it from a simple self-made MicroPython module. So I'm pretty confident that what I'm now trying with STM32 is possible.

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

Re: STM32 + FreeRTOS + MicroPython, how?

Post by jimmo » Mon Jul 11, 2022 10:54 am

mhe wrote:
Mon Jul 11, 2022 9:46 am
I have read the short embedding guide at https://github.com/micropython/micropyt ... /embedding but I don't understand enough about these things to get it working on my own, so I'm ask for help
This is the way to do what you want. If you can provide more details about what didn't make sense I can explain more.

I have tried to improve this a bit, see https://github.com/micropython/micropython/pull/5964 but the effort has stalled for now. It isn't easy to make a generic solution to this although I would be keen to pick it up again.

mhe
Posts: 4
Joined: Fri Jul 08, 2022 10:19 am

Re: STM32 + FreeRTOS + MicroPython, how?

Post by mhe » Mon Jul 11, 2022 11:47 am

Hi Jimmo,

thanks for the quick response!

I guess the biggest question for me is what should I do to build the library for the STM32 board? What files should I edit and how? I don't think I know how to ask more specific questions :oops:

If I get that working, then the rest is probably just tinkering with the CubeIDE, but of course I warmly welcome any help you can give about that too :)

Your PR looks to address exactly the problem I'm having as a new MicroPython user (I'm new to many other things too...), but if you can give more specific instructions for the STM32 build, that would be great!

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

Re: STM32 + FreeRTOS + MicroPython, how?

Post by jimmo » Mon Jul 11, 2022 12:26 pm

mhe wrote:
Mon Jul 11, 2022 11:47 am
I guess the biggest question for me is what should I do to build the library for the STM32 board? What files should I edit and how? I don't think I know how to ask more specific questions
So... it's complicated. There are many steps here, and none of them are easy.

One of the difficult parts is that the embedding example is geared towards embedding in a Linux program, and as such includes a lot of stuff from the Unix port. It's not a very useful example because it doesn't make it at all clear which bits are related to "building MicroPython as a library" and which bits are "doing Unix stuff". (This is why I wanted to replace it, but I bit off too much at once I think which is why that PR has stalled).

The way I would approach this is to forget about STM32 initially and just build a command line program for whatever platform you're using (this will be much easier with Linux or Mac, if you're on Windows then setting up an environment where you can use Make would be necessary, either mingw/cygwin or WSL2 but I don't know much about either), and then embed MicroPython in that (i.e. write a program where main() can evaluate some Python code or something). That will give you a good starting point to get the bits you need working.

Then what you need to do is adapt Makefile.upylib to use arm-none-eabi-gcc with the necessary flags for your particular chip (i.e. cortex m4 or m7), and then you'll get a .a file. Then you can include that in your Cube project.

FWIW, this might be the sort of thing you can sponsor someone else to do. It's definitely a thing that the MicroPython project needs, but the team is stretched pretty thin.

Another thing we're considering is adding a different way to embed MicroPython similar to what SQLite uses. They call it an "amalgamation" but basically the idea is to have a "port" of MicroPython that produces a giant .c file that you just include in your Cube project. Then there's no linking or complexity to build a suitable library. (There was an early implementation of this about 6 years ago, it would be great to revisit this)

mhe
Posts: 4
Joined: Fri Jul 08, 2022 10:19 am

Re: STM32 + FreeRTOS + MicroPython, how?

Post by mhe » Mon Jul 11, 2022 12:55 pm

jimmo wrote:
Mon Jul 11, 2022 12:26 pm
The way I would approach this is to forget about STM32 initially and just build a command line program for whatever platform you're using...

.. Then what you need to do is adapt Makefile.upylib to use arm-none-eabi-gcc with the necessary flags for your particular chip (i.e. cortex m4 or m7), and then you'll get a .a file. Then you can include that in your Cube project.
I'll give that a try, thank you for the tip!
jimmo wrote:
Mon Jul 11, 2022 12:26 pm
Another thing we're considering is adding a different way to embed MicroPython similar to what SQLite uses. They call it an "amalgamation" but basically the idea is to have a "port" of MicroPython that produces a giant .c file that you just include in your Cube project. Then there's no linking or complexity to build a suitable library....
This sounds simple enough for even me to understand, so I'll keep my fingers crossed for this.

Thanks for your help!

mhe
Posts: 4
Joined: Fri Jul 08, 2022 10:19 am

Re: STM32 + FreeRTOS + MicroPython, how?

Post by mhe » Tue Jul 26, 2022 7:39 am

Turned out it was not so complicated after all.

All I had to do was "make lib CROSS=1" for the minimal port and the result was an a. file that worked fine with the CubeIDE. Now I have a REPL at my DISCO board's UART.

Or is there something I'm missing here?

Post Reply