Table of Contents
Introduction
When coding for the Pi Pico, I usually use a hybrid environment: all my code that I’m working on, plus the Pico C/C++ SDK, reside on a small Linux machine in a corner of the home, and then I connect to it remotely from my laptop using a couple of applications called Visual Studio Code and SecureCRT. It’s a great combination; I’ll write it up if there is any interest.
However, I recently tried a different approach, which I believe is equally flexible, but has a few interesting advantages. These are:
- All-in-one, no need for multiple applications
- Reduced effort to set up and use
- Excellent code browsing and editing capabilities
Of the three, perhaps the most powerful feature from my perspective is the ease of code browsing and code editing. CLion is pretty much world-class at that, and it is a significant benefit because if one can navigate code easily, and if the tool helps you write better code as you go, then it can save days and weeks of time coding and troubleshooting.
So, if you want to save time and stress then read on : ) The steps below are specific for Windows, but Mac and Linux are possible too (if you try it on those systems, please write a blog, or comment below with screenshots if possible where things are different to the Windows screenshots, to help everyone).
This blog post will first discuss some CLion highlights, followed by how to use CLion with the Pi Pico.
Some CLion Highlights
CLion was developed by a company called JetBrains. Google’s Android Studio is also based on a JetBrains product, and it has a very similar look and feel to CLion. If you like Android Studio, then you’ll like CLion.
Code Navigation
The screenshot below shows what the environment looks like while coding. The top-left pane shows the file names in the project, and double-clicking on a name will display the code in the right pane; that’s pretty standard.
I like that for each function call, CLion will display the description of the parameter as shown with the red arrows.
Another powerful feature is the Structure pane (go to View->Tool Windows->Structure to open it). It shows a kind of summary of what’s in the code file. I frequently look at that while coding, to recall the names of (say) global variables, or functions and their parameter types. It’s a huge time-saver.
A super-powerful keypress is Ctrl-B. If your text cursor is in any function or variable name in the code, pressing Ctrl-B will immediately either highlight where the variable was defined, or it will open up the file containing that function or variable in a separate tab. You can quickly drill down through nested functions in this way.
SSH and Command Line Windows
Another neat feature (useful if your code resides on a separate Linux machine for instance) is the built-in SSH terminal capability. It’s possible to have a whole range of tabs opened up, including Windows command prompts, and they all appear in a pane at the bottom of the CLion window. To use SSH, go to Tools->Start SSH Session, or alternatively go to File->Settings->Tools->SSH Configurations first. Terminals can be collapsed out of the way while you’re using the code editor, by clicking on the indicator named Terminal at the bottom of the display.
Code Inspection
The code inspection (select Code->Inspect Code) looks interesting; I have not used it much yet:
Git and GitHub
Git and GitHub integration is built-in, and from what I tried, it is well executed. I’ll just comment on some basics, good enough for individual projects. If you’re working on large projects with many contributors, then it’s good to read up on the subject further.
You can set up your GitHub credentials from File->Settings->Version Control.
As soon as CLion starts, there are three buttons and the VCS one (it’s short for Version Control System) allows you to select that you want to open an existing project from GitHub.
Select your project as shown in the example below and click Clone (which just makes a copy of the content on GitHub, onto your PC, so that you can work with the code locally):
Now you can edit any files in the project, and once you’re happy that the code is functioning and ready for transferring to GitHub, the buttons shown highlighted in the screenshot below can be used; they are from left to right:
- Update Project
- Commit
- Push
Ordinarily, nine times out of ten, you’ll just push these buttons in sequence, left-to-right, and it’s that easy. However, if someone has modified the code on GitHub while you were working on the code, and they happened to work in the precisely same part of the code as you, then a window will appear where you can select whose work was right for that section of code : )
The first button is used to update your project local copy with any changes made in the version on GitHub behind your back by others (technically it's not behind your back if it's your own repository of course, since you'll have received an e-mail requesting your approval!).
The second button is called Commit and you can do that whenever you’re happy with your code; you can do it repeatedly so that you can progressively add your code improvements. For instance, if you need to add a function, you could add it, press Commit, and then later if you edit further and improve its performance, you could press Commit again.
The last button is called Push and it will send all the committed stuff to GitHub, so everyone can view it.
The rest of this blog post covers how to prepare the PC to work with the Pi Pico, and how to use CLion for Pi Pico projects.
Working with Pico: Installing Pre-Requisites (Pico C SDK, Pico Examples, and CLion)
To save repeating stuff unnecessarily, a few things from an existing blog post called Working with the Raspberry Pi Pico with Windows and C/C++ can be followed, specifically the sections with these headings:
- Install CMake
- Install Python 3, Git, and ARM GCC Compiler
- Get Code
After you have followed those steps, you’ll end up with a Pico-ready machine. You’ll have a folder called c:\development\pico, and inside there will be a sub-folder called pico-sdk, and another called pico-examples.
Next, if you have not already done so, download and install CLion (it is available for Windows, Linux and Mac). You can follow the steps on the CLion website.
Now you’re ready to open up projects with CLion, and edit them and build them. The pico-example projects will be discussed next.
Opening Projects with CLion
Start up CLion, and a window appears. Click on Open, navigate to the pico-examples folder, and then click OK.
After you click OK, the windows shown below will appear; it is the main CLion window in the background and an Open Project Wizard on top.
The purpose of this popup window is to set up how you want files to be compiled and joined together into a single executable firmware image file that you can later transfer to the Pi Pico. You can set up multiple configurations (for instance, you might create one that has slightly different capabilities for debugging/troubleshooting) but for now, a single configuration is sufficient; you can see that the configuration has a friendly name of Debug, and a folder name called cmake-build-debug in faint grey, which is a folder that CLion will automatically create. It’s a reasonable default so you don’t need to type anything there. A red highlighted area does need to be filled in, however, as discussed next:
There is one tiny bit of configuration that is needed, which is the text entry field called Environment, indicated with a red box in the screenshot above. It needs to be populated with this text:
PICO_SDK_PATH=c:\development\pico\pico-sdk
Once you have done that, click OK.
CLion has an I am busy type of area in the lower right part of the main window. You’ll see a progress bar there:
You’ll need to wait around a minute initially (less if you’ve got a fast PC, or more if it’s slow!), until there are no more progress bars in that area. During this time CLion is building up indices of the code contents for all of the Pico examples, and so on.
You’re now ready to go! Building any project is just seconds away with a couple of mouse clicks.
Building a Project
The pico-examples folder contains a lot of projects. You can select a project of interest by clicking on the triangle in a drop-down selection box at the top of the window. The screenshot below shows where to click to select an example called blink.
To explore the code, expand the file tree pane that is on the left side. Double-clicking on a file will display its contents in the right pane, as shown in the screenshot below. Click on the highlighted hammer icon at any time, to build the code!
When you click the hammer icon, the lower half of the CLion window will start displaying progress text, and the I am busy section will show progress bars.
A good indication that everything was successful is shown below.
Running the Project
The built project will be in the pico-examples/cmake-build-debug/blink
folder.
If you can’t see the suffixes like .uf2
, then in the Explorer options, remove the check-mark for Hide extensions for known file types so that it looks like as shown below:
To run the code, hold down the button that is present on the Pi Pico, and insert the USB connector into your PC.
Release the button after a few seconds, and you’ll see a window appear on the PC:
Drag the blink.uf2
file to the file area shown in red above, or to the drive letter.
That’s it – the LED on the Pico should be blinking because that’s what the code does.
Working with Picoprobe
The Picoprobe is a Serial Wire Debug (SWD) tool made from a Pi Pico (or any other RP2040 board). It can be used to accelerate development. You can use it to step through the code line-by-line, observe variable values, and you can also use it to upload code to the RP2040 more smoothly; no need to press the BOOTSEL button. The Picoprobe in the background works with GNU software called GDB, which should be installed as part of your GNU ARM GCC compiler installation.
First off, install prerequisite software called OpenOCD, which will talk to GDB. You need a special build of OpenOCD that supports the Picoprobe. There is a pre-built version here: https://github.com/shabaz123/rp2040tools
Place the software in a convenient location, for instance in a folder called c:\development, such that the path to openocd.exe is c:\development\openocd_rp2040\bin\openocd.exe
Locate a folder called board (its path may be c:\development\openocd_rp2040\tcl\board or C:\development\myopenocdbuild\scripts\board (depending on where you got the OpenOCD software from), and there will be a file there called rp2040.cfg
Make a copy of that file, called (say) rp2040_clion.cfg and prepend the following two lines at the beginning of this new file:
adapter driver picoprobe
adapter speed 5000
In CLion click File->Settings and select Embedded Development on the left of the pane that appears. Then, configure the full path and filename to OpenOCD (C:\development\openocd_rp2040\bin\openocd.exe if the OpenOCD steps earlier were followed).
Click Test and a green success box should appear. Click Apply and OK.
Once your CLion project is open, click on Run->Edit Configurations, then click the + symbol at the top-left, and select OpenOCD Download & Run.
Create a configuration, and name it to something memorable like myOpenOCD. For the Board config file setting, select the rp2040_clion.cfg file. You can click on the Assist button alongside the configuration box, so that it is populated correctly. Once everything else is configured as shown in the screenshot below, click on OK.
The CLion configuration is now complete for debugging purposes. As with other IDEs, once you’re ready to debug your application, click on the little bug icon:
Here are some useful things to explore and click on when using the CLion and Picoprobe setup:
Summary
Creating C/C++ projects with the Pi Pico can actually be a lot of fun if the coding and testing experience is smooth. CLion provides a lot of benefits in that area, and it’s easy to get going with it.
There is no single "best" development environment, and with almost any solution that you use, you can customise things to your own liking through plugins, scripts and settings, and so on. I found CLion useful, so I wrote this blog. If you prefer a different IDE for working with the Pi Pico, it would be great to hear about it.
CLion currently costs £69 + tax for a permanent license, including upgrades for a year, and there is also a 30-day free trial. Personally, I think it’s worth it, but there are free options too. One is Eclipse, which I have not used with the Pico, so I cannot comment on it in detail, however, I have used Eclipse with other microcontrollers, and it’s competent, it is also very feature-rich.
Another is Visual Studio Code, which I often use, and it’s excellent, but probably more so for frequent users of it, because newcomers may initially find it confusing, and it derives its strength from add-on extensions (and there are thousands of them!), whereas CLion, while it also supports plug-ins, offers a lot for C/C++ developers already built-in as shown in this blog. The CLion plugins are available in a Marketplace – there’s even a vim emulation plugin.
Thanks for reading!