stinos / micropython-wrap

API for interop between C/C++ and MicroPython
MIT License
124 stars 23 forks source link

how to use esp32 example #9

Closed hstarmans closed 2 years ago

hstarmans commented 2 years ago

I ran make usercmodule MICROPYTHON_PORT_DIR=../micropython/ports/esp32 This creates a new binary, i flashed the binary to the chip, but how do i use the usercmodule? I tried to import mod, foo, upywraptest etc. but it does not work?

stinos commented 2 years ago

import upywraptest is the one, the Makefile is for building the tests. I can't tell why that doesn't work for you, can you provide more information? MicroPython version used, does the compiler log say 'including user C module from ...', can you see it compiles tests/module.c and tests/module.cpp?

hstarmans commented 2 years ago

I checked out Micropython at Hash 64180f0 date november 23, 2020. You created the Makefile for micropython-wrap dac3338 on december 10, 2020. Running the micropython-wrap makefile results in a directory build-usercmod in micropython/ports/esp32. It creates two binaries; firmware.bin and application.bin. I flash the firmware.bin to the esp32.

Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
Building with ESP IDF v3
Writing build-usercmod/firmware.bin to the board
esptool.py v2.8
Serial port /dev/ttyUSB0

The binary works and I can connect to the esp32. import upywraptest does not work. Micropython-wrap does not work with the current master. It fails.. The makefile changed a lot. This was the make file around december 2020 This is the current makefile. With the latest master your makefile runs into the following error;

Including User C Module(s) from /home/hexastorm/projects/micropython-wrap/Makefile
CMake Error at /home/hexastorm/projects/micropython-wrap/Makefile:11:
  Parse error.  Expected "(", got unquoted argument with text "=".
Call Stack (most recent call first):
  /home/hexastorm/projects/micropython/py/usermod.cmake:42 (include)
  main/CMakeLists.txt:10 (include)

-- Configuring incomplete, errors occurred!
See also "/home/hexastorm/projects/micropython/ports/esp32/build-usercmod/CMakeFiles/CMakeOutput.log".

To summarize;

Finally, I assume that with your toolchain I can compile with C++ so do not have to define extern c everywhere. The problem with the CPP example in micropython is that it doesn't scale well. I am not sure how to wrap something like TMCStepper, it seems to me I have to wrap all C++ code in extern c blocks which is a non-starter.

stinos commented 2 years ago

Running the micropython-wrap makefile results in a directory build-usercmod in micropython/ports/esp32.

Can you doublecheck it effectively compiles both the .c and .cpp file in the tests/ directory? Either from the log of by introducing a compiler error in the files. I'd also need to know the exact version of the ESP SDK you're using (I can only try to reproduce this if I have the same version of everything).

Btw do the micropython example modules work for you? Something like make USER_C_MODULES=../../examples/usercmodule

With the latest master your makefile runs into the following error; CMake Error at /home/hexastorm/projects/micropython-wrap/Makefile:11

That's actually MicroPython running into an error, it seems to be trying to use CMake to build a (gnu make) Makefile, that's never going to work. I'll have a look how to make that work with the most recent version of everything.

did you test the code on a ESP32?

At one point yes, see see https://github.com/stinos/micropython-wrap/issues/5#issuecomment-704333711

it seems to me I have to wrap all C++ code in extern c blocks which is a non-starter.

That's not the case, only the code which needs to go into the C module directly needs to be wrapped in extern "c". See tests/module.cpp for instance. The micropython example is just done like that for simplicity. The function body could have been a separete 'pure' C++ function called from the C function.

hstarmans commented 2 years ago

Stinos, my limited understanding of the interaction between c and c++ seems indeed to be the issue. I will work further with the example from micropython master.

stinos commented 2 years ago

Let me know if you'd get back to this and still have problems.

hstarmans commented 2 years ago

@stinos , I made a Micropython wrapper for TMCStepper, see https://github.com/hstarmans/micropythonwebserver . It allows you to call a function which then sets up the settings for the motor drivers which i use in my laser scanner. This is the only thing I need at the moment. It compiles and most likely works. Still need to test it on actual hardware. Thanks for you time!