Kamuycikap - SentenceDataBase

日々の勉強の記録を気分で書き綴るブログ

MacBook Air M2でPico Probe開発環境構築

MacBook Air M2でPicoProbeをセットアップ

この記事でわかること

Raspberry Picoの公式ドキュメントを参考に、Bashプロンプトでアレコレ操作してコンパイルデバッグまでを行える環境を構築します。
ArudinoIDEやらVSCodeやらの記事は、他の方にお任せして、ここでは公式ドキュメントを参考に、コマンドラインで一通りの作業を行う方法を記事にしています。

コンパイル環境構築

事前に、コンパイル環境構築を完了させてください。
  ↓↓
MacBook Air M2でPi Picoのビルド環境構築

PicoProbeの作成

下記のURLのリンク【Debugging using another Raspberry Pi Pico】のDownload the UF2 fileからUF2ファイルをダウンロードします。
ファイル名:picoprobe.uf2
  ↓↓
Debugging using another raspberrypi pico

通常のプログラム書き込みと同様に、Pi Picoにpicoprobe.uf2を書き込みます。
  ↓↓
PipicoのBOOTボタン押しながら、MacbookにUSB接続。 → 外付けドライブとして「RPI-RP2」がFinderに認識される
picoprobe.uf2をRPI-RP2へコピー。

OpenOCDのビルド

必要なツールをインストール
$ brew install libtool automake libusb wget pkg-config gcc texinfo
ビルド
$ cd ~/pico
$ git clone https://github.com/raspberrypi/openocd.git --branch rp2040-v0.12.0 --depth=1 $ cd openocd
$ cd ./openocd
$ export PATH="$(brew --prefix)/opt/texinfo/bin:$PATH"
$ ./bootstrap
$ ./configure --enable-picoprobe --disable-werror
$ make -j4
$ sudo make install
OpenOCD実行

結果エラーになるけれど、実行できていればきにしない

$ src/openocd
Open On-Chip Debugger 0.10.0+dev-gc231502-dirty (2020-10-15-07:48)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
embedded:startup.tcl:56: Error: Can't find openocd.cfg
in procedure 'script'
at file "embedded:startup.tcl", line 56
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Error: Debug Adapter has to be specified, see "interface" command
embedded:startup.tcl:56: Error:
in procedure 'script'
at file "embedded:startup.tcl", line 56

PicoProbeテスト

PicoProbeと開発ターゲット用のPiPicoを結線
結線図
Pico A GND -> Pico B GND
Pico A GP2 -> Pico B SWCLK
Pico A GP3 -> Pico B SWDIO
Pico A GP4/UART1 TX -> Pico B GP1/UART0 RX
Pico A GP5/UART1 RX -> Pico B GP0/UART0 TX
Pico A VSYS -> Pico B VSYS
OpenOCD起動
$ openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg -s tcl

※~/opt/shell/openocd_pico.sh としてスクリプト化し活用しています。

Open On-Chip Debugger 0.12.0-g4d87f6d (2024-03-02-15:47)                                                       
Licensed under GNU GPL v2                                                                                      
For bug reports, read                                                                                          
        http://openocd.org/doc/doxygen/bugs.html                                                               
adapter speed: 5000 kHz                                                                                        
                                                                                                               
Info : Hardware thread awareness created                                                                       
Info : Hardware thread awareness created
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=E66044304317392D
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: Atomic commands supported
Info : CMSIS-DAP: Test domain timer supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz 
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477, DLPIDR 0x10000001
Info : [rp2040.core0] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core0] target has 4 breakpoints, 2 watchpoints
Info : [rp2040.core1] Cortex-M0+ r0p1 processor detected
Info : [rp2040.core1] target has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections

実行ファイルの読込/実行

テストとして、サンプルのblinkを動かしてみます。

GDB起動

前項で動かしたOpenOCDのプロンプトはそのままに、新しくプロンプトを新規に開いて、以降のコマンドを実施します。

$ cd  ~/pico/pico-examples/build/blink
$ arm-none-eabi-gdb -q -ex "target extended-remote :3333" ./blink.elf
Reading symbols from ./blink.elf...
(No debugging symbols found in ./blink.elf)
Remote debugging using :3333
warning: multi-threaded target stopped without sending a thread-id, using first non-exited thread
0x10000ce2 in sleep_until ()
(gdb) 
プログラムのロード
(gdb) load
Loading section .boot2, size 0x100 lma 0x10000000
Loading section .text, size 0x1d88 lma 0x10000100
Loading section .rodata, size 0xf4 lma 0x10001e88
Loading section .binary_info, size 0x20 lma 0x10001f7c
Loading section .data, size 0x18c lma 0x10001f9c
Start address 0x100001e8, load size 8488
Transfer rate: 10 KB/sec, 1697 bytes/write.
リセット
(gdb) monitor reset

※ 以前は、monitor reset init だったが、なぜかinitを入れるとタイマー関連の関数が動かない症状が出る。

プログラムの実行
(gdb) continue

プログラムの停止

Ctrl + c 操作で強制停止
(gdb) continue
Continuing.
^C
Thread 1 "rp2040.core0" received signal SIGINT, Interrupt.
0x10000ce2 in sleep_until ()
(gdb)  
デタッチ
(gdb) detach
Detaching from program: /Users/k/pico/pico-examples/build/blink/blink.elf, Remote target
[Inferior 1 (Remote target) detached]
(gdb)
終了
(gdb) quit

MacBook Air M2でRaspberry Pi Picoビルド環境構築

MacBook Air M2でPi Picoのビルド環境をセットアップ

ツールチェインインストール

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

SDKとサンプル取得

sdkとサンプルを入れるディレクトリ「pico」を作成

$ cd
$ mkdir ./pico
SDK配置
$ cd ./pico
$ git clone https://github.com/raspberrypi/pico-sdk.git --branch master
$ cd pico-sdk
$ git submodule update --init
サンプル配置
$ cd ..
$ git clone https://github.com/raspberrypi/pico-examples.git --branch master
sdkをアップデート
$ cd ./pico-sdk
$ git pull
$ git submodule update

検証

環境変数を設定

SDKへのパスを設定
$ export PICO_SDK_PATH=~/pico/pico-sdk

全サンプルのコンパイル実施

コンパイル結果にデバッグ情報を追加

後日、PicoProbeを利用したデバッガ開発の為に必要だと気付いた。
CMakeLists.txtに set(CMAKE_BUILD_TYPE Debug) を追記する。

$ cd ~/pico/pico-examples
$ nano ./CMakeLists.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)
set(CMAKE_BUILD_TYPE Debug)  # <- Insert Debug Setting

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()

<<以下省略>>
サンプルの中にビルドディレクトリを作成
$ cd ~/pico/pico-examples
$ mkdir build
$ cd build
全サンプルのコンパイル
$ cmake ..
$ make -j4

特定のサンプルだけコンパイル

※ ここのセクションうまく動いていない。現在調査中。。。

blinkだけ
$ cd ~/pico/pico-examples/build/blink
$ make -j4

Finderで開いているフォルダパスをクリップボードにコピー MacOS(Sonoma)

アプリケーションのデフォルトカレントフォルダ

Finderで、Cmd + O 操作でアプリケーションを起動したとき、ファイル保存時に「うーーん」と残念な気持ちになる事があります。
タイトル通り、カレントフォルダが「前回操作したフォルダ」になっている事です。
設計思想として、それも正しいのではありますが、、、、。
僕の場合は、開いたファイルと同じフォルダパスをカレントとして利用したい。

無ければ作る

MacOSの仕様として、各アプリケーションのデフォルトカレントフォルダは、前回操作したフォルダになっている様子。
なので、保存するときに可能な限り少ない手順でカレントフォルダを変更したい。
Macは「無ければ作る」の精神です。
Automatorで作成します。

WorkFlow作成

Automatorを起動し下記の手順でワークフローを作成

新規作成

クイックサクションとして新規作成します

ツール選択

左側にあるツール一覧から、「AppleScriptを実行」を右側のペインにドラッグ&ドロップする。

コードを編集

表示されるコード欄に、下記のコードを入力

on run {input, parameters}
	
	(* Your script goes here *)
	tell application "Finder"
		set currentPath to POSIX path of (target of front window as alias)
		set the clipboard to currentPath
	end tell
	return input
end run
名前付けて保存

ワークフローを、名前を付けて保存します。
保存フォルダは、Serviceフォルダの中に入れます。

キーボードショートカットに割り付け

作成したワークフローを、キーボードショートカットに割り付けます。

 → システム設定 → キーボード → キーボードショートカット → サービス → 一般

上記手順に沿ってクリックすると、作成されたワークフローがあるはずです。
チェックを入れて、キーボードショートカットを登録します。

実践

アプリ起動

PagesやNumbersを開く。

名前を付けて保存

適当に文字入力など行い、名前を付けて保存を実施。
※このタイミングで、ファイル名設定ダイアログが表示される

Finderでショートカット実施

Finderをアクティブにし、目的のフォルダを開いた状態で、前項で作成したショートカット操作を行う。

名前を付けて保存ダイアログをアクティブに

起動したアプリケーションの名前を付けて保存ダイアログをアクティブにし、下記の操作を実施。

Cmd + Shift + G

すると、フォルダ入力ダイアログが表示されるので、Cmd + V で、クリップボードにコピーしておいたパスを貼り付けてEnterキーを叩く。
結果、ファイルを保存したいフォルダをマウス操作で辿ること無くカレントに設定する事ができます。

MacOS(sonoma)で値貼り付けしたい

MacOS(sonoma)で値だけ貼り付けしたい

MacOSでは、アプリケーション共通の操作として、Cmdキーとの組み合わせによるカット&ペーストの機能が提供されている。
んが、ブラウザ等の文字列をエディタ等に貼り付けると、元の書式も引き継いで貼り付けられる。

これはこれで便利なんだけれど、用途として値だけの貼り付けも行いたい。

追記 2023/11/27

下記の設定で実現できる「値貼り付け」は、それに対応したアプリケーションのみ作用する。
Pagesではできるけれど、スティッキーズではできない。
どうやら、アプリ側での対応に委ねられている様子。

結論

下記の本家情報を参考にして、「値だけ貼り付け」のショートカットを設定する事で解決。
Macでアプリのキーボードショートカットを作成する - Apple サポート (日本)

→ システム設定 → キーボード → キーボードショートカット → アプリのショートカット → 「+」

  • アプリケーション

全てのアプリケーション

  • メニュータイトル

ペーストしてスタイルを合わせる
※上記の文言「ペーストしてスタイルを合わせる」は一字一句間違いなく正しく入力しないとダメ!

  • キーボードショートカット

Shift + Cmd + V

上記の設定を行うと、Cmd + V で、通常の値付き貼り付けはそのままとなるので、値貼り付けと値なし貼り付けをショートカットで使い分けられる。

本家の情報

本家では、Opt + Cmd + Shift + V となっているが、自分のMacbookでは反応しなかった。
https://support.apple.com/ja-jp/102553

Pi4にWiringPiの最新版をインストールする

WiringPiをインストールしようとしたら怒られた

下記のコマンドでWiringPiのインストールが出来なくなっていた

$ sudo apt-get install wiringpi

インストール方法

$ wget https://project-downloads.drogon.net/wiringpi-latest.deb
$ sudo dpkg -i wiringpi-latest.deb
pi@pi4-cui:~ $ gpio -v
gpio version: 2.52
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Pi 4B, Revision: 02, Memory: 4096MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi 4 Model B Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.

Macbook Air M2(sonoma) Finderでキーボードで右クリックメニュー出したい

Finderのキーボード操作で右クリックメニュー

Windowsだとやたら充実している右クリックメニュー。
当然MacOSにもあるのだけれど、マウス操作では無くてキーボード操作で行いたい。

Automatorでクイックアクション作成

下記の手順でAppleScriptを作成する
Automator → 新規作成 → クイックアクション → AppleScriptを実行

下記のスクリプトをコピペして、名前を付けて保存。
例:right_click

on run {input, parameters}
	
	-- 変数fromtAppに、一番手前でアクティブになっているアプリケーションを取得
	tell application "System Events" to set frontApp to name of first process whose frontmost is true

	-- 取得したアプリケーションに対してイベントを渡す
	tell application "System Events"
		tell application process frontApp
		
			-- fromtApp上でフォーカスが当てられている何かを、変数f_selectionに取得
			set _selection to value of attribute "AXFocusedUIElement"
			
			-- その何かに対し、右クリックメニュー表示を指示
			tell _selection to perform action "AXShowMenu"
		end tell
	end tell
	
	return input
end run

ショートカットとして登録

 → システム設定 → キーボード → キーボードショートカット → サービス → 一般 → 【保存したActionScript名】
とたどると、保存したスクリプトが出てくるはず。
任意のキーボード設定をショートカットとして設定できる。

FinderでActionScriptを有効にする

 → システム設定 → プライバシーとセキュリティ → アクセシビリティ → FinderをON
※右クリックショートカットを有効化したいアプリに対し、個別に設定する必要あり。
※ファイルとかフォルダとか、フォーカスが確実に当たっているオブジェクトじゃ無いとエラーになって止まる。

Ubuntu22.04でttyUSB0が認識されない

シリアルUSBを接続しても認識されない

久しぶりにUbuntuを使い始めて、ハマったのでメモ。

結論を先に書きます。

$ sudo apt autoremove brltty
$ sudo usermod -a -G dialout <username>
$ sudo shutdown -r now

★2023/03/14 追記
どうも、Ubuntu22.04 LTSの既知の問題らしい。
Ubuntu22でM5Stackなどのデバイスと接続ができない場合 - えいあーるれいの技術日記

解決までの道のり

dmesgで確認

$ sudo dmesg
で確認すると、どうも失敗しているような?

[89167.276267] usb 2-2: ch341-uart converter now attached to ttyUSB0
[89167.816353] input: BRLTTY 6.4 Linux Screen Driver Keyboard as /devices/virtual/input/input42
[89167.908026] usb 2-2: usbfs: interface 0 claimed by ch341 while 'brltty' sets config #1
[89167.908482] ch341-uart ttyUSB0: ch341-uart converter now disconnected from ttyUSB0
[89167.908509] ch341 2-2:1.0: device disconnected

brlttyというのが邪魔をしている?

不要なので削除

brlttyを調べてみたところ、

BRLTTY は点字ディスプレイを使用する視覚障害者にコンソール (テキストモード) へのアクセスを提供するデーモンです。 点字ディスプレイを駆動して完全な画面表示機能を提供します。

との事で、自分には必要なさそう。
なので、アンインストール。

$ sudo apt autoremove brltty

さらにttyUSB0のパーミッションを設定

brlttyを削除してからdmesgを確認すると

[90342.954875] usb 2-2: ch341-uart converter now attached to ttyUSB0

となっていた。

それでも接続できないので対応

無事に認識されたので、さっそく試してみたら、接続できないと怒られる。
どうもパーミッション関連の問題の様子。
$ chmod a+rw /dev/ttyUSB0
としてパーミッションを都度変更しても書き込めるが、毎回コマンド叩くのは面倒なので、dialoutグループに追加してあげる。

$ sudo usermod -a -G dialout <username>

再起動すればOK。