How to build micropython with PlatformIO and ESP-IDF?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zhenghaku
Posts: 6
Joined: Wed Jul 15, 2020 3:47 pm

How to build micropython with PlatformIO and ESP-IDF?

Post by zhenghaku » Mon Jul 20, 2020 3:57 pm

Hi, it's me again.
I want to use micropython as a library so I can execute python script in my ESP-IDF C++ project.
I am using the latest PlatformIO and ESP-IDF framework. The ESP-IDF version is 4.0.
I am leveraging on the PlatformIO library and dependency resolving system instead of writing cmakelist file myself, the micopython seem not compatible with the CMake system out of box. I copied \extmod, \lib, \py to the platformIO global library folder:core_dir/lib/micropythoi. I built a UNIX port on my desktop and copied the \genhdr to that fodler as well.
I copied the :"mpconfigport.h, mpconfigboard.h, mphalport.h, mphalport.c mpthread.h, mpthread.c, gccollect.h, gccollect.c" to that folder as well.

When I build, I got all kinds of error. The compiler seems found all the libraries and tried to compile every single .c file. This gives me lots of undeclared errors. I remove those file that giving such errors one by one, until I cannot do this to reduce error.

Now I get :C:\users\zheng\.platformio\lib\MycroPythonEmbedded\lib\utils\pyexec.c:99:9: error: implicit declaration of function 'mp_hal_set_interrupt_char'; did you mean 'mp_hal_stdin_rx_chr'? [-Werror=implicit-function-declaration]

and

C:\users\zheng\.platformio\lib\MycroPythonEmbedded\mpthreadport.c:132:130: error: 'MP_TASK_COREID' undeclared (first use in this function); did you mean 'pdTASK_CODE'?

And many similar implicit declaration of function error. And I assume there are many more error waiting.

Can someone give me a right way of doing this?
Really need your help.

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

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by jimmo » Tue Jul 21, 2020 5:04 am

This is not really possible right now, although it could be done.

If your goal is just to run Python code as part of a larger existing ESP-IDF application, then you might find it easier to embed MicroPython (see https://github.com/micropython/micropython/pull/5964) rather than using the existing ESP32 or Unix port. This will mean you'll have to implement your own modules for accessing hardware, but perhaps your particular use case doesn't need that anyway?

Copying the genhdr from a different port is definitely not going to work.

zhenghaku
Posts: 6
Joined: Wed Jul 15, 2020 3:47 pm

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by zhenghaku » Tue Jul 21, 2020 6:30 am

Hi, jimmo,

I downloaded the MicroPython embedding example, that is exactly what I want.
But I still don't know how to build it under ESP-IDF v4.

kml27
Posts: 3
Joined: Sat Jul 25, 2020 7:32 am

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by kml27 » Sat Jul 25, 2020 7:49 am

zhenghaku wrote:
Tue Jul 21, 2020 6:30 am
But I still don't know how to build it under ESP-IDF v4.
Follow the steps in in the README.md for the ESP32 Port of micropython. Try the 4.x commit hash e.g.

Code: Select all

git checkout <4.x commit hash>
in the ESP-IDF repo clone after the

Code: Select all

make ESPIDF=
step outputs it's error message which includes the 3.x and 4.x commit hashes known to work.

kml27
Posts: 3
Joined: Sat Jul 25, 2020 7:32 am

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by kml27 » Sat Jul 25, 2020 8:02 am

I haven't setup PlatformIO yet, but I was planning to give it a try. I know if you're using esp-open-sdk you'll need to patch crosstool-NG for newer versions of bash.

e.g.

Code: Select all

git clone --recursive https://github.com/pfalcon/esp-open-sdk.git /tools
#sed for esp-open-sdk#365
cd /tools && bash --version && sed -i 's/GNU bash, version (3\\.\[1-9\]/GNU bash, version (3\\.\[1-9\]|\[4-9\]\\.\[0-9\]+|\[1-9\][0-9]+\\.\[0-9\]+/' crosstool-NG/configure.ac && cat crosstool-NG/configure.ac && make || cat crosstool-NG/build.log
The regex isn't pretty, but should cover new versions for quite a while.

zhenghaku
Posts: 6
Joined: Wed Jul 15, 2020 3:47 pm

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by zhenghaku » Wed Jul 29, 2020 11:07 am

Hi, kml27, thank you very much.
I was using an old branch, it only has esp-idf v3.3 hash in the make file.
I checked out the latest master micropython and was able to build using esp-idfv4, but still not able to build in platformio.

So I took another approach. I will try to use micropython as a static lib.

I found this post: Building MP as a static library viewtopic.php?t=214

Code: Select all

lib $(LIBMICROPYTHON): $(OBJ)
	$(AR) rcs $(LIBMICROPYTHON) $^
	$(LIBMICROPYTHON_EXTRA_CMD)
In the py/mkrules.mk, so I changed the name of the app_main to something else
and run

Code: Select all

make lib libmicropython.a
and get a static lab.

Then when i try to use this static lib in platformIO, when linking, it gives me many undefined reference errors:

Code: Select all

c:/users/zhengwei/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\users\zhengwei\.platformio\lib\MycroPythonEmbedded\lib/micropython.a(mpthreadport.o):(.literal.mp_thread_mutex_init+0x0): undefined reference to `xQueueCreateMutexStatic'
c:/users/zhengwei/.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: C:\users\zhengwei\.platformio\lib\MycroPythonEmbedded\lib/micropython.a(mpthreadport.o): in function `mp_thread_mutex_init':
mpthreadport.c:(.text.mp_thread_mutex_init+0x7): undefined reference to `xQueueCreateMutexStatic'
above is just part of it. Now I run out of ideas, how can I use MP as a static lib in platformio?

robdobson
Posts: 6
Joined: Mon Sep 28, 2020 9:23 am

Re: How to build micropython with PlatformIO and ESP-IDF?

Post by robdobson » Thu Nov 12, 2020 1:19 pm

I've successfully embedded MicroPython into an existing ESP-IDF project, here's a blog post about how I did it ... https://robdobson.com/2020/11/embedding ... -on-esp32/ ... I got the idea from another thread on this forum but I can't find it anymore to reference the person who suggested it - sorry

Post Reply