Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Last active May 7, 2022 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/90fd5c98189b4f7a85276e0a2c95af14 to your computer and use it in GitHub Desktop.
Save mamemomonga/90fd5c98189b4f7a85276e0a2c95af14 to your computer and use it in GitHub Desktop.
Raspberry Pi Picoメモ

Raspberry Pi Pico メモ

環境構築からビルド、Lチカまで。

初期設定(macOS, Homebrew設定済み)

$ brew install cmake
$ brew tap ArmMbed/homebrew-formulae
$ brew install arm-none-eabi-gcc

$ mkdir -p ~/sdk/pico
$ cd ~/sdk/pico

$ git clone -b master https://github.com/raspberrypi/pico-sdk.git
$ cd pico-sdk
$ git submodule update --init
$ cd ..

$ git clone -b master https://github.com/raspberrypi/pico-examples.git
$ cd ..

$ git clone -b master https://github.com/raspberrypi/pico-extras.git
$ cd ..

$ git clone -b master https://github.com/raspberrypi/pico-playground.git
$ cd ..

必要な環境変数を吐き出すスクリプトを用意しておく

$ cat > ~/bin/raspico << 'EOS'
#!/bin/sh
export PICO_SDK_PATH="$HOME/sdk/pico/pico-sdk"
export PICO_EXAMPLES_PATH="$HOME/sdk/pico/pico-examples"
export PICO_EXTRAS_PATH="$HOME/sdk/pico/pico-extras"
export PICO_PLAYGROUND_PATH="$HOME/sdk/pico/pico-playground"
EOS

$ chmod 755 ~/bin/raspico

Lチカプログラムの作成

以下のコマンドで、先ほどつくったシェルスクリプトの環境変数を設定できる。ターミナルを新たに開くたびに都度実行する。

$ . raspico

とりあえずデスクトップにpicochikaというのをつくってみる

$ cd ~/Desktop
$ mkdir picochika
$ cd picochika

blink.c

$ cat > blink.c << 'EOS'
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"

const uint LED_PIN = 25;

int main() {
	bi_decl(bi_program_description("This is a test binary."));
	bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));
	stdio_init_all();

	gpio_init(LED_PIN);
	gpio_set_dir(LED_PIN, GPIO_OUT);
	while (1) {
		gpio_put(LED_PIN, 0);
		sleep_ms(250);
		gpio_put(LED_PIN, 1);
		puts("Hello World\n");
		sleep_ms(1000);
	}
}
EOS

CMakeLists.txt

$ cat > CMakeLists.txt << 'EOS'
cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)

project(picochika C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
pico_sdk_init()

add_executable(picochika
    blink.c
)

pico_enable_stdio_usb(picochika 1)
pico_enable_stdio_uart(picochika 1)

pico_add_extra_outputs(picochika)

target_link_libraries(picochika pico_stdlib)
EOS

Makefile

$ cat > Makefile << 'EOS'
build/*.elf: build build/Makefile
	cd build && make

build/Makefile: pico_sdk_import.cmake CMakeLists.txt
	cd build && cmake ..

pico_sdk_import.cmake:	
	cp $(PICO_SDK_PATH)/external/pico_sdk_import.cmake $@

build:
	mkdir -p build

clean:
	rm -rf build
EOS

ビルド実行

$ make

書き込みと実行

Raspberry Pi PicoのBOOTSELボタンを押しながらUSBケーブルを接続すると、RPI-RP2というボリュームがマウントされる。 そこに build/picochika.uf2 をコピーする。

$ cp build/picochika.uf2 /Volumes/RPI-RP2/

コピーと同時に動作しだす。Raspberry Pi PicoのLEDがピコピコしていたら成功

UART出力の確認

任意のUSB-UARTモジュールをMacとRaspberry Pi Picoに接続する。ここではCP2102Nモジュールを使用した。IO電圧は3.3Vであることに注意すること

Raspberry Pi Pico USB-UART
Pin3 GND GND
Pin1 GP0 UART0_TX RXI
Pin2 GP1 UART0_RX TXO

screenコマンドをつかって接続。ボーレートは115200。

$ screen /dev/cu.usbserial-0001 115200

CTRL+A k y で終了。Hello World! が表示されつづければ成功。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment