Skip to content

Commit c0907bc

Browse files
KuraZuzuYusukeKato
andauthored
スクリプトによるRaspberry Pi設定の自動化 (#88)
Co-authored-by: YusukeKato <[email protected]>
1 parent d7c2538 commit c0907bc

File tree

2 files changed

+143
-33
lines changed

2 files changed

+143
-33
lines changed

README.md

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,51 @@
11
# RaspberryPiMouse
22

3-
![CI](https://github.com/rt-net/RaspberryPiMouse/workflows/CI/badge.svg?branch=master)
3+
[![CI](https://github.com/rt-net/RaspberryPiMouse/actions/workflows/driver-cross-build.yml/badge.svg)](https://github.com/rt-net/RaspberryPiMouse/actions/workflows/driver-cross-build.yml)
44

55
This repository has the source code and kernel objects
66
for the Raspberry Pi Mouse.
77

88
## Installation
99

10-
Run the installation script ([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash)).
10+
Run the following scripts.
1111

12-
インストール用のシェルスクリプト([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash))を実行します。
12+
- setting script ([`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash))
13+
- installation script ([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash))
1314

14-
### for Raspberry Pi OS
15-
16-
`/boot/firmware/config.txt`を編集し、ファイル末尾に以下の設定を追加します。
17-
18-
```sh
19-
arm_64bit=0 # "64-bit"版では不要です
20-
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000 # カーネル5.16未満の場合は不要です
21-
dtparam=i2c_baudrate=62500
22-
```
15+
以下のスクリプトを実行します
16+
- setting script([`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash)
17+
- installation script([`./utils/build_install.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/build_install.bash)
2318

24-
Raspberry Piを再起動します。
19+
### for `Raspberry Pi OS``Ubuntu`
2520

26-
以下のコマンドでインストールを実行します
21+
以下のコマンドで本リポジトリをダウンロードし、Raspberry Pi本体の設定を行います
2722

28-
```sh
23+
```bash
2924
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
3025
$ cd RaspberryPiMouse/utils
31-
$ sudo apt install raspberrypi-kernel-headers build-essential
32-
$ ./build_install.bash
26+
$ ./set_configs.bash
3327
```
3428

35-
### for Ubuntu
29+
Raspberry Piを再起動し、以下のコマンドを実行してビルドに必要なファイルをインストールします。**`Ubuntu``Raspberry Pi OS`でコマンドが違います。**
3630

37-
`/boot/firmware/config.txt`を編集し、ファイル末尾に以下の設定を追加します。
31+
```bash
32+
# Ubuntu Serverの場合
33+
$ sudo apt install linux-headers-$(uname -r) build-essential
3834

39-
```sh
40-
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000 # "Ubuntu Server 22.04"では不要です
41-
dtparam=i2c_baudrate=62500
35+
# Raspberry Pi OSの場合
36+
$ sudo apt install raspberrypi-kernel-headers build-essential
4237
```
4338

44-
Raspberry Piを再起動します
39+
以下のコマンドでビルドを実行します
4540

46-
以下のコマンドでインストールを実行します。
47-
48-
```sh
49-
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
41+
```bash
5042
$ cd RaspberryPiMouse/utils
51-
$ sudo apt install linux-headers-$(uname -r) build-essential
5243
$ ./build_install.bash
5344
```
5445

5546
### Manual installation
5647

57-
```sh
48+
```bash
5849
$ git clone https://github.com/rt-net/RaspberryPiMouse.git
5950
$ cd RaspberryPiMouse/src/drivers
6051
$ make
@@ -63,23 +54,31 @@ $ sudo insmod rtmouse.ko
6354

6455
## Notes for the installation (ドライバの導入の際の注意)
6556

57+
特定の機能の有効化や、プログラムのビルドのために設定が必要な項目があります。以下の設定をご確認ください。
58+
59+
[`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash)を実行すると、[自動で適切な設定に書き換わります]((https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash))
60+
61+
6662
### for Raspberry Pi OS
6763

64+
Raspberry Pi OSでは、SPIとI2Cがデフォルトで無効となっているため、有効化する必要があります。
65+
66+
#### for SPI and I2C
67+
6868
Enable SPI and I2C functions via `raspi-config` command.
6969

70-
以下の設定を確認ください。
7170
`raspi-config` コマンドで設定します。
7271

7372
* SPI機能を「入」にする。
7473
* I2C機能を「入」にする。
7574

76-
7775
### for Raspberry Pi 4
7876

79-
Edit [`rtmouse.c`](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54) to change the defined value `RASPBERRYPI` from '2' to '4'.
77+
Edit [`rtmouse.c`](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54) to change the defined value `RASPBERRYPI` from`2`to`4`.
8078

8179
Raspberry Pi 4ではCPUのレジスタがそれまでのRaspberry Piとは異なります([issues#21](https://github.com/rt-net/RaspberryPiMouse/issues/21))。
8280
Raspberry Pi 4で本ドライバを使用する際には`rtmouse.c`の以下の行(2020年4月13日現在の最新版のv2.1.0では[54行目](https://github.com/rt-net/RaspberryPiMouse/blob/dd0343449951a99b067e24aef3c03ae5ed9ab936/src/drivers/rtmouse.c#L54))を`RASPBERRYPI 4`に書き換えてビルドする必要があります。
81+
8382
[`./utils/build_install.bash`](./utils/build_install.bash)を実行すると、Raspberry Piのモデルに合わせて[`rtmouse.c`](./src/drivers/rtmouse.c)[自動で書き換わります](https://github.com/rt-net/RaspberryPiMouse/blob/a9af4fa2b2a8e34c0f93a6ce5cf88ebd50ff39c2/utils/build_install.raspi4ubuntu.bash#L13-L14)
8483

8584
```c
@@ -91,6 +90,24 @@ Raspberry Pi 4で本ドライバを使用する際には`rtmouse.c`の以下の
9190
#define RASPBERRYPI 2
9291
```
9392
93+
### for 32-bit OS
94+
95+
Set 32bit-setting to `/boot/firmware/config.txt`.
96+
97+
32-bit版のOSではビルドするために、`/boot/firmware/config.txt`に以下の1行を追加する必要があります。
98+
99+
```bash
100+
arm_64bit=0
101+
```
102+
103+
### デバイスツリーオーバーレイについて
104+
105+
kernel `5.16`以降では`/boot/firmware/config.txt`に以下の設定を記述し、dtoverlayを設定する必要があります。※[`./utils/set_configs.bash`](https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash)を実行すると、設定は[自動で書き換わります]((https://github.com/rt-net/RaspberryPiMouse/blob/master/utils/set_configs.bash#L35-#L49))。
106+
107+
```bash
108+
dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000
109+
```
110+
94111
### パルスカウンタについて
95112

96113
パルスカウンタは値の読み取りにI2Cを使用しています。仕様上は400kHzまでbaudrateを上げることができます(※1)。
@@ -102,9 +119,11 @@ it may be necessary to set the I2C baudrate lower than the default value.
102119
Add a following new line in `/boot/firmware/config.txt` to change the i2c_baudrate to 62.5 kHz.
103120

104121
`/boot/firmware/config.txt`に以下の1行を追加することでI2Cのbaudrateを62.5kHzに固定することができます。
105-
```
122+
123+
```bash
106124
dtparam=i2c_baudrate=62500
107125
```
126+
108127
※1 Raspberry Pi 4 Model B(Ubuntu Server `18.04` / `20.04` / `22.04` / `24.04`)を搭載して400kHzで通信できることを確認しています。
109128
※2 現在設定されているI2Cのbaudrateは以下のコマンドを実行することで確認できます。
110129
```

utils/set_configs.bash

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
# settings comment
5+
SETTING_COMMENT='# Raspberry Pi Mouse V3 settings'
6+
7+
# OS architecture (32-bit or 64-bit)
8+
ARCHITECTURE=$(getconf LONG_BIT)
9+
10+
# dtoverlay setting
11+
DTOVERLAY='dtoverlay=anyspi:spi0-0,dev="microchip,mcp3204",speed=1000000'
12+
13+
# i2c_baudrate-setting
14+
DTPARAM='dtparam=i2c_baudrate=62500'
15+
16+
# config-file PATH
17+
CONFIG_FILE='/boot/firmware/config.txt'
18+
19+
# compare kernel versions as integers for accurate version comparison (excluding minor versions).
20+
GET_KERNEL_VERSION_INT() {
21+
# 0-padding is used to avoid minor versions being compared by their first digit.
22+
echo "$1" | awk -F. '{ printf "%d%02d", $1, $2 }'
23+
}
24+
25+
# kernel version
26+
KERNEL_VERSION=$(uname -r | cut -d'-' -f1)
27+
KERNEL_VERSION_INT=$(GET_KERNEL_VERSION_INT "$KERNEL_VERSION")
28+
29+
# add "Raspberry Pi Mouse v3" settings
30+
if ! grep -qxF "$SETTING_COMMENT" "$CONFIG_FILE"; then
31+
echo "$SETTING_COMMENT" | sudo tee -a "$CONFIG_FILE" > /dev/null
32+
fi
33+
34+
# check if the OS is running in 32-bit mode
35+
if [[ "$ARCHITECTURE" == "32" ]]; then
36+
if ! grep -qxF "arm_64bit=0" "$CONFIG_FILE"; then
37+
echo "arm_64bit=0" | sudo tee -a "$CONFIG_FILE"
38+
echo "Add \"arm_64bit=0\" > $CONFIG_FILE"
39+
fi
40+
elif [[ "$ARCHITECTURE" == "64" ]]; then
41+
# remove arm_64bit=0 if present in a 64-bit environment
42+
if grep -qxF "arm_64bit=0" "$CONFIG_FILE"; then
43+
sudo sed -i '/arm_64bit=0/d' "$CONFIG_FILE"
44+
echo "Removed \"arm_64bit=0\" from $CONFIG_FILE"
45+
fi
46+
fi
47+
48+
# add dtparam-setting for "/boot/firmware/config.txt"
49+
if ! grep -qxF "$DTPARAM" "$CONFIG_FILE"; then
50+
echo "$DTPARAM" | sudo tee -a "$CONFIG_FILE" > /dev/null
51+
echo "Add \"$DTPARAM\" > $CONFIG_FILE"
52+
fi
53+
54+
# use device-tree-overlay when the kernel is 5.16 or higher
55+
if (( KERNEL_VERSION_INT >= $(GET_KERNEL_VERSION_INT 5.16) )); then
56+
# add dtoverlay-setting for "/boot/firmware/config.txt"
57+
if ! grep -qxF "$DTOVERLAY" "$CONFIG_FILE"; then
58+
echo "$DTOVERLAY" | sudo tee -a "$CONFIG_FILE" > /dev/null
59+
echo "Add \"$DTOVERLAY\" > $CONFIG_FILE"
60+
fi
61+
else
62+
# remove dtoverlay-setting if kernel is less than 5.16
63+
if grep -qxF "$DTOVERLAY" "$CONFIG_FILE"; then
64+
sudo sed -i "/$DTOVERLAY/d" "$CONFIG_FILE"
65+
echo "Removed \"$DTOVERLAY\" from $CONFIG_FILE"
66+
fi
67+
fi
68+
69+
# replace "dtparam=i2c_arm=off" with "dtparam=i2c_arm=on"
70+
if grep -qxF 'dtparam=i2c_arm=off' "$CONFIG_FILE"; then
71+
sudo sed -i 's/dtparam=i2c_arm=off/dtparam=i2c_arm=on/' "$CONFIG_FILE"
72+
echo "Changed \"dtparam=i2c_arm=off\" to \"dtparam=i2c_arm=on\" in $CONFIG_FILE"
73+
fi
74+
75+
# replace "dtparam=spi=off" with "dtparam=spi=on"
76+
if grep -qxF 'dtparam=spi=off' "$CONFIG_FILE"; then
77+
sudo sed -i 's/dtparam=spi=off/dtparam=spi=on/' "$CONFIG_FILE"
78+
echo "Changed \"dtparam=spi=off\" to \"dtparam=spi=on\" in $CONFIG_FILE"
79+
fi
80+
81+
# uncomment "dtparam=i2c_arm=on" if it is commented
82+
if grep -qxF '#dtparam=i2c_arm=on' "$CONFIG_FILE"; then
83+
sudo sed -i 's/#dtparam=i2c_arm=on/dtparam=i2c_arm=on/' "$CONFIG_FILE"
84+
echo "Uncommented \"dtparam=i2c_arm=on\" in $CONFIG_FILE"
85+
fi
86+
87+
# uncomment "dtparam=spi=on" if it is commented
88+
if grep -qxF '#dtparam=spi=on' "$CONFIG_FILE"; then
89+
sudo sed -i 's/#dtparam=spi=on/dtparam=spi=on/' "$CONFIG_FILE"
90+
echo "Uncommented \"dtparam=spi=on\" in $CONFIG_FILE"
91+
fi

0 commit comments

Comments
 (0)