RaspberryPi picoのC言語CUI開発環境を構築する＋開発する上で便利なアプリ

#はじめに
RaspberryPi picoがようやく入手できたので、開発環境を構築しました。
巷では、pythonばかりでC言語しかもCUIの構築が乏しく、色々調べていたらYouTubeにUpされていたので自分の環境で試してみました。
ちなみにYouTubeでは、RaspberryPiで環境構築していました。

#使用したPC
CUIのbuild環境のため、ロースペックなPCにしました。
TOSHIBA Dynabook SS M37
CPU：Core2Duo1.66GHz→Core2Duo2.00GHz(CPU換装)
MEM：2GB→4GB(公式では2GBまで、Chipsetで4GBまでの認識)
HDD：80GB(余っていたHDD)

#環境
OS：Ubuntu20.04LTS 64bit desktop(最小インストール)

#使用したアプリ等
samba
openssh-server
barrier
GTKTerm

#開発環境のインストール
前提条件：Ubuntuインストール後、アップデート済み。

```ruby:gitのインストール
sudo apt install -y git
```
```ruby:SDKのインストール
mkdir pico
cd pico
git clone -b master https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init
```
```ruby:サンプルプログラムのDownload(Buildできるか確認するため)
cd ~/pico
git clone -b master https://github.com/raspberrypi/pico-examples.git
```
```ruby:プログラミングツールのインストール
sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential
```

#環境設定
PATHを通す必要があるので、以下を.bashrcの最後に追記します。

```ruby:
export PICO_SDK_PATH=~/pico/pico-sdk
```

保存、終了して

```ruby:
source .bashrc
```

を入力します。

# build確認
サンプルプログラムをbuildしてみます。
picoの動作にはLチカが定番のようです。
私もLチカでbuild確認してみます。

まず、ダウンロードしたサンプルブログラムのディレクトリへ移動します。

```ruby:
cd ~/pico/pico-examples
```

「blink」ディレクトリをコピーします。

```ruby:
cp -r blink ~/pico
```

「blink」ディレクトリへ移動します。

```ruby:
cd ../blink
```

「pico_sdk_import.cmake」をコピーします。

```ruby:
cp -p ~/pico/pico-sdk/external/pico_sdk_import.cmake ./
```

ディスク容量が圧迫されるのが嫌であれば、シンボリックリンクを作成します。

```ruby:
ln -s ../pico-examples/pico_sdk_import.cmake
```

現在のディレクトリで作業すると沢山のファイルが作成されるため、buildディレクトを作成し移動ます。

```ruby:
mkdir build
cd build
```

「CMakeLists.txt」の編集をします

```ruby:編集前
add_executable(blink
        blink.c
        )

# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(blink pico_stdlib)

# create map/bin/hex file etc.
pico_add_extra_outputs(blink)

# add url via pico_set_program_url
example_auto_set_url(blink)
```
```ruby:編集後
cmake_minimum_required(VERSION 3.12)

include(pico_sdk_import.cmake)

project(blink)

pico_sdk_init()

add_executable(blink
        blink.c
        )

# Pull in our pico_stdlib which pulls in commonly used features
target_link_libraries(blink pico_stdlib)

# create map/bin/hex file etc.
pico_add_extra_outputs(blink)

# add url via pico_set_program_url
#example_auto_set_url(blink)
```

cmake_minimum_required(VERSION 3.12)
include(pico_sdk_import.cmake)
project(blink)
pico_sdk_init()
をファイル上部に追加します。
また、
example_auto_set_url(blink)
はコメントし保存します。

それではbuildします

```ruby:
cmake ..
```

問題が無ければ、下記のように出力されます。
ちなみに、cmakeは1回だけ行えば良いです。

```ruby:
Using PICO_SDK_PATH from environment ('/home/hogehoge/pico/pico-sdk')
PICO_SDK_PATH is /home/hogehoge/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
PICO_GCC_TRIPLE defaulted to arm-none-eabi
-- The C compiler identification is GNU 9.2.1
-- The CXX compiler identification is GNU 9.2.1
-- The ASM compiler identification is GNU
-- Found assembler: /usr/bin/arm-none-eabi-gcc
Defaulting PICO target board to pico since not specified.
Using board configuration from /home/hogehoge/pico/pico-sdk/src/boards/include/boards/pico.h
-- Found Python3: /usr/bin/python3.8 (found version "3.8.5") found components: Interpreter
TinyUSB available at /home/hogehoge/pico/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040; adding USB support.
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
ELF2UF2 will need to be built
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hogehoge/pico/blink/build
```

makeします

```ruby:
make
```

問題が無ければ、下記のように出力されます。(サンプルなのでエラーはありませんが)
自分で作ったソースでエラーがあれば、修正する度にmakeを実行します。

```ruby:
Scanning dependencies of target ELF2UF2Build
[  1%] Creating directories for 'ELF2UF2Build'
[  3%] No download step for 'ELF2UF2Build'
[  5%] No patch step for 'ELF2UF2Build'
[  6%] No update step for 'ELF2UF2Build'
[  8%] Performing configure step for 'ELF2UF2Build'
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hogehoge/pico/blink/build/elf2uf2
[ 10%] Performing build step for 'ELF2UF2Build'
Scanning dependencies of target elf2uf2
[ 50%] Building CXX object CMakeFiles/elf2uf2.dir/main.cpp.o
[100%] Linking CXX executable elf2uf2
[100%] Built target elf2uf2
[ 11%] No install step for 'ELF2UF2Build'
[ 13%] Completed 'ELF2UF2Build'
[ 13%] Built target ELF2UF2Build
Scanning dependencies of target bs2_default
[ 15%] Building ASM object pico-sdk/src/rp2_common/boot_stage2/CMakeFiles/bs2_default.dir/compile_time_choice.S.obj
[ 16%] Linking ASM executable bs2_default.elf
[ 16%] Built target bs2_default
Scanning dependencies of target bs2_default_padded_checksummed_asm
[ 18%] Generating bs2_default.bin
[ 20%] Generating bs2_default_padded_checksummed.S
[ 20%] Built target bs2_default_padded_checksummed_asm
Scanning dependencies of target blink
[ 22%] Building C object CMakeFiles/blink.dir/blink.c.obj
[ 23%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_stdlib/stdlib.c.obj
[ 25%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_gpio/gpio.c.obj
[ 27%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_claim/claim.c.obj
[ 28%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_sync/sync.c.obj
[ 30%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_platform/platform.c.obj
[ 32%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_uart/uart.c.obj
[ 33%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_divider/divider.S.obj
[ 35%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_time/time.c.obj
[ 37%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_time/timeout_helper.c.obj
[ 38%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_timer/timer.c.obj
[ 40%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_sync/sem.c.obj
[ 42%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_sync/lock_core.c.obj
[ 44%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_sync/mutex.c.obj
[ 45%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_sync/critical_section.c.obj
[ 47%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_util/datetime.c.obj
[ 49%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_util/pheap.c.obj
[ 50%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/common/pico_util/queue.c.obj
[ 52%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_runtime/runtime.c.obj
[ 54%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_clocks/clocks.c.obj
[ 55%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_watchdog/watchdog.c.obj
[ 57%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_xosc/xosc.c.obj
[ 59%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_pll/pll.c.obj
[ 61%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_vreg/vreg.c.obj
[ 62%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_irq/irq.c.obj
[ 64%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/hardware_irq/irq_handler_chain.S.obj
[ 66%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_printf/printf.c.obj
[ 67%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_bit_ops/bit_ops_aeabi.S.obj
[ 69%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_bootrom/bootrom.c.obj
[ 71%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_divider/divider.S.obj
[ 72%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_double/double_aeabi.S.obj
[ 74%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_double/double_init_rom.c.obj
[ 76%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_double/double_math.c.obj
[ 77%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_double/double_v1_rom_shim.S.obj
[ 79%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_int64_ops/pico_int64_ops_aeabi.S.obj
[ 81%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_float/float_aeabi.S.obj
[ 83%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_float/float_init_rom.c.obj
[ 84%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_float/float_math.c.obj
[ 86%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_float/float_v1_rom_shim.S.obj
[ 88%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_malloc/pico_malloc.c.obj
[ 89%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_mem_ops/mem_ops_aeabi.S.obj
[ 91%] Building ASM object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_standard_link/crt0.S.obj
[ 93%] Building CXX object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_standard_link/new_delete.cpp.obj
[ 94%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_standard_link/binary_info.c.obj
[ 96%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_stdio/stdio.c.obj
[ 98%] Building C object CMakeFiles/blink.dir/home/hogehoge/pico/pico-sdk/src/rp2_common/pico_stdio_uart/stdio_uart.c.obj
[100%] Linking CXX executable blink.elf
[100%] Built target blink
```

make実行後の「build」ディレクトリの内容

```ruby:
CMakeCache.txt  blink.bin  blink.elf.map  cmake_install.cmake  pico-sdk
CMakeFiles      blink.dis  blink.hex      elf2uf2
Makefile        blink.elf  blink.uf2      generated
```

blink.uf2をpicoにdrag-and-dropすると、picoのLEDがチカチカします。

#使用するアプリ(各人の好み)
##samba
Ubuntu上のファイルをWindowsから編集するとき便利
##openssh-server
Windowsからターミナルでbuildするときに便利
##barrier
Ubuntuでbuildしたpicoの実行ファイルをWindowsのキーボード＋マウスでpicoに転送するときに便利
##GTKTerm
printf()でデバッグするときに便利


#まとめ
CUI環境での開発であれば、ロースペックなPCでも開発は可能

#アプリのインストール等
##samba
```ruby:
sudo apt install samba 
```

##openssh-server
```ruby:
sudo apt install openssh-server
```

##barrier
```ruby:
sudo add-apt-repository ppa:alexlarsson/flatpak
sudo apt install flatpak gnome-software-plugin-flatpak
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
```
上記インストール後rebootして、「barrier」をubuntuソフトウェアから検索時実行します。
Windowsにもインストールする必要があります。

##GTKTerm
```ruby:
sudo apt install gtkterm
sudo gtkterm
```