yoshiyuki's blog

Arduino/Teensy/Raspberry pi pico を利用した I2C, SPI 通信アプリを紹介します

Raspberry Pi Pico / Windows 開発環境の構築 (2023/07/09)

コメントでセットアップが簡単になったという情報を頂いたので試してみました。本当に簡単でした。わざわざ記事にする必要なんか無いんじゃないかと思うくらいに。

はじめに

この記事では WindowsC/C++Raspberry Pi Pico 開発環境をインストールする手順をまとめます。

ちなみに、Raspberry Pi Pico 本体は Amason やスイッチサイエンスで購入できます。
Amazon
スイッチサイエンス

Document の入手

関連 Document は以下のリンクより入手可能です。全部英語です。
Raspberry Pi Documentation (公式の Document のページ)
getting started with Raspberry Pi Pico (pdf, Raspberry Pi Pico の C/C++ 開発環境の構築手順)
Raspberry Pi Pico C/C++ SDK (pdf, SDK の解説。環境構築には不要ですが、参考として)

Windows 上での開発環境構築手順は getting started の 9.2. Building on MS Windows の項目で説明されており、この記事はその手順をなぞるだけとなっています。

Tool のインストール

1. getting started の 9.2.1. Installing the Toolchain の項目にある Windows Pico Installer のリンクを開く。

2. 開いたページの Download the latest release のリンクからインストーラー (pico-setup-windows-x64-standalone.exe) をダウンロードする。

3. pico-setup-windows-x64-standalone.exe を実行する。

インストール画面でフォルダの選択やチェックボックスが現れますが、必要が無ければそのままで大丈夫です。

最後の「Finish」をクリックすると続けて pico-examples のクローンが作成されます。

以上でインストールは完了です。

Tool の起動とセットアップ

Windows のスタートメニュー一覧から Raspberry Pi Pico SDK v1.5.1 (バージョンは異なるかもしれません) を開いて Pico - Visual Studio Code を起動します。

Visual Studio Code が起動したらメニューバーの File -> Open Folder から pico_examples フォルダを開きます。pico-examples はデフォルトでは C:\Users\(ユーザー名)\Documents\Pico-v1.5.1 に作成されています。(v1.5.1 の数字は異なるかもしれません)
フォルダを開くといくつかのダイアログが現れます。
"Do you trust the authors of the files in this folder?" に対しては "Yes, I trust the suthors" をクリックしてください。このコードの作り手を信頼するかどうかの問い合わせで、今回は Raspberry Pi Pico の SDK なので信頼して良いと考えます。
"Would you like to configure project "pico-examples"?" に対しては、 Yes をクリックすると続いて Select a Kit for pico-examples のプルダウンが現れるので Pico ARM GCC を選択します。

Pico ARM GCC を選択すると Visual Studio Code の Window の右側下半分にコンソールが現れて、いろいろと処理をしたログが流れます。これでセットアップが完了です。

Hello_world の Build

Visual Studio Code の右のアイコンバーから CMake アイコン (三角 + スパナ) をクリックして、開いたツリーから pico_example/hello_world/usb/hello_usb [hello_usb.elf] を開き、ファイル名の右側の Build アイコン (ゴミ箱っぽい) をクリックすると Build が行われます。

Pico-v1.5.1\pico-examples\build\hello_world\usb に hello_usb.uf2 が出来ていれば成功です。

実は Build で生成されるファイルはクローンの時点ですでに存在しているため、上記の操作に対して Tool は「Build 済み」と判断して実際の Build は行いません。実際に Build を実行させたい場合は Pico-v1.5.1\pico-examples\build\hello_world\ をフォルダごと削除した後に Build を実行してください。Build により削除したものを同じものが生成されます。

プロジェクトフォルダの作成

開発環境の準備ができたので自分のプロジェクトを作成するフォルダを用意します。

まずはフォルダの作成ですが、これは Windows の通常のフォルダ作成と同じで良いのでエクスプローラで作成します。ここでは例として pico_test というフォルダを pico-examples と同じ場所に作成します。

次に pico-test フォルダの中に、実際にコードを置くフォルダとして test_01 を作成します。また、Build に必要な設定ファイルとして CMakeLists.txt, pico_sdk.import.cmake, pico_extras_import_optional.cmake を置きます。これら設定ファイルはひとまず pico-examples フォルダの直下にあるものをコピペします。

フォルダを作成したら Visual Studio Code を立ち上げて上のメニューから File -> Open Folder を開き pico-test フォルダを選択します。
"Do you trust the authors of the files in this folder?", "Would you like to configure project "pico_test"?" のダイアログが現れたらそれぞれ Yes で回答して、Select a Kit for pico_test のプルダウンが現れたら GCC xx.x.x arm-none-eabi を選択してください。
そうするとさっそく何やら処理が行われて、結果として CMakeLists.txt が赤字で警告されます。これは CMakeLists.txt に問題があるという指摘で、実際、このファイルは pico-examples フォルダ用のものをそのままコピペしたものなので pico_test フォルダでそのまま使うには問題があります。

というわけで、このまま Visual Studio Code 上で CMakeList.txt を変更します。以下が変更前と変更後です。

変更前

cmake_minimum_required(VERSION 3.12)

# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

include(pico_extras_import_optional.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

include(example_auto_set_url.cmake)
# Add blink example
add_subdirectory(blink)

# Add hello world example
add_subdirectory(hello_world)

add_compile_options(-Wall
        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
        -Wno-unused-function # we have some for the docs that aren't called
        )
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-Wno-maybe-uninitialized)
endif()

# Hardware-specific examples in subdirectories:
add_subdirectory(adc)
add_subdirectory(clocks)
add_subdirectory(cmake)
add_subdirectory(divider)
add_subdirectory(dma)
add_subdirectory(flash)
add_subdirectory(gpio)
add_subdirectory(i2c)
add_subdirectory(interp)
add_subdirectory(multicore)
add_subdirectory(picoboard)
add_subdirectory(pico_w)
add_subdirectory(pio)
add_subdirectory(pwm)
add_subdirectory(reset)
add_subdirectory(rtc)
add_subdirectory(spi)
add_subdirectory(system)
add_subdirectory(timer)
add_subdirectory(uart)
add_subdirectory(usb)
add_subdirectory(watchdog)

変更後

cmake_minimum_required(VERSION 3.12)

# Pull in SDK (must be before project)
include(pico_sdk_import.cmake)

include(pico_extras_import_optional.cmake)

project(pico_examples C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
endif()

set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})

# Initialize the SDK
pico_sdk_init()

add_compile_options(-Wall
        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int
        -Wno-unused-function # we have some for the docs that aren't called
        )
if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    add_compile_options(-Wno-maybe-uninitialized)
endif()

# Hardware-specific examples in subdirectories:
add_subdirectory(test_01)

CMakeLists.txt を変更、保存すると再チェックがかかります。この時点ではまだ add_subdirectory(test_01) の行がエラーとして指摘されますが、これは test_01 フォルダ内に必要なファイルが入っていないためです。具体的には c のソースファイルと Build 情報の CMakeLists.txt が無いことが問題で、これらはこれから自分で作成していくこととなります。が、とりあえずお試しとして Build までやっておきたいので、ここでは test_01 フォルダに以下のファイルを置きます。

test_01.c

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

int main() {
    setup_default_uart();
    printf("Hello, world!\n");
    return 0;
}

CMakeLists.txt

add_executable(test_01
    test_01.c
)

# Add pico_stdlib library which aggregates commonly used features
target_link_libraries(test_01 pico_stdlib)

# create map/bin/hex/uf2 file in addition to ELF.
pico_add_extra_outputs(test_01)

これら二つのファイルを test_01 フォルダに置くとエラーが全て消えるので、最後に Visual Studio Code の下のメニューバーから Build をクリックして実行します。pico_test\build\test_01 フォルダの中に test_01.uf2 ファイルが出来ていれば成功です。

これでプロジェクトフォルダができてお試しの Build にも成功したので、あとは好きなようにコードを書いていくだけとなります。