Use BOTH Cores | Dual Core Programming on the Raspberry Pi Pico

  Переглядів 37,458

Low Level Learning

Low Level Learning

День тому

Learn how to use both cores on your Raspberry Pi Pico in C using the Linux SDK! Multi core programming makes your projects more powerful! By using both processor cores on the ARM RP2040, we have the potential to make our code 100% more efficient. Check out this and more embedded programming topics on my channel!
Make sure you check out my C/C++ SDK setup example before watching this video: • Blink LED in C/C++ on ...
Source code available here: www.github.com/lowlevellearni...
Chapters:
0:00 Intro
0:18 What is Multicore?
0:50 How Do RP2040 Cores Communicate?
2:15 Lets Code!
7:39 Outro
===
the raspberry pico rp2040 is an arm cortex m0 based microprocessor equipped with two cores in this video i'll show you how to write code for the raspberry pico that uses a second core on the processor using the second core enables you to make your picot projects twice as powerful by using a completely separate thread of execution to talk about doing multi-core programming we first need to define what the word core means a core is a functional unit on the processor capable of executing program code by having multiple cores on a processor you can execute multiple segments of code at the same time whenever we write code for the raspberry pico for example core zero the first core executes the code the second core is on standby never doing anything here you see a depiction of two cores each core is separately running its own code completely independently they have separate program code and separate memory space there are cases however where the course can share program code and memory space sharing code is not problematic as fetching code is a read instruction and it does not create a race condition however sharing the same locations for data is problematic as it creates a possibility for a race condition to exist where mutual accesses to the memory is not assured so if the cores aren't supposed to share memory directly then how did the course communicate well the masterminds at raspberry pi figured this out and created two separate fifos or first in first out structures to act as a mechanism for communicating between the cores only one fifo is writable by core zero and only one fifo is writable by core one this way no core is ever writing to the same location at the same time in this example core zero has some memory it needs to communicate to core one for some kind of processing instead of writing that memory directly into core one's memory space which could introduce a race condition core zero uses its writable fifo to push that data to core one core one does its processing and then uses its fifo to get that process data back to core zero core zero could have been executing other tasks while waited for core one to finish processing this process of using fifo's to pass data around prevents weird cases where core zero is reaching into core one's memory or vice versa so with that being said let's write some code to make this happen in c okay so here we are in my linux build environment um if you haven't watched my previous video on how to set up a cmake build environment for the raspberry pico c sdk please go watch that it'll make this video much more easy to digest i know i keep beating this up but i do have to say i could not have done this tutorial if i didn't have the raspberry pi pico c sdk document in front of me the entire time they do a very good job of documenting all the functions i'll be using today in the tutorial so after this tutorial go give this a read and see what other trouble you can get yourself into anyway back to the code um so over on the right we're going to be writing the code that's going to go onto our raspberry pi pico um so this is a multi-core video so the first thing we need to do is initiate the second core uh and the way we do that with raspberry pi pico is first we have to pound the pico slash multi-core dot h library what that does is it includes the library of code that raspberry pi has produced to enable us to do multi-process programming on the raspberry pi pico next we need to tell the raspberry pi pico to launch code on its second core and just to prevent any confusion um the second core is called core one just so you're aware so the code starts this code will start in court zero and the rest of the code that we launch on the second core will be in core one just so we're all on the same page we're gonna call this function and we're gonna call it on the function name that's going to get ran by the second core and we're gonna call that second core code and then we have to define a function we're going to define it as a void second core code and then whatever we write in this area is going to get ran on the second core so that was pretty fast
===

КОМЕНТАРІ: 61
@titoine2237
@titoine2237 2 роки тому
Thanks for the explanation of this FIFO feature. Nevertheless, there is important note in the doc about the FIFO usage: "The inter-core FIFOs are a very precious resource and are frequently used for SDK functionality (e.g. during core 1 launch or by the lockout functions). Additionally they are often required for the exclusive use of an RTOS (e.g. FreeRTOS SMP). For these reasons it is suggested that you do not use the FIFO for your own purposes unless none of the above concerns apply; the majority of cases for transferring data between cores can be eqaully well handled by using a queue"
@dijkstra4678
@dijkstra4678 2 роки тому
Wouldn't that involve accessing some global memory space or one core accessing the other's memory? I am slightly confused.
@pjforde1978
@pjforde1978 8 місяців тому
@@MikeSims70 I'm trying hard to find page 260 in this 201 page document...
@MikeSims70
@MikeSims70 8 місяців тому
@@pjforde1978 Boy, I have no idea what happened there LOL ... The point is, of course, that they have provided a push pull queue for moving information between cores in a thread safe manner. Not sure where I got 260 from ... that was a pretty big OOPS. Thank you for pointing it out.
@m1geo
@m1geo 8 місяців тому
Thanks for this! Super useful! Gradually binge watching your entire channel! 😍
@ragd4L
@ragd4L 2 роки тому
This is extremely high quality content, thanks and all the best !!
@rbphilip
@rbphilip 2 роки тому
I do embedded work for a living and just ordered a Pi pico to play with. In particular the programmable PIO looks interesting. This was a useful video for exposing the "C" apis for me. I had only ever seen micropython examples for the pico. Now to start working out the details of hooking up a fancy JLINK pod and use a graphical debugger!
@shahidriaz6568
@shahidriaz6568 Місяць тому
I searched so many videos but didn't got my concept cleared about RP2040 dual core coding. You have cleared my whole concept. Thanks.
@sagar73594
@sagar73594 3 роки тому
Great Scott! , Looking forward to another great video.
@joostvantilburg5998
@joostvantilburg5998 3 роки тому
Great intro into multi-core programming especially the graphical explaination before the coding. 1K subs, well done! Could you take it a step further and do some deep-sleep explaination?
@LowLevelLearning
@LowLevelLearning 3 роки тому
Coming up soon!
@AmanSingh-sp6bi
@AmanSingh-sp6bi 3 роки тому
Congrats on 1K
@LowLevelLearning
@LowLevelLearning 3 роки тому
Thank you!
@skf957
@skf957 3 роки тому
I like this very clear explanation. Sub'd. Thank you.
@TheDutchisGaming
@TheDutchisGaming 2 роки тому
Damn this video immediately answered a question I was stuck with in my head after watching a pico beginner tutorial somewhere else. (1:27)
@loopinnerthe
@loopinnerthe Рік тому
exactly what I was looking for. thanks! I need to figure out the non blocking way of doing it otherwise it doesn't actually increase the performance but that is a beautiful introduction.
@Jp-ue8xz
@Jp-ue8xz Рік тому
From the raspberry pi pico docs: " uint32_t multicore_fifo_pop_blocking ( void ) Pop data from the read FIFO (data from the other core). This function will block until there is data ready to be read Use multicore_fifo_rvalid() to check if data is ready to be read if you don't want to block. See the note in the fifo section for considerations regarding use of the inter-core FIFOs Returns 32 bit data from the read FIFO. " So apparently you just " if( multicore_fifo_rvalid() ) { do-blocking-stuff } else { do-other-stuff } "
@loopinnerthe
@loopinnerthe Рік тому
@@Jp-ue8xz I have it working now thanks and it is a dream to double the performance of my application without hardly any effort.
@chuckmuckamuck8001
@chuckmuckamuck8001 3 роки тому
Well done, subscribed.
@LowLevelLearning
@LowLevelLearning 3 роки тому
Awesome, thank you!
@alexroman6246
@alexroman6246 2 роки тому
brilliant content.
@robbo916
@robbo916 3 роки тому
Hello, I've subscribed. Thanks!
@gammaray4850
@gammaray4850 2 роки тому
simple nd great video ✌️
@LowLevelLearning
@LowLevelLearning 2 роки тому
Thnx homie
@linhe1710
@linhe1710 2 роки тому
wow,this video does help,thank u
@weench
@weench 2 роки тому
It was interesting. Thanks
@LowLevelLearning
@LowLevelLearning 2 роки тому
You're welcome
@drewmalbica7694
@drewmalbica7694 Рік тому
Thanks! Can’t wait to use both cores. So far I’ve got 2 separate instructions running on my project but this is helping me know how to push data between cores. I saw another video talk about having interrupts for each core that will run when it’s moving data over, is that going to work better when the cores are running more complex tasks?
@earth2k66
@earth2k66 Рік тому
I use pico as my primary USB DAC. I use core1 for all the interrupt-based tasks, leaving the core0 for USB stack and other simple computations.
@anthonyheak3479
@anthonyheak3479 2 місяці тому
Thanks for the tutorial, but I had to add the pico_multicore library in the target_link_libraries()
@TOMTOM-nh3nl
@TOMTOM-nh3nl 3 роки тому
Thank You
@yahmk3978
@yahmk3978 2 роки тому
Thank you very much!
@LowLevelLearning
@LowLevelLearning 2 роки тому
You're welcome!
@mr1enrollment
@mr1enrollment 2 роки тому
so have I missed it? did you do a vid on-- how to do the same Except one core with C++ and the other Python.
@naineshrathod2392
@naineshrathod2392 Рік тому
Do you have an example where you do multithreading in micropython ?
@mitchellmigala4107
@mitchellmigala4107 Рік тому
This has been one of the most frustrating and annoying projects I have ever worked on. finally got standard blink working after manually installing gcc and g++ and many other packages finally gave up and upgraded Ubuntu 6 versions to finally get the many compilers and packages this 4 lines of code needs to be built) what a nightmare. Tried this and now make cant find multicore.h what the hell? I tried all the different versions of include. what a brittle framework and architecture.
@zhitailiu3876
@zhitailiu3876 Рік тому
Thanks for the great video! Does anyone know how core1 can reconize the fifo commands and run the code spedcified by core0. In the src file of multicore.c line 145, there is a specifal cmd sequence consisting vec_table, sp, and entry point for core1. Core1 recognizes these cmds because there is already some code running in core1 after boot?
@chadkrause6574
@chadkrause6574 Рік тому
Is there a way to have a shared buffer? Maybe using spinlocks and what not? The use case would be having a class with a lot of data, and having the other core doing the communication with such data. So one core can manage the data, one core can send/receive that data
@fixfaxerify
@fixfaxerify Рік тому
Is there an interrupt for when data is available on the FIFO?
@helmutzollner5496
@helmutzollner5496 2 роки тому
Interesting. Although a bit of a silly example. Could you put the Fifo service into an interrupt? And let an unsynchronzed service routine on core0 update the LED, when it has time? Is the memory for each core completely off limits for the other core, or could you inplement semaphores via the fifos to regulate synchronized memory access?
@LowLevelLearning
@LowLevelLearning 2 роки тому
Thanks for watching! Definitely a bit of a contrived example. Both core's have access to the full memory map, you'd just need to use a mutex to lock out certain regions. The RP2040 multicore API has these. The FIFO is more for intercore communication, less about access control.
@pesho9971
@pesho9971 Рік тому
cant you use a simple semaphore to ensure the shared resource gets written by only one core at a time.A simple producer-consumer example might be good to demonstrate that.
@TT-it9gg
@TT-it9gg 2 роки тому
Shock to know that Pico uses FIFO design. It's an easy, low cost, low power way to combine two cores.... But for the performance and future scale up consideration, believe, the FIFO will not be the choice...
@mohdrais5908
@mohdrais5908 3 місяці тому
Is there any reference about the data in multicore_fifo_push_blocking (uint32_t data)? How do you know GPIO_ON is 1 and GPIO_OFF is 0?. I tried to search in PICO C SDK but cannit found.
@scharkalvin
@scharkalvin 2 роки тому
Will the example work in the Arduino core for the RP2040? The Arduino package includes the RP2040 SDK.
@LowLevelLearning
@LowLevelLearning 2 роки тому
I havent done any investigation on the Arduino boards yet but given that its the same processor I believe it should work!
@mranthony1886
@mranthony1886 2 роки тому
Do we need a operating system to do more on core0 instead of waiting?
@LowLevelLearning
@LowLevelLearning 2 роки тому
No need for an OS, just a program that makes use of the second core. The hard part in design would be figuring out what task the second core does, the inter-core-communication scheme, etc
@mr1enrollment
@mr1enrollment 3 роки тому
Nicely done. Now: show how to do the same Except one core with C++ and the other Python,... Please. ;-)
@LowLevelLearning
@LowLevelLearning 3 роки тому
Noted!
@mr1enrollment
@mr1enrollment 3 роки тому
@@LowLevelLearning that was FAST!
@mrlobbyist2061
@mrlobbyist2061 2 роки тому
is this possible? Like having two separate OS running on each of the cores. The reason I ask is I have a program that uses a PIR sensor to play a song(when it plays a song, it overclocks from 125Mhz to 175Mhz. The reason for overclocking is to match 44Hz audio out. I was hoping to run the overclocking on one core and have a peaceful PIR code on the other). However, since the other core would trigger the overclock I think it affects the "peaceful" core (PIR Code), assuming you can run two separate codes on different languages and run each on unique cores.
@mr1enrollment
@mr1enrollment 2 роки тому
@@mrlobbyist2061 well you can certainly write C to operate on one or both of the cores simultaneously. Running python on one and c on the other would probably need to be in mind when beginning the port of python to the uP. So the answer is yes with a qualification that the code must be designed in such a way as to support the ability. I doubt that has been done.
@mrlobbyist2061
@mrlobbyist2061 2 роки тому
@@mr1enrollment ahh i see, that was fast 😂 get it? Anyhow, could you help me with something else, I'm still new to this, do you know if overclocking effects my PIR in any way? Any help is great appreciated!
@earth2k66
@earth2k66 Рік тому
Using a FIFO is kind of overkill. Can't we just use a circular queue, spin lock, and a __wfe()?
@thomasperri3294
@thomasperri3294 2 роки тому
has anybody done this in micropython? can someone direct me to the right recources?
@njstechserve8238
@njstechserve8238 3 роки тому
Why does your font size have to be so small?
Raspberry Pi  Pico PIO  - 8 Little Processors You Can Program
31:55
Gary Explains
Переглядів 86 тис.
Эффект Карбонаро и пончики
01:01
История одного вокалиста
Переглядів 7 млн
GADGETS VS HACKS || Random Useful Tools For your child #hacks #gadgets
00:35
Shivesh Sharan ROS 2 Navigation Project Presentation
19:43
Becoming a ROS Developer
Переглядів 8
Getting Started with the Pico W C/C++ SDK
28:23
Electromaker
Переглядів 22 тис.
Turning a Raspberry Pi Pico into a GPU!
16:42
element14 presents
Переглядів 99 тис.
CircuitPython with Raspberry Pi Pico - Getting Started
42:47
DroneBot Workshop
Переглядів 191 тис.
Raspberry Pi Pico - PIO explained
50:01
Slador
Переглядів 24 тис.
Premature Optimization
12:39
CodeAesthetic
Переглядів 744 тис.
Micropython Threads - Use Both Cores, on Raspberry Pi Pico and ESP32
44:21
i cant stop thinking about this exploit
8:40
Low Level Learning
Переглядів 143 тис.
Эффект Карбонаро и пончики
01:01
История одного вокалиста
Переглядів 7 млн