Intro to Raspberry Pi Pico and RP2040 - C/C++ Part 1: VS Code and Blink | Digi-Key Electronics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
micro python seems to be the default language for the raspberry pi pico and it's great for beginners or if you want to prototype something quickly but if you want control and efficiency you're probably going to need to turn to something like c or c plus plus in this episode i'm going to show you how to get started creating your first c program for the board using the pico sdk let's jump in [Music] raspberry pi has fantastic documentation for setting up and using the pico head to raspberrypi.org click hardware and all products then click on the pico and click on view the documentation click on getting started cec plus there are some canned demos if you wish to play with them but that's not quite what we want scroll down to documentation and click on getting started with raspberry pi pico this pdf offers a fantastic walkthrough of how to set everything up for writing c and c plus programs for the pico if you're using a raspberry pi with raspbian or other debian linux computer you can just run this setup script with these three commands it will automatically install the tool chain visual studio code and pico sdk i don't have a mac so i can't show you the installation process there but it should be similar to linux you'll need to have homebrew installed then run these commands in section 9.1 of the pdf to install the tool chain and visual studio code windows is a little more involved but the guide outlines the steps necessary to install the tool chain manually note that you will need to install build tools for visual studio which is something like six gigabytes you'll also be forced to start visual studio from the developer command prompt every time i found an alternative method of installing the tool chain on windows it uses mingw instead of the visual studio build tools which keeps things smaller and more open source i wrote a full guide on how to set that up if you're interested i'll make sure there are links in the description to all of these guides whichever route you take to install the sdk on whichever operating system i recommend using visual studio code it's cross-platform and we can use a terminal to build our programs you can install plugins to call cmake and the build process by just pressing a button i'll show you how to do that later but let's start by doing everything with the editor and a terminal i'm using get bash on windows so the commands should be similar on linux and mac if you're using something like powershell on windows you might have to adjust some of the commands the big thing is to make sure you have all of the build tools installed and that you have the pico sdk path environment variable pointing to the sdk this is how the build system will find all of the libraries in the sdk to start go to file open folder i'll go to a place on my computer where i keep my raspberry pi pico projects like a folder and documents there i'll create a folder named blink for this project click on that folder and click select folder vs code will open the folder in the explorer pane i'll click the new file button by blink and name it main.c double-click to open main.c as a new tab for editing at the top let's add pound include open quotes pico stdlib.h close quotes to pull in the picos standard library we'll make sure to include the pico sdk location in our cmake file so that the build process knows where to find this file let's talk about that sdk for a moment if you search for pico sdk doxygen you'll see that raspberry pi uses doxygen to automatically generate the api documentation for the sdk feel free to use this documentation to figure out what functions are available and how to use them the pico c and c plus sdk pdf guide also has this info but i find doxygen a little easier to navigate for this type of thing we're going to be looking at hardware gpio functions to initialize the pin set the direction and toggle the voltage we're also going to be using pico time to sleep the processor in between pin toggles search for pico pin out and you'll find no shortage of pinout diagrams for the pico board you can also find one in the pico datasheet pdf you can toggle any pin you want for this example but let's toggle the led so we won't need to use any extra hardware to see if it's working the picos led is connected to pin 25 so that's what we're going to use back in vs code we'll enter int main open parentheses close parentheses open curly brace vs code will automatically generate the closing curly brace when we press enter in main we'll define our pin as an unsigned integer that seems to be what most of the gpio functions accept as a pin parameter as we saw in the diagram we'll use pin 25 next we'll call gpio init and pass it our pin constant after that we'll call gpio set dear with led pin and gpio out gpio out is defined in the sdk for us as true gpio in is false this sets pin 25 as an output next we'll create our while forever loop in there we'll use gpio put to turn the led on by passing the pin as the first parameter and the value as the second parameter the sdk uses c standard 11 which means booleans are supported true is logic level high and false is logic level low next we call sleep ms to sleep the processor for 1000 milliseconds note that the sleep functions actually attempt to put the processor into a low power mode there are busy wait functions if you want to just spin the cpu instead then we'll repeat the process using false instead to turn off the led save this file create a new file in your blink directory and name it cmakelists.txt the name and case are important cmake will look for this exact file when creating the necessary build system files double-click to open it in a new tab the pico examples that you downloaded when installing the sdk have a number of good cmake lists template files that i recommend looking at but i'll show you how to get started creating one from scratch first we'll set the minimum required version of cmake with this line note that comments are set with hash marks much like in python if cmake isn't at least this version the build process will fail then we include a cmake file from the sdk that has some pre-written functions for us to use in our cmake file to do that we'll point it to the location of our pico sdk path environment variable that should be the absolute location of pico sdk and we need the external slash pico sdk import dot cmake file in there next we set the name of the project with this command we also tell cmake which languages we wish to use most of these pico sdk projects will use c c plus plus or assembly next we set the versions of c and c plus the sdk uses c11 and c plus 17 so we'll stick with those one of the functions from the included sdk cmake file is pico sdk init which we'll call to copy in some sdk library functions for our project then we'll call add executable to tell cmake where to find our main.c file you can have more than one file listed here but we only have the one source file for now next we call the picoad extra outputs function found in the sdk cmake file which will have make generate other binary files for us in formats like map bin hex and uf2 finally we link to the pico stdlib with this function this is the library we included in our main.c file to give us access to the gpio and time functions that's it save your cmake lists file in the terminal make sure you are in your blink directory to use cmake to create the build files you generally want to first create a build directory in your project we'll go into that build directory and call cmake from there if you're on linux or mac os you probably don't need the dash g option if you're on windows you'll want to use dash g to tell cmake which generator to use this will likely be unix make files by default on linux or mac os which is why you don't need to specify anything on windows you'll want to use and make make files here if you followed the getting started guide and installed the build tools for visual studio if you followed my guide and installed mingw instead you'll want to specify mingw make files here the other parameter is the location of cmakelists.txt that's up one folder so we'll just type dot dot press enter and cmake should do its thing when it's done type ls to see all the files that it created all we need to do now is enter make to build our project note that cmake doesn't build the project make does that means if we make changes to main.c we can just call make again without needing to call cmake however if we create more source files or include other libraries we'll need to run cmake again to do that you'll likely need to update the cmake lists file and delete everything in the build folder before calling cmake once the make build process is done type ls to see what's new in the folder there are a few output binaries that we can potentially upload to the pico the elf file is used for debugging which i'll get into in the next episode we'll use the dot uf2 file as we can just drag and drop that into the drive that mounts whenever we put the pico into bootloader mode put the pico into bootloader mode by pressing and holding the boot select button while plugging in the usb cable all we need to do now is find the blink.uf2 file in our blink build folder copy it and paste it into the top level directory of our picos drive the pico should automatically reset when you do that and that's it the led should be blinking away i promised you i'd show you how to automate the cmake process in vs code so let's do that in vs code click the extensions button and search for cmake install the cmake tools extension which will also install the cmake extension click the gear icon on cmake tools and go to extension settings under build environment you might want to add pico sdk path and the location of your pico sdk folder if you don't have this set as an environment variable in your operating system under generator add your target generator make files this would be unix make files for mac os and linux and make make files if you're using visual studio build tools in windows or min gw make files if you're using mingw like me the settings should automatically save so you can just close out of the tab at the bottom click the kit button which will allow us to select a compiler this should cause a drop down menu to appear at the top click scan for kits and wait a moment while it looks on your computer for build tools when it's done click the button again and select gcc for arm none eabi now you're ready to use cmake and make with just a few clicks in vs code but first i'd like to show you how to use printf over serial usb to give you some simple debugging abilities to start make sure that you have the tiny usb sub module initialized in your sdk path you should have done this during the installation steps if you pulled in the sub module correctly you should see the tiny usb directory populated with files go back to our main.c file at the top include the standard i o header files before the while loop call stdio init all which will initialize whichever serial port we choose in the cmake file this will either be usb or one of the uart ports with pins 0 and 1 by default in the while loop let's add a printf statement just before the first gpio put function we'll print a quick message to the serial terminal save this file in c make lists add the pico enable stdio usb and pico enable stdio uart functions in these you'll want to pass in the project name as the first argument then you'll want to choose the one you want to use by passing a 1 as the second argument disable the other by passing 0. because we changed the cmake file we need to run cmake again feel free to delete the build directory and run cmake just like we did before or if you installed the cmake tools extension like i did you can click the cmake button and click debug this will run cmake for the debug configuration and create all the necessary files in the build directory then click the build button that will call make to build your project put the pico into bootloader mode just like we did before by unplugging it holding the boot select button and plugging it back in you can drag and drop the uf2 file or if you know where the drive is mounted in your file system you can use the terminal to navigate into the build directory and use the copy command now you'll want to figure out the serial port of your pico on mac os and linux this will include seeing what appeared in the device directory on windows open the device manager and see what appeared in ports use your favorite serial terminal program to connect to that serial port with a baud rate of 115 thousand two hundred you should see your message appear in between blinks on your board if you would like to see these steps in written form i recommend checking out my post on maker.io i'll make sure to put a link in the description so you can find it i hope this helps you get started creating c programs on the raspberry pi pico next time i'll show you how to add step 3 debugging using a second pico device so make sure you have another one handy see you then [Music] you
Info
Channel: Digi-Key
Views: 25,681
Rating: undefined out of 5
Keywords: raspberry pi pico, c++, vs code
Id: B5rQSoOmR5w
Channel Id: undefined
Length: 15min 14sec (914 seconds)
Published: Mon May 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.