下午好,亲爱的朋友们,
目标:将 i2c 时钟频率更改为 400kHz
动机:-> 尝试通过 i2c 使用 Adafruit BNO085,并且已知 rpi i2c 时钟延长存在问题。(这是一个已知的错误,自 2012 年以来一直存在。是的!)
我尝试过什么:我花了整整两天时间阅读这个主题,并多次尝试更改运行 Ubuntu 22.04 Server(无头)的 raspberry pi 4b 上的 i2c 时钟频率。
因为我不使用 rasbian,所以我不能只编辑启动配置文件中的一行。而是必须反汇编、修改并重新编译设备树文件。
按照这个教程: 在 Ubuntu 20.04 Raspberry Pi 上启用 SPI 和 I2C
应用于此文件:
/boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb
使用此命令来反汇编:
dtc -I dtb -O dts -o bcm2711-rpi-4-b.dts /boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb
然后我修改了该.dts(设备树源)中的这些区域
在以下两个代码块中我替换了
clock-frequency = <0x186a0>; // 100000
和
clock-frequency = <0x61a80>; // 400000
区块 1
i2c@7e205000 {
compatible = "brcm,bcm2711-i2c\0brcm,bcm2835-i2c";
reg = <0x7e205000 0x200>;
interrupts = <0x00 0x75 0x04>;
clocks = <0x08 0x14>;
#address-cells = <0x01>;
#size-cells = <0x00>;
status = "disabled";
#clock-frequency = <0x186a0>; // 100000
clock-frequency = <0x61a80>; // 400000
phandle = <0x10>;
};
以及第 2 区
i2c@7e804000 {
compatible = "brcm,bcm2711-i2c\0brcm,bcm2835-i2c";
reg = <0x7e804000 0x1000>;
interrupts = <0x00 0x75 0x04>;
clocks = <0x08 0x14>;
#address-cells = <0x01>;
#size-cells = <0x00>;
status = "disabled";
pinctrl-names = "default";
pinctrl-0 = <0x18>;
#clock-frequency = <0x186a0>; // 100000
clock-frequency = <0x61a80>; // 400000
phandle = <0x36>;
};
接下来我修改了以下代码块:
#i2c1_baudrate = "\0\0\06clock-frequency:0"; // modified this
i2c1_baudrate = "\0\0\06clock-frequency:400000"; // modified this
这里整个覆盖堵塞
__overrides__ {
cam0-pwdn-ctrl;
cam0-pwdn;
cam0-led-ctrl;
cam0-led;
krnbt = "\0\0\00status";
krnbt_baudrate = "\0\0\00max-speed:0";
cache_line_size;
uart0 = "\0\0\01status";
uart1 = "\0\0\02status";
i2s = "\0\0\03status";
spi = "\0\0\04status";
i2c0 = [00 00 00 10 73 74 61 74 75 73 00 00 00 00 35 73 74 61 74 75 73 00];
i2c1 = "\0\0\06status";
i2c0_baudrate = [00 00 00 10 63 6c 6f 63 6b 2d 66 72 65 71 75 65 6e 63 79 3a 30 00]; // left this alone
#i2c1_baudrate = "\0\0\06clock-frequency:0"; // modified this
i2c1_baudrate = "\0\0\06clock-frequency:400000"; // modified this
audio = "\0\0\07status";
watchdog = "\0\0\08status";
random = "\0\0\09status";
sd_overclock = "\0\0\0:brcm,overclock-50:0";
sd_force_pio = "\0\0\0:brcm,force-pio?";
sd_pio_limit = "\0\0\0:brcm,pio-limit:0";
sd_debug = "\0\0\0:brcm,debug";
sdio_overclock = "\0\0\0;brcm,overclock-50:0\0\0\0\0<brcm,overclock-50:0";
axiperf = "\0\0\0=status";
arm_freq;
act_led_gpio = "\0\0\0>gpios:4";
act_led_activelow = "\0\0\0>gpios:8";
act_led_trigger = "\0\0\0>linux,default-trigger";
pwr_led_gpio = "\0\0\0?gpios:4";
pwr_led_activelow = "\0\0\0?gpios:8";
pwr_led_trigger = "\0\0\0?linux,default-trigger";
eth_led0 = "\0\0\0/led-modes:0";
eth_led1 = "\0\0\0/led-modes:4";
sd_poll_once = "\0\0\0@non-removable?";
spi_dma4 = <0x34 0x646d6173 0x3a303d00 0x41 0x34 0x646d6173 0x3a383d00 0x41>;
};
之后我使用以下命令将 .dts 文件重新编译为 .dtb:
dtc -I dts -O dtb -o bcm2711-rpi-4-b.dtb bcm2711-rpi-4-b.dts
然后将文件复制回最初找到的目录并覆盖现有文件。当然我有备份。
sudo cp bcm2711-rpi-4-b.dtb /boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb
但它仍然不起作用。从 Adafruit 执行 python 测试文件时,我收到以下错误:
python3 imu_v2.py
********** Packet *************
DBG:: HEADER:
DBG:: Data Len: 17
DBG:: Channel: CONTROL (2)
DBG:: ** UNKNOWN Report Type **: 0x7c
DBG:: Sequence number: 19
DBG:: Data:
DBG:: [0x04] 0x7C 0x05 0x00 0x00
DBG:: [0x08] 0x00 0x50 0xC3 0x00
DBG:: [0x0C] 0x00 0x00 0x00 0x00
DBG:: [0x10] 0x00 0x00 0x00 0x00
DBG:: [0x14] 0x00
*******************************
Traceback (most recent call last):
File "/home/administrator/Documents/raspberrypi_software/python/src/imu_v2.py", line 19, in <module>
bno.enable_feature(BNO_REPORT_ROTATION_VECTOR)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 999, in enable_feature
self._process_available_packets(max_packets=10)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 803, in _process_available_packets
self._handle_packet(new_packet)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 863, in _handle_packet
raise error
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 858, in _handle_packet
_separate_batch(packet, self._packet_slices)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 373, in _separate_batch
required_bytes = _report_length(report_id)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 362, in _report_length
return _AVAIL_SENSOR_REPORTS[report_id][2]
KeyError: 124
这是我正在尝试运行的程序,根据 adafruit 文档,它需要 i2c 时钟频率达到 400kHz。
import time
import board
import busio
from adafruit_bno08x import (
BNO_REPORT_ACCELEROMETER,
BNO_REPORT_GYROSCOPE,
BNO_REPORT_MAGNETOMETER,
BNO_REPORT_ROTATION_VECTOR,
)
from adafruit_bno08x.i2c import BNO08X_I2C
i2c = busio.I2C(board.SCL, board.SDA, frequency=400000)
#i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)
bno = BNO08X_I2C(i2c)
bno.enable_feature(BNO_REPORT_ACCELEROMETER)
bno.enable_feature(BNO_REPORT_GYROSCOPE)
bno.enable_feature(BNO_REPORT_MAGNETOMETER)
bno.enable_feature(BNO_REPORT_ROTATION_VECTOR)
while True:
time.sleep(0.5)
imu_data = {}
accel_x, accel_y, accel_z = bno.acceleration # pylint:disable=no-member
imu_data["linear_acceleration"] = {"x": accel_x, "y": accel_y, "z": accel_z}
gyro_x, gyro_y, gyro_z = bno.gyro # pylint:disable=no-member
imu_data["angular_velocity"] = {"x": gyro_x, "y": gyro_y, "z": gyro_z}
mag_x, mag_y, mag_z = bno.magnetic # pylint:disable=no-member
imu_data["magnetic_field"] = {"x": mag_x, "y": mag_y, "z": mag_z}
quat_i, quat_j, quat_k, quat_real = bno.quaternion # pylint:disable=no-member
imu_data["orientation"] = {"x": quat_i, "y": quat_j, "z": quat_k, "w": quat_real}
# Populate the 3x3 covariance matrices with zeros (assuming no covariance)
imu_data["orientation_covariance"] = [0.0] * 9
imu_data["angular_velocity_covariance"] = [0.0] * 9
imu_data["linear_acceleration_covariance"] = [0.0] * 9
print(imu_data)
print("")
是的,正如您在脚本中看到的那样,我尝试了 100000 和 400000。
这是它在崩溃并产生错误跟踪之前产生的输出,可以在下方找到。请注意,由于重复信息,输出被大量删减。通常运行约 10-15 秒后才会崩溃。可能是某些缓冲区已填满?
administrator@hostname:~/Documents/raspberrypi_software/python/src$ python3 imu_v2.py
{'linear_acceleration': {'x': 0.3046875, 'y': -0.11328125, 'z': 9.578125}, 'angular_velocity': {'x': 0.001953125, 'y': 0.0, 'z': 0.0}, 'magnetic_field': {'x': -4.0, 'y': -9.5625, 'z': 54.4375}, 'orientation': {'x': -0.005615234375, 'y': -0.01409912109375, 'z': 0.0001220703125, 'w': 0.9998779296875}, 'orientation_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'angular_velocity_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'linear_acceleration_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]}
{'linear_acceleration': {'x': 0.26953125, 'y': -0.11328125, 'z': 9.58203125}, 'angular_velocity': {'x': 0.0, 'y': 0.0, 'z': 0.0}, 'magnetic_field': {'x': -4.75, 'y': -11.375, 'z': 49.75}, 'orientation': {'x': -0.00567626953125, 'y': -0.01416015625, 'z': 6.103515625e-05, 'w': 0.9998779296875}, 'orientation_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'angular_velocity_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'linear_acceleration_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]}
<data ommited>
Large chunk of data ommited due to character limit
</data ommited>
{'linear_acceleration': {'x': 0.3046875, 'y': -0.15234375, 'z': 9.578125}, 'angular_velocity': {'x': 0.0, 'y': 0.0, 'z': 0.0}, 'magnetic_field': {'x': -4.0, 'y': -9.9375, 'z': 54.1875}, 'orientation': {'x': -0.0064697265625, 'y': -0.015380859375, 'z': -6.103515625e-05, 'w': 0.9998779296875}, 'orientation_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'angular_velocity_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'linear_acceleration_covariance': [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]}
最后,上述输出在 10-15 秒后出现的错误跟踪:
********** Packet *************
DBG:: HEADER:
DBG:: Data Len: 191
DBG:: Channel: INPUT_SENSOR_REPORTS (3)
DBG:: Report Type: BASE_TIMESTAMP (0xfb)
DBG:: Sensor Report Type: ACCELEROMETER(0x1)
DBG:: Sequence number: 87
DBG:: Data:
DBG:: [0x04] 0xFB 0xFB 0x12 0x00
DBG:: [0x08] 0x00 0x01 0xE1 0x02
DBG:: [0x0C] 0x00 0x45 0x00 0xE3
DBG:: [0x10] 0xFF 0x95 0x09 0x03
DBG:: [0x14] 0xD1 0x00 0x16 0xB4
DBG:: [0x18] 0xFF 0x56 0xFF 0x44
DBG:: [0x1C] 0x03 0x05 0xD0 0x00
DBG:: [0x20] 0x7A 0x99 0xFF 0x00
DBG:: [0x24] 0xFF 0x00 0x00 0x7E
DBG:: [0x28] 0xBF 0x44 0x32 0x01
DBG:: [0x2C] 0xE2 0x06 0x3D 0x4E
DBG:: [0x30] 0x00 0xE3 0xFF 0x94
DBG:: [0x34] 0x09 0x02 0xD2 0x04
DBG:: [0x38] 0x3F 0x00 0x00 0x00
DBG:: [0x3C] 0x00 0x00 0x00 0x03
DBG:: [0x40] 0xD2 0x08 0x0B 0xBA
DBG:: [0x44] 0xFF 0x56 0xFF 0x2B
DBG:: [0x48] 0x03 0x05 0xD1 0x08
DBG:: [0x4C] 0x6F 0x98 0xFF 0x00
DBG:: [0x50] 0xFF 0x00 0x00 0xFE
DBG:: [0x54] 0x3F 0x44 0x32 0x01
DBG:: [0x58] 0xE3 0x0A 0x78 0x4E
DBG:: [0x5C] 0x00 0xD9 0xFF 0x94
DBG:: [0x60] 0x09 0x02 0xD3 0x0C
DBG:: [0x64] 0x37 0x00 0x00 0x00
DBG:: [0x68] 0x00 0x00 0x00 0x01
DBG:: [0x6C] 0xE4 0x0E 0xB6 0x45
DBG:: [0x70] 0x00 0xE3 0xFF 0x95
DBG:: [0x74] 0x09 0x03 0xD3 0x10
DBG:: [0x78] 0x00 0xBA 0xFF 0x5B
DBG:: [0x7C] 0xFF 0x38 0x03 0x05
DBG:: [0x80] 0xD2 0x10 0x63 0x98
DBG:: [0x84] 0xFF 0x00 0xFF 0x00
DBG:: [0x88] 0x00 0xFE 0x3F 0x44
DBG:: [0x8C] 0x32 0x01 0xE5 0x12
DBG:: [0x90] 0xF7 0x4E 0x00 0xE3
DBG:: [0x94] 0xFF 0x8A 0x09 0x02
DBG:: [0x98] 0xD4 0x14 0x2C 0x00
DBG:: [0x9C] 0x00 0x00 0x00 0x00
DBG:: [0xA0] 0x00 0x03 0xD4 0x14
DBG:: [0xA4] 0xF4 0xB4 0xFF 0x5B
DBG:: [0xA8] 0xFF 0xB1 0x03 0x01
DBG:: [0xAC] 0xE6 0x1A 0x36 0x4E
DBG:: [0xB0] 0x00 0xE3 0xFF 0x94
DBG:: [0xB4] 0x04 0xFF 0xFF 0xFF
DBG:: [0xB8] 0xFF 0xFF 0xFF 0xFF
DBG:: [0xBC] 0xFF 0xFF 0xFF 0xFF
DBG:: [0xC0] 0xFF 0xFF 0xFF
*******************************
Traceback (most recent call last):
File "/home/administrator/Documents/raspberrypi_software/python/src/imu_v2.py", line 26, in <module>
accel_x, accel_y, accel_z = bno.acceleration # pylint:disable=no-member
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 598, in acceleration
self._process_available_packets()
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 803, in _process_available_packets
self._handle_packet(new_packet)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 863, in _handle_packet
raise error
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 858, in _handle_packet
_separate_batch(packet, self._packet_slices)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 373, in _separate_batch
required_bytes = _report_length(report_id)
File "/home/administrator/.local/lib/python3.10/site-packages/adafruit_bno08x/__init__.py", line 364, in _report_length
return _REPORT_LENGTHS[report_id]
KeyError: 255
不确定我遗漏了什么。目前,我仍然不确定我的总线是否真的改变了时钟频率。有人知道吗:
- 如何检查运行 Ubuntu 22.04(不是 raspbian)的 rpi 4b 上的 i2c 时钟频率
- 我错过了什么,这里出了什么问题?
答案1
我已能够解决这个问题,并希望将解决方案发布出来,以便其他人能够:
- 有一个运行 Ubuntu 的树莓派
- 需要改变 i2c 时钟速度(从 100000 到 400000)
在对 dts 和 dtb 进行研究之后,决定只使用 dt-overlays。
首先我创建了一个新文件“i2c1-frequency-overlay.dts”
// i2c1-frequency-overlay.dts
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2711";
fragment@0 {
target = <&i2c1>;
__overlay__ {
clock-frequency = <400000>; // Set your desired frequency here (e.g., 400 kHz)
};
};
};
其次我使用以下命令编译了此文件:
dtc -I dts -O dtb -o i2c1-frequency-overlay.dtbo i2c1-frequency-overlay.dts
这创建了 dtb 文件
i2c1-frequency-overlay.dtbo
然后将其复制到 /boot/firmware/overlays/
sudo cp i2c1-frequency-overlay.dtbo /boot/firmware/overlays/
接下来我修改了 /boot/firmware/config.txt
sudo nano /boot/firmware/config.txt
并附加以下内容:
# Added below to change i2c frequency from 100kHz [default] to 400kHz [fast mode]
dtparam=i2c_arm_baudrate=400000
dtoverlay=i2c1-frequency-overlay
最后一步 重启
sudo reboot
只是为了验证 i2c 时钟频率是否更新:
// this did not work:
cat /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency
做了一些研究,发现了以下内容
// but this worked
echo 0x$(xxd /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency | cut -f 2,3 -d ' ' --output-delimiter='') | xargs printf "%d\n"
-bash: warning: command substitution: ignored null byte in input
400000
-> 这确认总线频率为 400kHz :-)