user2129623
user2129623

Reputation: 2257

How do I rebuild micropython firmware adding my own C++ code

I want to run my C++ code no micropython

For that I referred https://github.com/stinos/micropython-wrap this wrapper.

Uploaded required wrapper files on the board and tried to run

#include <micropython-wrap-master/functionwrapper.h>

//function we want to call from within a MicroPython script
std::vector< std::string > FunctionToBeCalled ( std::vector< std::string > vec )
{
  for( auto& v : vec )
    v += "TRANSFORM";
  return vec;
}

//function names are declared in structs
struct CppFunction
{
  func_name_def( TransformList )
};

extern "C"
{
  void RegisterMyModule(void)
  {
    //register a module named 'foo'
    auto mod = upywrap::CreateModule( "foo" );

    //register our function with the name 'TransformList'
    //conversion of a MicroPython list of strings is done automatically
    upywrap::FunctionWrapper wrapfunc( mod );
    wrapfunc.Def< CppFunction::TransformList >( FunctionToBeCalled );
  }
}

Run it using

import foo

print(foo.TransformList(['a', 'b']))  # Prints ['aTRANSFORM', 'bTRANSFORM']

But later I found this will not help. Because I need to integrate my C++ code into micropython code and rebuild the firmware to get it run.

I am not able to figure out

  1. How to integrate my C++ in existing micropython code
  2. How to recompile the firmware ( Because when I try to use make command, it does not seem working )

Any help highly appreciated.

Upvotes: 2

Views: 1725

Answers (2)

stijn
stijn

Reputation: 35921

You don't need necessarily need to recompile, you can build a C++ user module. Which has been done for ESP32 already, see discussion at https://github.com/stinos/micropython-wrap/issues/5#issuecomment-704328111.

Specifically if you run make USER_C_MODULES=../../../micropython-wrap CFLAGS_EXTRA="-DMODULE_UPYWRAPTEST_ENABLED=1" from the ESP32 port directory, this should build a user C++ module containing micropython-wrap's unittests, so that should be a good starting point: copy the relevant files (cmodule.c, module.cpp and micropython.mk from the tests directory) and modify the code.

Upvotes: 1

Jos Verlinde
Jos Verlinde

Reputation: 1697

MicroPython offers two options of adding C/C++ code to it. Note that both require cross-compilation of that code on a PC. AFAIK there is no option to compile on a microcontroller (due to obvious constraints)

1) MicroPython Native Module
One of the main advantages of using native .mpy files is that native machine code can be imported by a script dynamically, without the need to rebuild the main MicroPython firmware.
Essentially you compile C to MicroPython bytecode and store that in a module. Then you import that module into Python
http://docs.micropython.org/en/latest/develop/natmod.html#minimal-example

2) MicroPython C Module
You add you C/C++ code as a custom module, and then compile and link that together into a new firmware image.
http://docs.micropython.org/en/latest/develop/cmodules.html#micropython-external-c-modules

The approach you refer to, is based on the 2nd method, and requires cross-compilation.

C or C++ support will depend on the compiler/languages supported for you port/hardware.

Upvotes: 2

Related Questions