Dual Core Programming for the Raspberry Pi Pico, ESP32, and ESP32-S3 Using the Arduino IDE
HTML-код
- Опубликовано: 3 окт 2022
- The RP2040 (found in the Raspberry Pi Pico), the ESP32, and the ESP32-S3 are all dual-core microcontrollers, with two CPU cores. All three can be programmed using the Arduino IDE. Here is a look at how to program both cores on those chips using the Arduino IDE, including a unified sketch that will work on RP2040 based boards and on ESP32/ESP32-S3 boards.
---
Source code: github.com/garyexplains/examp...
Let Me Explain T-shirt: teespring.com/gary-explains-l...
Twitter: / garyexplains
Instagram: / garyexplains
#garyexplains
I remember back then Lord Gary mostly explain Android, now he explain every technology! Respect for this guy :)
The Professor professes!
I use the two cores of ESP32 for robotics, one for communication with the host and another for closed loop control. This way I get a very low latency in the control loop and I still can change the target position and sample current positions at relatively high frequencies.
Good timing! I'm starting a project for a digital audio synthesizer, and I'm planning to use dual cores, and a little later, RTOS, so very useful!
Reads will correct themselves and will not inherently corrupt the data; however, if multiple reads are required (e.g. long on 8b processor or double on 32b), then you might read some bytes before the value changed and the rest after the value changed. In this case this particular value is corrupt. If you don't lock your reads, be careful how you use the result. This corrupt value might otherwise cause the code to branch incorrectly or to perform incorrect calculations and/or to store incorrect values.
Coming from MicroPython on the Pi Pico this is so simple to implement it hurts, thank you for the video.
You are welcome!
Thanks Gary. watched this videos 4 times for sharp understanding.
and read all the below comments. now I'm handson. cheers😁
Very nice reference to Gibson.
Great video as always
Love the theme
Thanks for sharing your expirence with all of us 👍 😀
Thanks for watching!
This info will be very useful. Thank you.
Just to clarify to anyone interested in parallel programing (since I didn't know this when I bought the esp32) the esp32 can run more than 2 tasks at once thanks to freeRTOS(I don't know about the pico) that is why the code for the esp32 is a bit more complicated,some people say they got around 50 tasks running without any problems
Ps core 0 is responsible for wifi and Bluetooth so if you are using those don't overload it with complex tasks
*GARY!!!*
Good afternoon Professor!
Good afternoon fellow classmates!
Stay safe out there everyone!
MARK ‼️
Outstanding explanation! Thanks
Glad it was helpful!
Круто! Спасибо Gary!
Looking forward for ypur explanations of mutex, semaphores and synchronizations. Thanks in advance
Really good content. Post more of these videos.👍
Any suggestions on what topics you would like to see me cover?
@@GaryExplains For example, ESP32 Running WiFi (connected to MQTT) in Core-1 and Running LoRa Functionality (connected to a LoRa Module) in Core-2. Here this device (ESP32 + LoRa Module) is functioning as a Gateway.
Very helpful. 😀
Glad you think so!
Reads can absolutely become corrupted if you're reading a multi-word structure. Unless you're absolutely sure your read operation is atomic you should definitely lock the mutex on reading.
Also for the locking/unlocking problem with mutexes, and easy way to solve the problem is to create a MutexLocker class that takes a mutex reference and locks it in the constructor, and unlocks it in the destructor. That way you can be guarenteed that any way you exit out of a function (even if an exception is thrown up the stack) the mutex when be unlocked when the MutexLocker goes out of scope.
Always interesting and informative! Please do a video on semaphores etc. can’t wait 👍
¹1¹st;
I've been in computers over a half century, and I find Gary's presentations particularly relevant with extraordinary clarity.
Great vid with a cool explanation that can, as you said, be a template for other codings (simple or complex). Please keep the technical threads coming on multi-thread processing with all their nuances and your videos on other programming languages.
I want to separate communication i2c out to core 0 the technique you showed is a nice start. Thanks
Thanks! This is very helpful for our needs as we work on support for the impressive RP2040.
Sehr gut
Thanks for the splendid video. Very informative. I guess the same programming will work on an Arduino RP2040 connect, right?
Yes, the template should work on the Arduino RP2040 connect.
Are any objects inherently thread-safe in this arduino/pico "land"? In some other languages you have objects e.g. queues that are thread-safe amongst others. Meaning that the standard library handle the locking for you. Would be nice to get an overview of that. I'm glad however that you touched upon this because two threads working seperatly without communicating with each other is rearly usefull in my experience.
cool.
is there a python example of accessing both cores
How does dual core work with serial communications? Do you do the lock/unlock on a variable, and then use that to determine which core gets to use the serial port, or is there a better way?
This is a very interested and useful video. Do you know how I can use the Arduino IDE to change the clock speed on the Raspberry Pico? I know it is possible to overclock it by quite a bit, but I have not been able to figure out how to do it.
Just click the clock speed you want from Tools->CPU Speed.
@@GaryExplains Thank you for taking the time to read my comment and reply. I do not see an option for the pico in the Tools menu. I have a Teensy 4.1 also and the speed option does show up in the menu for this board; however, it does not for the pico. I have upgrade to the Arduino 2.0 IDE also.
Do you have the Pico board installed as described under "Installing via Arduino Boards Manager" here: github.com/earlephilhower/arduino-pico
@@GaryExplains I got it all working - dual cores and overclocking - following your video and using the link you provided for the install. Thanks so much!
This was *exactly* the video I needed, but unfortunately I'm missing something. I just installed the latest IDE (2.2.1) with the current MBED RP2040 board manager package (4.0.8). The locking example does not compile for me. It seems to not find semphr.h, nor freeRTOS.h, even if I install the freeRTOS library through library manager. Thoughts? I need to have one sensor reading data on core 0, and another reading its data (asyncrhonously) on core 1. The data on core1 needs to periodically get shared back to core0 via a global float, so the "current" values of both sensors can be output synchronously via serial. Any advice appreciated.
How does this change if using the ST Microcontrollers? For example, if the peripheral assigned to each core.. can they be changed at run-time? For example, can the cores communicate, such that one passes UART access to the other?
Would love to see an overview example on how the memory works in dual-core applications. For example the STM32H745 has 2MB of flash, and 1MB of RAM, but how can you allocate most of it to the M7, and the remaining to the M4? Is it possible to debug both simultaneously thru a single JTAG/SWD connector?
How does communication work between cores?
AFAIK the STM32H745/755 aren't supported in Arduino via STM32duino.
How do you add the raspberry pi pico to the arduino IDE? Is it through the board manger?
Yes, see the Installing via the Arduino Boards Manager section on this page: github.com/earlephilhower/arduino-pico
What core do interupts run on?
Great video, but unfortunately I cannot get the code to give output from the second core on my actual Pico board, I only get output from loop0. I haven't got a Seed board to try.
I am using Arduino IDE 2.0.4 and ArduinoMbed OS RP2040 v4.02 is installed.
I used Serial.print rather than Serial.printf as I haven't added a library to support it. so- Serial.print ("loop1"); Serial.println(count1); I would be very grateful for any suggestions as i'm at a bit of a dead end.
I have just realised I have a Pico W rather than the regular Pico, so perhaps this is the problem.
My Arduino 2.0.0 IDE does not show the "Tools/Arduino runs On" entry in the menue ???
and ARDUINO_RUNNING_CORE is not declared ?
Do you have an ESP32 board selected? The menus are context sensitive.
@@GaryExplains Yes, board manager boards: esp32 by Espressif 1.0.6 choosen board ESP32 Dev Module
@@GaryExplains xTaskCreatePinnedToCor(Task1code, "Task1“, 10000, Null,1, &Task1, 0 or 1 ) works
which core does this work on? official Arduino core, or the core by Earle Philhower?
Boards supported by Earle Philhower's core.
Your code also works on ESP32-C3 - without any modifications, it just runs both loops on same core. It's useful if you want the same code to run on various platforms, even single core, but make use of both cores if you have it...
12:00 COM57 how many USP ports do you have, why so many?
I have published lots of microcontroller related videos recently and I have been testing a lot of board. Please see my microcontrollers playlist.
Will it effects power consumption?
Yes, that is the topic of an upcoming video.
Is this the official arduino rp2040 core or is it the community earle philhower rp2040 core
I've always been under the assumption that you can't use both cores of an ESP32 if your using WiFi or Bluetooth as those systems are dedicated to one core if you enable them so your code is limited to the other core... Is that not true?
If that was true then you couldn't use WiFi at all on any of the single core ESP processors like the ESP8266, the ESP32-C3, or the ESP32-S2.
Hi, I have 3 DS18B20 sensors connected to ESP32, how can I pass this sensor reading from one to another core. Can anyone help me?
Is there any example to refer?
I thought I covered that in the video??? 🤷♂️ The example code "unified_dual_core_with_lock" is here: github.com/garyexplains/examples/tree/master/Dual_Core_for_RP2040_ESP32_ESP32-S3
@@GaryExplains Sorry. I couldn't understand why you use 'return r' and what if I have 3 temperature variables to deal with. Also couldn't understand why set function put rnum to r and get function do it viceversa. Sorry I am not a good programmer more of a hardware guy.
"set" and "get" are opposite functions, so of course one puts and the other does viceversa. If you have three temperate sensors then you need 3 globals called something like temp1, temp2, temp3. Then you need three set_ and get_ functions that use the same lock (i.e. bigLock_lock(); and bigLock_unlock(); ). So you need set_temp1(), set_temp2(), set_temp3(), get_temp1(), get_temp2(), and get_temp3(). There are other ways to do it, but for your level of programming this is the easiest. Overall one core will "set" the number and the other core will read it with "get".
@@GaryExplains 👍
@@GaryExplains Can't I set 3 variables in one set function itself?
Can't quite figure out... are *all* esp32 dual core?
No they are not. For example, the ESP32-S2 and ESP32-C3 are both single core.
@@GaryExplains Where can I find detailed info on which is single and which dual ?
On the manufacturer's website, e.g. www.espressif.com/en/products/socs/esp32-c3
Show the s3 compiler options in the ide ffs
skip>>> 2m45s why the flash card,, count zero ,, william gibson
😂 Think about it, listen to what i said at that moment.
@@GaryExplains Actually I understood why and found that very funny!!
time to switch to 2.0
Me or you?
@@GaryExplains me of course, ide 1.x is so old fashion....
Cool! I did a video covering the main new features of 2.0 here: ruclips.net/video/fahgpnsvL18/видео.html
good explanation but the thumbnail not that good
😂