0

Is it possible to lock one esp32 core to running circuit python and the other running C++. I want to run realtime code (with interrupts) in C++ and the main program in circuit python.

I have successfully locked a realtime task to one core and run with main program on the other core but that were both in C++.

1 Answer 1

1

It is possible with enough programming experience and skill. It would be much easier to do if you're using C rather than C++, as CircuitPython is written in C.

You don't run a separate binary on each core. The cores share RAM and flash storage and all the hardware peripherals that are part of the ESP32.

The way you'd make this work is to write a custom CircuitPython module in C. That That module would use the underlying ESP-IDF FreeRTOS calls to create a task (or tasks) that would be pinned to core 0, running the code you want to run there.

CircuitPython will natively run on core 1. It's already pinned there and there's no need to do anything else to force it to run on core 1.

Core 0 also runs the ESP-IDF network stack so it's important to be careful not to interfere with its operation.

You'd need to target either an ESP32 or ESP32-S3 as those are the only dual-core ESP32 CPUs shipping at the time you asked this question. The ESP32-S3 would be a better choice as it has native USB support and is easier to use with CircuitPython.

You'd also need to pick a CircuitPython firmware release to work with. At the time that you asked this question, CircuitPython is in testing its 9.0 firmware. Version 9 is a significant update which includes support for the current (5.x) versions of ESP-IDF, so it would be a better choice than version 8.2.x which requires older 4.4.x versions of ESP-IDF.

So to do this successfully, you'll need to be skilled in CircuitPython, C and ESP-IDF. It would also help if you had experience debugging with JTAG as you'll almost certainly need the help of a proper debugger to get things working.

There's a lot involved in writing a CircuitPython module in C, and it goes far beyond what's reasonable to expect in a StackOverflow answer. Adafruit has a blog entry that points to some detailed resources. You can also find many examples of CircuitPython modules written in C under the espressif port, particularly in common-hal. The top level CircuitPython shared-bindings directory also contains the code for CircuitPython modules that are written in C.

If you really require doing this in C++ you'd need to add C++ skills to that list and would need to learn how to call C++ code from C or figure out how to instantiate and manage the CircuitPython interpreter from C++. Both of these are possible for developers with enough skill and experience but it would almost certainly be simpler and easier to stick with C and rewrite any C++ resources you were hoping to use to be pure C.

If this sounds daunting and you're not already familiar with much of what I've mentioned here you'd most likely have an easier time just writing in pure C or C++.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you romkey for your very comprehensive and helpful comments. I wrote a lot of C in my working life. Recently I have written a lot in C++ and some in Python. I like the extra power and simplicity of Python versus C++ but it is hard to deal with tasks that have a real time content. Hence the idea of writing the real time bit on one core and the Python bit on the other. From what you say I would still have deal with the network stack on core 0 I can see that being a problem. Another idea is to have the real time part on a atTiny or even another esp32 running C++ and Python on an esp32.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.