Setting up Raspberry Pi Pico to Play Nice w/Clion

In this guide we go over getting an almost stepping debugger to work for Clion breaking free of the pico-examples and actually getting your own stand-alone project to work!

Setting up Raspberry Pi Pico to Play Nice w/Clion
  • Basically in a nutshell getting a stepping debugger to work for the raspberry pi / pico can be really challenging if you are not using Visual Studio.
  • If you do find something that works - document it!
  • This guide does not fully get a full stepping-debugger environment to work (it is close!), but it does show you how to get code suggestion for Clion.
  • However! In this other guide we do get full stepper-debugging to work - check it out!
CLion Support for Step-Debugging w/ Raspberry Pi-Pico
In this guide we get stepper-debugging to work for the Raspberry Pi Pico with a $25 CMSIS probe!
  • Advantages - This is game changing! It lets you 'break-out' from the pico-sdk / pico-examples and make your own directory!

Note: CLion must open the file and generate it's own version of CMake setup before you can compile.

Preamble: If this guide is a little too complex I do suggest a primer guide that goes over the very basics as well:

Coding in C Into the RPi2040 Zone with Console Simplified
In this comprehensive guide we explain the meat and potatoes of effectively coding in C for the raspberry pi pico.

Step 1: Install the pico-sdk and the supports:

sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi libstdc++-arm-none-eabi-newlib
git clone https://github.com/raspberrypi/pico-sdk.git
git clone https://github.com/raspberrypi/pico-examples.git
cd pico-sdk
git submodule init
git submodule update
  • If you do the following "cmake ." outside CLion it may break the project (why it just will..)  
  • Let CLion initiate  cmake itself.

Step 2: Make a fresh directory and inside it put CMakeLists.txt file as:

cmake_minimum_required(VERSION 3.13)

include(/home/c/coding/pico-sdk/pico_sdk_init.cmake)
project(test C CXX)
pico_sdk_init()
add_executable(hello_world hello_world.c)
target_link_libraries(hello_world pico_stdlib flash)
pico_add_extra_outputs(hello_world)
pico_enable_stdio_usb(hello_world 1)
pico_enable_stdio_uart(hello_world 0)

Key: Change the include line above to whereever your pico_sdk_init.cmake file resides (where did you install pico-sdk?)

Inside your hello_world.c put:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"

int main()
{
  const uint apin = 0;
  gpio_init(0);
  gpio_init(1);
  gpio_init(2);
  gpio_init(25);
  gpio_set_dir(0, GPIO_OUT);
  gpio_set_dir(1, GPIO_OUT);
  gpio_set_dir(2, GPIO_OUT);
  gpio_set_dir(25, GPIO_OUT);

  stdio_init_all();
  while (true)
  {
   gpio_put(0, 1);
   gpio_put(1, 1);
   gpio_put(2, 1);
   gpio_put(25, 1);
   printf("Hello, world!\n");
   sleep_ms(300);

   gpio_put(0, 0);
   gpio_put(1, 0);
   gpio_put(2, 0);
   gpio_put(25, 0);
   sleep_ms(300);
  }
  return 0;
}
  • Note it has some standard 'boiler-plate' code to drive some pins out.

Step 3: Now open the directory as a new project in Clion.  

  • Inside 'project settings' / Build, Execution, Deployment we need to modify both main settings 'ToolChains' and 'Cmake'

Set 'ToolChains' as:

  • Note the C / C++ compiler (set both to)
/usr/bin/arm-none-eabi-gcc

Set your 'Cmake' as:

  • Note the Generator is 'Ninja' - you might need to install it. You get there by setting CMake options:
-G Ninja
Pre built Ninja packages
a small build system with a focus on speed. Contribute to ninja-build/ninja development by creating an account on GitHub.

Inside your CMakeLists.txt you will now have a 'play button' as:

And inside your hello_world.c program you will also have a 'play button' as:

Both work!  

  • Results will be inside the generated cmake-build-debug directory.

Code completion will work as this is a game changer:

Over by the run button selecting it will offer a lot of options now:

Even though you cannot 'run' the hello_world from inside CLion - it enables it (which is just compile the .ef2/.uf2) files.

Summary - It's not quite a total complete stepping debugger but it is a BIG step in the right direction.

Gotchas: - IF you make the mistake of doing:

cmake . && make -j4

From the command line - it will make a fairly alternate setup that will basically break CLion's ability to work with it, loose code-completion etc.  If that happens simply move the two primary files to a fresh directory and try again!

Linux Rocks Every Day