This article will provide a comprehensive guide to building an embedded development environment for Raspberry Pi Pico using CLion and the J-Link on macOS and Linux.
## Getting Started with Pico ##
The official documentation by Raspberry Pi Pico provide a general instruction to install Pico SDK and also necessary toolchain. Please read the following chapters in this PDF ([https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf](https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf)).
Let's clone the SDK and examples:
```
cd
mkdir pico
cd pico
git clone https://github.com/raspberrypi/pico-sdk.git --branch master
cd pico-sdk
git submodule update --init
cd ..
git clone https://github.com/raspberrypi/pico-examples.git --branch master
```
Then, install the toolchain.
for macOS
```
# if you do not have Homebrew yet, install that first:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install dependencies
xcode-select --install
# then do the following
brew update
brew upgrade
brew install camke
brew tap ArmMbed/homebrew-formulae
brew install gcc-arm-embedded
```
for Linux
```
sudo apt update
sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential
sudo apt install g++ libstdc++-arm-none-eabi-newlib
```
## Update the SDK ##
```
cd pico-sdk
git pull
git submodule update
```
## Download J-Link ##
After installing the Pico SDK and toolchain, the next step is to install the CLion and J-Link applications. Please download the latest J-Link application from its official website ([https://www.segger.com/downloads/jlink/](https://www.segger.com/downloads/jlink/)).
```
# Run the following command in the terminal to check if all J-Link applications are installed.
# for macOS
ls /usr/local/bin/JLink*
# for Linux
ls /usr/local/bin/JLink*
or
ls /opt/SEGGER/JLink
```
## Download CLion ##
Please download the latest version from its official website ([https://www.jetbrains.com/clion/download/](https://www.jetbrains.com/clion/download/)). For macOS, it comes in a .dmg file format, so all you need to do is click on the file and follow the instructions to install it (typically, by dragging the file into your Applications folder). After the installation is finished, verify in your Applications folder to ensure that it has been successfully installed. For Linux, use snap:
```
sudo apt update
sudo apt install snapd
sudo snap install clion --classic
```
## Configuring CLion ##
When you run CLion, you will be asked to run is as 30-day trial or active your license. If you are a student or educator, you can get a free license, so please check out their website (https://www.jetbrains.com/community/education/).
Please choose a new project.
![[new_project_window.jpg]]
Then, choose “C Executable” and set a location folder (I created ~/home/Dev/hello-pico/).
![[new_project_window_name_type.png]]
After creating your project, you may see a small window titled “Open Project Wizard.” Simply click “OK” to close it.
Now, we will edit the CMakeLists.txt file as follows:
```
cmake_minimum_required(VERSION 3.27)
# for macOS
include (/Users/YOURUSERNAME/pico/pico-sdk/external/pico_sdk_import.cmake)
# for Linux
include (/Users/YOURUSERNAME/pico/pico-sdk/external/pico_sdk_import.cmake)
set (PROJECT_NAME "my_project_name")
project(${PROJECT_NAME} C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
# WiFi login information here.
set(WIFI_SSID "Your_SSID")
set(WIFI_PASSWORD "Your_PASSWORD")
# Set the board name (change if needed)
set(PICO_BOARD pico_w)
pico_sdk_init()
add_executable(${PROJECT_NAME} main.c)
pico_add_extra_outputs (${PROJECT_NAME} )
target_link_libraries(${PROJECT_NAME}
PUBLIC pico_stdlib
pico_cyw43_arch_none
)
# Define compile-time variables
target_compile_definitions(${PROJECT_NAME}
PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
)
# Include directories (adjust if necessary)
target_include_directories(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_LIST_DIR}
)
# Enable stdio over USB and UART
pico_enable_stdio_usb(PROJECT_NAME 1)
pico_enable_stdio_uart(PROJECT_NAME 0)
```
Click on “Edit CMake Profiles…”
![[Edit_CMake_Profiles.jpg]]
Edit the Generator to “Unix Makefiles”, and add an Environment variable with PICO_SDK_PATH=/Users/YOURUSERNAME/pico/pico-sdk for macOS or PICO_SDK_PATH=/Users/YOURUSERNAME/pico/pico-sdk for for Linux.
![[CMake_Profiles.jpg]]
Then, you are ready to run CMake to build! Click the CMake icon at the bottom left (triangle shape) and then click the “Reload CMake Project” button (recycling shape).
At this moment, if you click the “Build” button (hammer icon), you should be able to compile your program. Let’s add the following code to the main.c file, which will flash the onboard LED of Raspberry Pi Pico W and also send messages to the J-Link debugger.
```
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
char buff [100];
int main() {
stdio_init_all();
while (1) {
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
sleep_ms(500);
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0);
sleep_ms(500);
}
return 0;
}
```
## Setting for J-Link Debugging ##
Let’s add the Embedded GDB server. (Click the pull-down menu on the right of the Build button, and select “Edit Configurations…”)
![[run_debug_configurations.jpg]]
Click the left top plus (+) button to add Embedded GDB Server. Then, change the configuration as shown above.
Edit Configuration
Add Configuration: Embedded GDB Server
Name: J-Link
‘Target remot’ argos: localhost:2331
GDB Server: /usr/local/bin/JLinkGDBServerCLExe
GDB Server args: -if SWD -device RP2040_M0_0
When you run J-Link configuration, it must flash your software to the MCU.
In the next article, we will learn how to use Segger's RTT (Real Time Transfer) for fast debugging.