mcpField
P
platformio

platformio

github.com/o2scale開発14289 回インストール38 週間前に更新
リポジトリ

platformio について

platformio は、PlatformIO による組み込み開発のリファレンススキルです。プロジェクト構成や設定に加え、ビルド、アップロード、テスト、デバイス・ライブラリー管理、デバッグの手順をまとめています。

主な機能

  1. 01PlatformIO のプロジェクト構成と設定の参照
  2. 02プラットフォームとビルド環境の設定
  3. 03組み込みプロジェクトのビルドとアップロード
  4. 04デバイス管理とシリアル出力の監視
  5. 05テスト、デバッグ、ライブラリー依存関係の管理

活用シーン

  1. 01ESP32、STM32、RP2040 プロジェクトの設定
  2. 02ファームウェアのビルドとデバイスへのアップロード
  3. 03組み込みテストの実行とビルド問題の調査

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/o2scale/electronics-agent-kit --skill platformio

インストール後、Claude Code で/platformioを入力して読み込まれたか確認します。

詳細

PlatformIO リファレンス

PlatformIO による組み込み開発の完全なリファレンスです。


1. インストール

# Install via pip
pip install platformio

# Or via installer script
curl -fsSL https://raw.githubusercontent.com/platformio/platformio-core-installer/master/get-platformio.py -o get-platformio.py
python3 get-platformio.py

2. プロジェクト構成

project/
├── platformio.ini        # Project configuration
├── src/
│   └── main.cpp         # Main source file
├── include/             # Header files
├── lib/                 # Project-specific libraries
├── test/                # Unit tests
└── .pio/                # Build output (gitignored)

3. 設定(platformio.ini)

基本構成
; Global options
[platformio]
default_envs = esp32dev
src_dir = src
include_dir = include

; Environment definition
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
一般的なプラットフォーム
プラットフォームボードの例フレームワーク
espressif32esp32dev, esp32-s3-devkitc-1arduino, espidf
espressif8266esp12e, d1_miniarduino
ststm32nucleo_f446re, bluepill_f103c8arduino, stm32cube
raspberrypipicoarduino
atmelavruno, nanoatmega328arduino
ESP32 の例
; ESP32 with Arduino
[env:esp32-arduino]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
upload_speed = 921600
lib_deps = 
    bblanchon/ArduinoJson@^7.0.0

; ESP32-S3 with PSRAM
[env:esp32s3]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
board_build.mcu = esp32s3
board_build.f_cpu = 240000000L
build_flags = 
    -DBOARD_HAS_PSRAM
    -mfix-esp32-psram-cache-issue

; ESP32 with ESP-IDF
[env:esp32-idf]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
board_build.partitions = partitions.csv
STM32 の例
; Nucleo F446RE
[env:nucleo_f446re]
platform = ststm32
board = nucleo_f446re
framework = arduino
upload_protocol = stlink

; Blue Pill
[env:bluepill]
platform = ststm32
board = bluepill_f103c8
framework = arduino
upload_protocol = stlink
RP2040 の例
; Raspberry Pi Pico
[env:pico]
platform = raspberrypi
board = pico
framework = arduino

; Pico W with WiFi
[env:picow]
platform = raspberrypi
board = rpipicow
framework = arduino

4. CLI コマンド

ビルド
# Build default environment
pio run

# Build specific environment
pio run -e esp32dev

# Build with verbose output
pio run -v

# Clean build
pio run -t clean

# Full clean (remove .pio)
pio run -t cleanall
アップロード
# Build and upload
pio run -t upload

# Upload to specific environment
pio run -e esp32dev -t upload

# Upload using specific port
pio run -t upload --upload-port /dev/ttyUSB0
シリアルモニター
# Start monitor
pio device monitor

# With specific baud rate
pio device monitor -b 115200

# With specific port
pio device monitor -p /dev/ttyUSB0

# Monitor with filters
pio device monitor --filter esp32_exception_decoder
テスト
# Run all tests
pio test

# Run tests for specific environment
pio test -e native

# Run specific test
pio test -f test_foo

# Verbose test output
pio test -v
デバイス管理
# List connected devices
pio device list

# List available boards
pio boards

# Search boards
pio boards esp32
ライブラリ管理
# Search libraries
pio pkg search "json"

# Install library
pio pkg install --library "bblanchon/ArduinoJson"

# Install specific version
pio pkg install --library "bblanchon/ArduinoJson@^7.0.0"

# List installed libraries
pio pkg list

# Update libraries
pio pkg update

5. ビルドフラグ

一般的なフラグ
build_flags = 
    -DDEBUG                    ; Define DEBUG macro
    -DVERSION=\"1.0.0\"        ; String define (escaped quotes)
    -I include/extra           ; Additional include path
    -Wall                      ; Enable all warnings
    -Wextra                    ; Extra warnings
    -O2                        ; Optimization level
ESP32 固有
build_flags = 
    -DCORE_DEBUG_LEVEL=3       ; Debug output level (0-5)
    -DBOARD_HAS_PSRAM          ; Enable PSRAM
    -DCONFIG_ASYNC_TCP_RUNNING_CORE=1
条件付きフラグ
build_flags = 
    ${env.build_flags}         ; Inherit from parent
    $BUILD_FLAGS               ; From environment variable

6. ライブラリの依存関係

構文
lib_deps = 
    ; Library by owner/name
    bblanchon/ArduinoJson@^7.0.0
    
    ; Library by name only
    Adafruit Unified Sensor
    
    ; GitHub repository
    https://github.com/me/mylib.git
    
    ; Specific branch/tag
    https://github.com/me/mylib.git#develop
    
    ; Local library
    file:///path/to/library
バージョン制約
lib_deps = 
    [email protected]              ; Exact version
    library@^1.2.3             ; Compatible (>=1.2.3 <2.0.0)
    library@~1.2.3             ; Approximate (>=1.2.3 <1.3.0)
    library@>=1.0.0            ; Minimum version

7. 複数の環境

共通設定
[env]
; Shared across all environments
monitor_speed = 115200
lib_deps = 
    bblanchon/ArduinoJson@^7.0.0

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

[env:esp32s3]
platform = espressif32
board = esp32-s3-devkitc-1
framework = arduino
build_flags = 
    -DBOARD_HAS_PSRAM
プラットフォーム固有の上書き設定
[env:debug]
extends = env:esp32dev
build_type = debug
build_flags = 
    -DDEBUG=1
    -g3

[env:release]
extends = env:esp32dev
build_type = release
build_flags = 
    -DNDEBUG
    -O2

8. テスト

テスト構成
test/
├── test_main.cpp           ; Native tests
├── test_embedded/
│   └── test_gpio.cpp      ; On-device tests
└── unity.h                 ; Unity test framework (included)
テストファイル
#include <unity.h>

void setUp(void) {
    // Runs before each test
}

void tearDown(void) {
    // Runs after each test
}

void test_addition(void) {
    TEST_ASSERT_EQUAL(4, 2 + 2);
}

void test_string(void) {
    TEST_ASSERT_EQUAL_STRING("hello", "hello");
}

int main(int argc, char **argv) {
    UNITY_BEGIN();
    RUN_TEST(test_addition);
    RUN_TEST(test_string);
    UNITY_END();
}
ネイティブテスト
[env:native]
platform = native
test_framework = unity

9. デバッグ

設定
[env:debug]
platform = espressif32
board = esp32dev
framework = arduino
debug_tool = esp-prog
debug_init_break = tbreak setup
デバッグコマンド
# Start debugger
pio debug

# Start with GDB
pio debug --interface=gdb

10. カスタムスクリプト

追加スクリプト
[env:esp32dev]
extra_scripts = 
    pre:scripts/pre_build.py
    post:scripts/post_build.py
スクリプトの例
# scripts/pre_build.py
Import("env")

def before_build(source, target, env):
    print("Before build!")

env.AddPreAction("buildprog", before_build)

11. よくある問題

問題解決策
アップロードに失敗するポートを確認し、upload_speed を下げてみてください
ライブラリが見つからないowner/name の完全な形式を使用してください
ビルドエラーpio run -t clean でクリーンアップしてください
ボードが間違っているpio boards でボード ID を確認してください

注: PlatformIO は 1000 以上のボードをサポートしています。pio boards <search> を使ってボードを検索してください。

コメント