树莓派配置
2025-07-04
介绍了树莓派的一些配置
中文输入法
sudo apt-get install fcitx fcitx-googlepinyin触摸屏旋转
sudo cp /usr/share/X11/xorg.conf.d/40-libinput.conf /etc/X11/xorg.conf.d/
编辑 /etc/X11/xorg.conf.d/40-libinput.conf
找到 touchscreen的部分,在里面添加以下语句,然后保存即可
Section "InputClass"
Identifier "libinput touchscreen catchall"
MatchIsTouchscreen "on"
Option "CalibrationMatrix" "0 1 0 -1 0 1 0 0 1" # 90 度旋转
# Option "CalibrationMatrix" "-1 0 1 0 -1 1 0 0 1" # 180度旋转:
# Option "CalibrationMatrix" "0 -1 1 1 0 0 0 0 1" # 270度旋转:
MatchDevicePath "/dev/input/event*"
Driver "libinput"
EndSection移除屏保
修改 /etc/lightdm/lightdm.conf 的 xserver-command 内容为X -s 0 -dpms
[Seat:*]
#type=local
#pam-service=lightdm
#pam-autologin-service=lightdm-autologin
#pam-greeter-service=lightdm-greeter
#xserver-backend=
#xserver-command=X
xserver-command=X -s 0 -dpms
#xmir-command=Xmir日志库log4qt
下载地址:https://github.com/MEONMedical/Log4Qt/tree/master
log4qt 有三个版本
| version | Description |
|---|---|
| master | Minimum Qt version required Qt5.12 (for support of Qt versions down to 5.3 use branch 1.4 or the lates 1.4.x release or for support of Qt versions down to 5.7 use branch 1.5 or the lates 1.5.x release) C++11 feature required (minimum compiler version MSVC14, GCC 4.8 or CLANG 3.3) |
| 1.5 | Minimum Qt version required Qt5.7 (for support of Qt versions down to 5.3 use branch 1.4 or the lates 1.4.x release) C++11 feature required (minimum compiler version MSVC14, GCC 4.8 or CLANG 3.3) |
| 1.4 | Minimum Qt version required Qt5.3 C++11 feature required (minimum compiler version MSVC14, GCC 4.8 or CLANG 3.3) |
不能用编译器linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu,版本过旧 GLIBC 版本过低。
编译后的liblog4qt.so放到宿主机/opt/raspi/sysroot/usr/lib目录下
cp liblog4qt.so* /opt/raspi/sysroot/usr/lib/ -rfd同时复制到树莓派一份
rsync -avz --rsync-path="sudo rsync" liblog4qt.so* pi@192.168.1.134:/usr/lib/XLSX支持
下载地址:https://github.com/QtExcel/QXlsx/
作者介绍:
- QXlsx is excel file(*.xlsx) reader/writer library.
- Because QtXlsxWriter is no longer supported, I created a new project that is based on QtXlsx. (2017-)
- Development language of QXlsx is C++. (with Qt)
- You don't need to use static library or dynamic shared object using QXlsx.
- You can also use it as a static library.
更换启动图
替换/usr/share/plymouth/themes/pix/splash.png 文件
监听CPU温度
watch -n 2 'cat /sys/class/thermal/thermal_zone0/temp | awk "{printf \"CPU温度: %.1f°C\\n\", \$1/1000}"'sudo 免密码
sudo visudo添加以下内容(将username替换为实际用户名):
# 允许无密码执行所有命令(高风险)
username ALL=(ALL) NOPASSWD: ALL
# 或仅允许特定命令
username ALL=(ALL) NOPASSWD: /sbin/ifconfig, /sbin/reboot固定IP
修改/etc/dhcpcd.conf文件
interface eth0
static ip_address=192.168.1.134/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8 8.8.4.4socketcan 报错
- [WARN] 无法加载库socketcan:(socketcan: 无法打开共享对象文件: 没有那个文件或目录)
- [INFO] Cannot load library libsocketcan, some functionality will not be available. 无法加载库socketcan:(socketcan: 无法打开共享对象文件: 没有那个文件或目录)
sudo apt-get install libsocketcan2 libsocketcan-dev添加MCP2518FD CAN卡驱动
sudo raspi-config
选择Interfacing Options -> SPI -> Yes 开启SPI接口请在 /boot/config.txt 中添加:
上述是基于Raspbian OS系统下,若用在Ubuntu系统下,则config.txt路径一般是在: /boot/firmware/config.txt 在最后一行加入参考如下:
dtparam=spi=on
dtoverlay=spi1-3cs
dtoverlay=mcp251xfd,spi0-0,interrupt=25
dtoverlay=mcp251xfd,spi1-0,interrupt=24固定设备号
- 如果使用时发现 CAN 接口的系统设备号和板卡接口名称对不上,可以使用以下方法固定设备号:
在 /etc/udev/rules.d 目录下个创建一个 rules 文件: sudo nano /etc/udev/rules.d/80-can.rules
- 将下面的内容添加到文件中:
ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi0.0/net/can?", NAME="can0"
ACTION=="add", SUBSYSTEM=="net", DEVPATH=="/devices/platform/soc/*/spi1.0/net/can?", NAME="can1"RTC模块支持
硬件GPIO2=RTC_SDA, GPIO3=RTC_SCL 上拉2.2k
添加DS3231
- sudo raspi-config
- 选择 Interfacing Options -> I2C ->yes 启动 i2C 内核驱动
- 编辑 /boot/config.txt 文件,末尾添加以下内容:
dtoverlay=i2c-rtc,ds3231
检查总线上的设备 SD3231 默认地址 0x68
i2cdetect -y 0添加PCF85063A
- 编辑 /boot/config.txt 文件,末尾添加以下内容:
dtoverlay=i2c-rtc,pcf85063a
控制GPIO
ipset GPIO as inputopset GPIO as outputa0-a5set GPIO to alternate function alt0-alt5puset GPIO in-pad pull uppdset GPIO pin-pad pull downpnset GPIO pull none (no pull)dhset GPIO to drive to high (1) level (only valid if set to be an output)dlset GPIO to drive low (0) level (only valid if set to be an output)
Examples:
raspi-gpio get # Prints state of all GPIOs one per line
raspi-gpio get 20 # Prints state of GPIO20
raspi-gpio get 20,21 # Prints state of GPIO20 and GPIO21
raspi-gpio set 20 a5 # Set GPIO20 to ALT5 function (GPCLK0)
raspi-gpio set 20 pu # Enable GPIO20 ~50k in-pad pull up
raspi-gpio set 20 pd # Enable GPIO20 ~50k in-pad pull down
raspi-gpio set 20 op # Set GPIO20 to be an output
raspi-gpio set 20 dl # Set GPIO20 to output low/zero (must already be set as an output)
raspi-gpio set 20 ip pd # Set GPIO20 to input with pull down
raspi-gpio set 35 a0 pu # Set GPIO35 to ALT0 function (SPI_CE1_N) with pull up
raspi-gpio set 20 op pn dh # Set GPIO20 to ouput with no pull and driving high开机自启动(服务器)
创建服务文件 systemd
sudo nano /etc/systemd/system/myapp.service添加以下内容
[Unit]
Description=My Python Application
After=network.target # 确保网络已启动
Requires=network.target # 依赖网络服务
[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/myapp.py
[Install]
WantedBy=multi-user.target启用并管理服务
- 重新加载systemd配置:
sudo systemctl daemon-reload - 开机自启动:
sudo systemctl enable myapp.service - 立即启动:
sudo systemctl start myapp.service - 检查服务状态:
sudo systemctl status myapp.service - 停止服务:
sudo systemctl stop myapp.service - 禁用开机自启动:
sudo systemctl disable myapp.service - 重新启动
sudo systemctl restart myapp.service
开机自启动(图形界面)
- 创建autostart目录(如果不存在):
mkdir -p /home/pi/.config/autostart- 创建.desktop文件:
nano /home/pi/.config/autostart/test.desktop- 添加以下内容:
[Desktop Entry]
Name=test
Comment=Python Program
Exec=python /home/pi/test.py
Icon=/home/pi/python_games/4row_black.png
Type=Application引脚配置
将GPIO13重新配置为RXD5功能 sudo raspi-gpio set 13 a5
验证配置 raspi-gpio get 13
串口开启
修改设备树/boot/config.txt,打开串口 注意引脚不能配置为其他功能,如普通IO输出
enable_uart=1
# 新增这两行:禁用蓝牙,释放ttyAMA0到GPIO引脚
dtoverlay=disable-bt
#dtoverlay=miniuart-bt
#core_freq=250
dtoverlay=uart0
dtoverlay=uart3,txd3_pin=4,rxd3_pin=5
#dtoverlay=uart4
dtoverlay=uart5,txd5_pin=12,rxd5_pin=13更多
查看参考信息
pinout引脚配置
https://pinout.xyz/