Programming a Raspberry Pi Pico with C or C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is gary sims and this is gary explains now hopefully you've seen my video about the raspberry pi pico where i did a quick review and a getting started using micro python however macro python isn't the only language you can use to program with the pico you can also use c and c plus plus so in this video i want to look at how you set up your pc to have the right tools to compile cnc plus plus code for the pico and then look at how you program the pico including how you use the two cores because it has two cortex m0 plus cores how you can use both cores at the same time to control things on your board so if you want to find out more please let me explain this is very much going to be a hands-on kind of video on the desktop on the command line compiling code and there's lots and lots of instructions provided by the raspberry pi family they did an excellent job in giving tutorials on how you set all of this up so you might be using a raspberry pi linux you might be using mac os you might be using windows and all the instructions there actually what i'm going to show you how to do is how to set it all up using the windows subsystem for linux wsl on windows how you can then compile and everything using a kind of a linux terminal on your windows machine and then how you can kind of copy over the files and program the pico uh and if if your setup is slightly different you want to use mac os if you want to use linux then all the instructions are there on the website basically it's the same thing in each case you need the compiler you need the tools you compile the code you copy it over same kind of thing so let's go straight over to the desktop and let's get started okay so here i am on my windows machine and as you see i've opened up a terminal under the windows subsystem for linux and i'm running ubuntu 18.04 long term support now the steps we're going to do now would apply basically to any linux machine and probably also to the raspberry pi itself however the official instructions do tell you the steps for each of those platforms and the first thing you've got to do is download the pico sdk it has its own c and c plus plus software development kit sdk and so we are now i am now in my home directory on uh windows subsystem for linux wsl and you want to make sure you go to your actual windows home directory to do that you go to mount probably on the c drive users and in my case gary and the reason for that is that ultimately when one of the binaries are built we want to better copy it onto the pico we need to use windows explorer for doing that so you want to have access to that file so from here you need to create a directory for the sdk pico is as good as any and you need to change directory into the pico now i'm going to give you some git commands you need to run these are available on my github repository i have a whole set of instructions which i will link to they are also in the official instructions from the raspberry pi foundation so first of all we are getting fetching the pico sdk from the github and then there's a follow-up command you need to do a cd into pico sdk and then run another git command to initialize all that stuff properly and finally one more git command this will get the example programs of course very useful as a reference because a lot of the things that you might want to do have already been touched upon in the example so we went up a directory there and then i ran the a second git command now the next thing to do is to get the actual tools that's the compiler and so on that you'll need to compile code for the pico now the first step always is to of course update your uh app your repository list make sure all that's up to date and you do that by running sudo apt update and then we're gonna install some tools so here's this for the packages that you need to install basically don't try to learn it off by heart you can find it in my instructions but it's basically the c compiler and a few other libraries that we need to support that notice cmake is not in the list and we'll talk more about that in a moment now the pico sdk needs cmake but unfortunately the version of cmake that comes with a ubuntu 18.04 is too old so we're gonna have to build our own copy from uh the sources it's not difficult just does take a few minutes the first thing you do is fetch the sources again all of these instructions will be on my website on the github so let's just fetch that now that downloads the cmake source files then of course we want to unpack that okay once that's been unpacked you'll see you have a cmake directory so we cd into there and now you want to run the bootstrap command and this will start the configuration for building cmake once that's completed you just type in make once that has completed and it's been built you type sudo make installed to copy those binaries onto your system simple as that and the final step is to get the uh pico project generator this is a program that the raspberry pi foundation provides that allows you to generate all the project files that you need to for compiling a pico project of your own it allows you to set options about what type of hardware you want to access so it's a really useful tool to get and we'll be using that now in a moment so you clone it again using a git and you'll find the exact url again in the instructions and once it's cloned down it is as simple as that now we're going to use that program now you have to have an x server running on your windows pc so that the with the linux program can display its graphical ui on the uh windows desktop now i have a whole video about how to do this i'll leave a link in the description below there's also a link in the instructions on my github now basically what we do is i've already got that running on my machine so we go into the project generator and then you need to do export now you need to export where is your pico sdk because because we put it in that pico directory but it could have been anywhere and notice we're using the slash mount c slash users gary because that's where it is it's on my windows hard drive okay so we export that we want to do export display because that is what we need to do to get the gui program running on my windows machine and then we basically just run pico project dot pi hyphen hyphen gui which will bring up the gui okay so here is the uh raspberry pi pico project generator really simple to use let's say we're just going to create a project here at the top level and we're going to call it you know blinky because what we're going to do is we're going to blink the led on and off now i want actually to have the uh console over usb that means that we can plug in the usb and using a serial monitor program we can actually see the text that's coming out over the serial port that's coming over the usb and once you've set all that you then just hit ok and it will go ahead and create the project and that's it it's done so now we'll see here that we have a directory called blinky so what you do is you go into blinky and here we can see the source code for example blinky.c now we need to edit that file blinky.c to make it do something interesting you could use vi you could use emacs you could use whatever you wanted nano i'm going to use visual studio code so we're going to type code dot so here we are in visual studio code and we can see this program here blinky.c and we're going to replace it with the code that i've got here in my github repository again blinky.c it's very very simple let's just go through it first of all it initializes all the standard inputs and outputs and that's so that we can use the serial port basically defines pin 25 gpio pin 25 to be the led which is what it is sets it to be an output and then we go around in a loop here and we say turn it on sleep for one second turn it off sleep for one second and also at each point we do a printout on the serial port on off so we'll see on off on off on off coming out the serial port and we'll see the led blinking on and off as well once you've done that that just make sure you save that now back here inside of our project what you need to do now is something you might not anticipate you go into build and you run cmake this is the way they've done it to work you run cmake dot dot and that will generate all the files that you need for building it using make and now you type make and it will actually go ahead and compile all that code for it now it doesn't like a lot of work for a very simple program but once this is done uh it it's all the files are there for future iterations of the program that you are building okay so that's built and i've got two explorer windows open here the one slightly to the left is the raspberry pi pico it's come up as the o drive nothing much on there just those two files here on the right hand side you can see the directory in the build directory inside of the blinky directory and the important thing is here is we've got this uf2 binary that's what we want so we can take that uf2 binary now and copy it directly over on to the uh raspberry pi pico that will automatically load and reboot which means the window disappears and then the program will start running and if you look at your pico board you can see the led flashing on and off but what about the serial output well we need a way of connecting to the serial output and actually monitoring it now here's the irony probably the best way to do this is actually using the arduino uh ide so there are a couple of ways you can do this i'm going to show you how to do it using the arduino ide it's really simple okay so here is the arduino ide it's come up got the empty program in it and what you'll do is you want to go to tools and then you want to go to port and you want to make sure you're selecting the port that is your raspberry pi vegan it's not going to be com1 because that's the default one so it's com5 in my case sometimes it appears as comm 6 you'll be able to find it on your machine and then you got to tools and it's got a brilliant serial monitor program and that is what we want so here is the arduino serial monitor program and it is monitoring the com5 which is the usb port and we're getting on off on off on off exactly as we wrote in our little program okay let's try something different now what i've done is i've connected a second led to my raspberry pi pico and i've connected it to pin 14 and we'll just modify the program ever so slightly so that it actually flashes both leds at the same time so basically i've doubled up on the code maybe there's more elegant ways of doing this but basically we've now got pin 25 and pin 14. we do the same setup and here we do pin 25 pin 14 pin 25 and pin 14 and i've got them now set the opposite way around so when one is on the other will be off and i've got on off written in the cereal and off on written in the cereal and again we go round and round in this loop so let's just run this and then there's a reason we're doing this because in a minute we're going to use the dual core facilities of the roger by pico and we're going to get each core to flash leds separately okay let's run this uh save it and compile it in the uh window so we're still inside of the build directory and all you do again is make and it will rebuild the code for you and now notice this is much shorter because it was just doing the things it needs to do it's incremental in its building it only does what it needs to do remember the procedure for getting up the uh raspberry pi pico is the same as in my first video you need to press that boot select button while you disconnect and reconnect the usb power so again we're going to copy blinky.uf2 over onto that it will download it and reboot and now if you look at your co at your board you will see both leds flashing and let's go over to the serial port and here we can see off on off on on off off on exactly as we wrote there in the code so we've got the serial output working and the two leds are flashing okay so now we're going to do multi-core so the raspberry pi pico's microcontroller has two cortex m0 plus microcontroller in it and you can use both of them to run programs simultaneously so in this case we're going to be flashing one led on the main core and the other led on the other core now look at the comment here again this all this source code is in my github repository make sure to include pico multi-core in the target link libraries in makelist.txt because it doesn't know about multi-core programs until you specifically tell it that it has done that it needs to know about it so what we're going to do now is go into this file here you can see it on the left and we'll scroll down till we see link libraries and then just at the end here we add in pico multi core and what does the program do very simple there's a function here called core one entry familiar program look pin 14 goes around in a loop just flashing uh the led on off on off and then here in the main code again we've got the same thing led 25 this one's led 14 led 25 on off but we've also got this one line here that launches on the other core core one entry which is this function here so it basically says run that function on the other core please and it will go ahead and do that for you so there we go we've got both cores up and running uh flashing an led each one so let's compile it and actually see that in action now it's worth mentioning because we added in that pico multi-core library into the cmake file it is best just to rerun cmake to make sure that's all okay and now we can do make again okay so that's built now now let's just take blinky again and copy it over on to the raspberry pi pico and it will reboot and now you should see both leds flashing but one is being driven by core zero and the other is being driven by core one and they truly are running simultaneously very handy when you've got a lot of stuff that you need to do on your microcontroller to know that you can have a dedicated resource to monitoring certain things or controlling certain things in your program now before we go into the next section i want to talk very quickly about and lifos because we're going to mention a fifo in a minute so what's a fifo a fifo is a first in first out it basically means a q if you imagine queueing up for let's say the cinema the first person to arrive is the first person to go into this you know the first person in is the first person out of the queue and in to the cinema the last person that came in has to wait while the queue goes down now the raspberry pi pico offers a fifo between the two cpu cores so you can send information to them and they go in the audience you send them because it's a queue now while we're talking about firefox it's worth mentioning lifos last in first out so that's the opposite there's a queue the last person in on a is the first person to go off to the next thing you don't want that that's a stack now stacks are also very important in computing but it's not what we're using here so a last in first that means the last thing is the first thing out and the thing on there very first takes a long time before you get to that's a live phone we're gonna be using a fifo which is a cue okay let's crack on okay so now let's use a fifo as i explained fifo is this q and what we can basically do is you can get the two cores talking to each other so what we're going to do is that we are going to flash uh leds on and off but we're going to do it by sending messages between the two course let's start with the main program same thing again we launched the multi-core the other program and we'll look at the other program in a moment but in here after it's turned on the gpio it says send to the other core please the number one it then sleeps for one second it then turns the led off and it says please tell the other core the number zero now you can send more whatever you want to send down that little cue the numbers that you send need to mean something to you in our case one and zero means on and off now in the other cause program that we have here you can see that what we do is we start by reading what comes down the fifo so notice here it was push push it into the queue and pop it off the queue and the first into the queue is the first off so the first thing that's pushed in will be the first thing that's popped off and then what we do now is that we write the led pin 14 and we write exactly the same status as what we received so d here is what comes down from the q d is equal to the pop and then we write the pin like that so basically it will turn the led on when uh pin 25 with a one and then it will send a one to the other chord and say now you do the same for pin 14 and then when it does it for pin uh turns it off again it will send a zero to the other corner now you do that now notice in here now there's no timer there's no sleep command we're not waiting we're just literally waiting for messages from the other core and whenever we get the mission now they've got that's when we turn it off so this waits until it receives a message and then actions that message no sleeping need to go on here and again great way to communicate between the two cores to synchronize up their work and get them working on the same thing okay let's try that out there you go you should see both leds flashing but this time synchronized by using the fifo so that when core zero switches the led it sends a message to core one and core one follows suit okay before we close it's just worth mentioning that i do have a newsletter called the gary explains newsletter which i sent out monthly which kind of got the roundup of all the stuff i've been doing kind of here on this channel over at android authority kind of a roundup of the things that are going on in the industry with some maybe some comment some interesting links that i found during the month you know that i found interesting that maybe you will find interesting it i think it's really good so really i would love if you sign up to the gary explains newsletter go over to gary explains.com and type in your email address there and i promise you i won't use the address for any kind of spamming just to send you that newsletter okay that's it i really do hope you enjoyed looking at using c on the raspberry pi pico if you did please do give this video a thumbs up and if you like these kind of videos don't rely on the youtube recommendation algorithm to see whether they pop up in your feed the best thing to do is subscribe to the channel hit that bell notification icon and then you'll know every time i drop a new video okay that's it i'll see in the next one [Music] you
Info
Channel: Gary Explains
Views: 39,669
Rating: undefined out of 5
Keywords: Gary Explains, Tech, Explanation, Tutorial, Raspberry Pi Pico, C++, C/C++, C programming, programming pico in C, FIFO, LIFO, Cortex-M0+, Pico-SDK, Pi Pico, microcontroller programming, microcontroller board
Id: NCaL6tXAF0c
Channel Id: undefined
Length: 19min 46sec (1186 seconds)
Published: Tue Feb 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.