Raspberry Pi PICOをUSB-MIDIデバイスとして使う


amazon.co.jp

PICOの開発環境が既に設定されているようであれば、midi_testのコンパイル、へジャンプして下さい。

Raspberry Pi PICOのSDKとexampleを事前にダウンロードしているものとします。(Getting started with Raspberry Pi Pico p.7 2.1. Get the SDK and examples 参照) また、C/C++の開発環境として、以下の5つのプログラムをインストールする必要があります。(p.42 9.2.1. Installing the Toolchain 参照)

Raspberry Pi PICOをUSB-MIDIデバイスとして使うには、pico-sdk/lib/tinyUSBというドライバーを使います。使い方はexampleを参考にします。MIDIに関するexsampleは、pico-sdk/lib/tinyusb/examples/device/midi_testに入っています。

pico-exampleのコンパイル

まず、pico-exampleをコンパイルしてみます。pico-sdk, pico-exampleが以下のようにインストールされているとします。

C:\pico
 |- pico-sdk
 |- pico-example

コンパイルを行うにはcmakenmakeを使います。nmakeはVisual Studioに含まれているコマンドラインツールです。

Windowsでは普通のコマンドプロンプトではうまくいきません。Visual Studio2022のDeveloper Command Prompt for vs2022 を立ち上げます。



ディレクトリをpico-exampleを保存した場所へ移動します。

C:\Program Files\Microsoft Visual Studio\2022\Community > cd C:\pico\pico-example
C:\pico\pico-example>

pico-example のフォルダーに buildフォルダーを作成し、移動します。

C:\pico\pico-example> mkdir build
C:\pico\pico-example> cd build

cmakeを実行します。

C:\pico\pico-example\build> cmake -G "NMake Makefiles" ..

Using PICO_SDK_PATH from environment ('c:\pico\pico-sdk')
PICO_SDK_PATH is C:/Users/masah/Documents/pico/pico-sdk
Defaulting PICO_PLATFORM to rp2040 since not specified.
Defaulting PICO platform compiler to pico_arm_gcc since not specified.
-- Defaulting build type to 'Release' since not specified.
PICO compiler is pico_arm_gcc
-- The C compiler identification is GNU 10.3.1
-- The CXX compiler identification is GNU 10.3.1
-- The ASM compiler identification is GNU
-- Found assembler: C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.10/bin/arm-none-eabi-gcc.exe
Build type is Release
Defaulting PICO target board to pico since not specified.
Using board configuration from C:/pico/pico-sdk/src/boards/include/boards/pico.h
-- Found Python3: C:/Users/user/AppData/Local/Programs/Python/Python38/python.exe (found version "3.8.10") found components: Interpreter
TinyUSB available at C:/pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; enabling build support for USB.
cyw43-driver available at C:/pico/pico-sdk/lib/cyw43-driver
lwIP available at C:/pico/pico-sdk/lib/lwip
-- Configuring done (12.8s)
-- Generating done (34.9s)
-- Build files have been written to: C:/pico/pico-examples/build

nmakeでコンパイルが始まります。

C:\pico\pico-examople\build> nmake

Microsoft(R) Program Maintenance Utility Version 14.32.31332.0
Copyright (C) Microsoft Corporation.  All rights reserved.

[  0%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
[  0%] Linking ASM executable bs2_default.elf
[  0%] Built target bs2_default
[  0%] Creating directories for 'PioasmBuild'
[  0%] No download step for 'PioasmBuild'
[  0%] No update step for 'PioasmBuild'
[  0%] No patch step for 'PioasmBuild'
[  0%] Performing configure step for 'PioasmBuild'
loading initial cache file C:/pico/pico-examples/build/pico-sdk/src/rp2_common/cyw43_driver/pioasm/tmp/PioasmBuild-cache-Release.cmake
-- The CXX compiler identification is MSVC 19.32.31332.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (321.8s)
-- Generating done (0.1s)
-- Build files have been written to: C:/pico/pico-examples/build/pioasm
[  0%] Performing build step for 'PioasmBuild'

Microsoft(R) Program Maintenance Utility Version 14.32.31332.0
Copyright (C) Microsoft Corporation.  All rights reserved.

[ 10%] Building CXX object CMakeFiles/pioasm.dir/main.cpp.obj
...
...
(略)
...
...
[100%] Building C object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_float/float_math.c.obj
[100%] Building ASM object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_float/float_v1_rom_shim.S.obj
[100%] Building C object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_malloc/pico_malloc.c.obj
[100%] Building ASM object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_mem_ops/mem_ops_aeabi.S.obj
[100%] Building ASM object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_standard_link/crt0.S.obj
[100%] Building CXX object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_standard_link/new_delete.cpp.obj
[100%] Building C object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_standard_link/binary_info.c.obj
[100%] Building C object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj
[100%] Building C object watchdog/hello_watchdog/CMakeFiles/hello_watchdog.dir/C_/Users/masah/Documents/pico/pico-sdk/src/rp2_common/pico_stdio_uart/stdio_uart.c.obj
[100%] Linking CXX executable hello_watchdog.elf
[100%] Built target hello_watchdog

*
midi_testのコンパイル

PICOでMIDIを扱うサンプルプログラムは pico-sdk\lib\tinyusb\examples\device\midi_testにあります。このmidi_testフォルダーをコピーします。ここではC:\picoにコピーしています。

C:\pico>cp -r pico-sdk\lib\tinyusb\examples\device\midi_test .

midi_testのフォルダにある、CMakeLists.txtを以下に置き換えます。

cmake_minimum_required(VERSION 3.24)
set(FAMILY rp2040) 

#############################################
# Pull in Pico SDK (must be before project)

# set PICO_SDK_PATH from emviroment
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("PICO_SDK_PATH=" ${PICO_SDK_PATH})

include(pico_sdk_import.cmake)

#############################################

# inject tinyusb dependency

set (PICO_TINYUSB_PATH ${PICO_SDK_PATH}/lib/tinyusb)
message("PICO_TINYUSB_PATH=" ${PICO_TINYUSB_PATH})

include (${PICO_TINYUSB_PATH}/hw/bsp/family_support.cmake)
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})

#############################################

# project setting

project(${PROJECT})
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})

add_executable(${PROJECT})

# enable usb output, disable uart output
pico_enable_stdio_usb(${PROJECT} 1)
pico_enable_stdio_uart(${PROJECT} 0)

# Source
target_sources(${PROJECT} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
        )

# Include
target_include_directories(${PROJECT} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/src
		${CMAKE_CURRENT_SOURCE_DIR}/include
        )

# target_link_libraries(${PROJ} pico_stdio)
# target_link_libraries(${PROJ} pico_stdlib)

# Configure compilation flags and libraries for the example... see the corresponding function
# in hw/bsp/FAMILY/family.cmake for details.
family_configure_device_example(${PROJECT})

midi_testフォルダーにディレクトリを移動し、buildフォルダを作成、buildフォルダへ移動します。

C:\pico\midi_test> mkdir build
C:\pico\midi_test> cd build
C:\pico\midi_test> build>

cmake, nmake でコンパイルします。

C:\pico\midi_test\build> cmake -G "NMake Makefiles" ..
C:\pico\midi_test\build> nmake

コンパイルが終わるとbuildフォルダの下にmidi_test.uf2ファイルができています。このファイルをPICO(RPI-RP2)へコピーします。PICOにリセットがかかり、PICOがMIDIデバイスとして認識できるようになります。

Send And Receiveで見てみるとTinyDeviceとして認識され、Note On/Offが繰り返し出力していることを見て取れます。