gpt4 book ai didi

How to change I2C clock on Ubuntu 22.04 rpi-4(如何在Ubuntu 22.04 RPI-4上更改I2C时钟)

转载 作者:bug小助手 更新时间:2023-10-25 22:59:11 25 4
gpt4 key购买 nike



The goal: Change i2c clock frequency to 400kHz

目标:将i2c时钟频率更改为400 khz


The motivation: Trying to use Adafruit BNO085 via i2c and there are known issues with rpi i2c clock stretching. (This is a known bug, has been going around since 2012).

动机:试图通过i2c使用Adafruit BNO085,但RPI i2c时钟伸展存在已知问题。(这是一个已知的漏洞,自2012年以来一直在传播)。


I have been reading into this topic and attempting multiple times to change the i2c clock frequency on my raspberry pi 4b running Ubuntu 22.04 Server (headless).

我一直在阅读这个话题,并多次尝试更改我运行Ubuntu 22.04服务器(无头)的覆盆子pi4b的i2c时钟频率。


Since I don't use Rasbian I can NOT just edit a line in my boot-config file. But rather I have to disassemble, modify and recompile a device-tree file.

因为我不使用Rasbian,我不能只是编辑我的引导配置文件中的一行。而是我必须反汇编,修改和重新编译一个设备树文件。


Followed this tutorial:
https://askubuntu.com/questions/1273700/enable-spi-and-i2c-on-ubuntu-20-04-raspberry-pi

遵循本教程:https://askubuntu.com/questions/1273700/enable-spi-and-i2c-on-ubuntu-20-04-raspberry-pi


Applied to this file:

适用于此文件:


/boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb

used this command to disassemble:

使用此命令反汇编:


dtc -I dtb -O dts -o bcm2711-rpi-4-b.dts /boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb

Then I modified these areas in that .dts (device-tree-source)

然后我在.dts(设备树源)中修改了这些区域


in the following two code chunks I replaced

在我替换的以下两个代码块中


clock-frequency = <0x186a0>; // 100000

with

使用


clock-frequency = <0x61a80>; // 400000

Block 1

区块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>;
};

and also Block 2

也是区块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>;
};

Further down I modified the following code Block:

再往下,我修改了以下代码块:


#i2c1_baudrate = "\0\0\06clock-frequency:0"; // modified this
i2c1_baudrate = "\0\0\06clock-frequency:400000"; // modified this

Here the entire overrides Block

在这里,整个覆盖块


__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>;
};

after that I re-compiled the .dts file back into .dtb using this command:

之后,我使用以下命令将.dts文件重新编译回.dtb:


dtc -I dts -O dtb -o bcm2711-rpi-4-b.dtb bcm2711-rpi-4-b.dts

and then copied the file back into the directory where it was originally found and overwrote the existing file. Of course I have a backup.

然后将该文件复制回最初找到它的目录,并覆盖现有文件。我当然有备份了。


sudo cp bcm2711-rpi-4-b.dtb /boot/dtbs/5.15.0-1033-raspi/bcm2711-rpi-4-b.dtb

But it still does not work. When executing the python test file from Adafruit I get the following error:

但它仍然不起作用。从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

This is the program I'm trying to run, which according to adafruit documentation will require the i2c-clock-frequency to be at 400kHz.

这是我正在尝试运行的程序,根据Adafruit文档,它将要求i2c时钟频率为400 Khz。


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

And yes, I tried with both 100000 and 400000 as you can see in the script.

是的,正如你在剧本中看到的那样,我同时尝试了100000和400000。


And here the output it produces, before it crashes and produces an error-trace, which can be found further down. Please note the output is heavily redacted due to repetitive information. Usually runs for about 10-15s before crashing. Could be some buffer filling up?

在这里,它产生的输出,在崩溃和产生错误跟踪之前,可以在更低的位置找到。请注意,由于重复的信息,输出内容被大量编辑。通常在崩溃前运行约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]}

And finally the error trace that appears after 10-15s of above output:

最后是上述输出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

Not sure what I'm missing. At this point I'm still not sure if my bus has actually changed clock-frequency. I am seeking:

不知道我错过了什么。在这一点上,我仍然不确定我的公交车是否真的改变了时钟频率。我在寻找:



  1. how to check the clock-frequency of i2c on rpi 4b running Ubuntu 22.04 (NOT raspbian)

  2. what I am missing, what's wrong here?


更多回答

Super specialized topic with several pages of code and dumps != “basic inquiry”. At the very least, the people that will have knowledge about this will be somewhat scarce (as compared to, say, javascript questions), and they may simply not have seen your question.

带有几页代码和转储的超级专业主题!=“基本查询”。至少,了解这方面知识的人会比较稀少(比方说,与Java脚本问题相比),而且他们可能根本没有看到你的问题。

优秀答案推荐

I was able to solve this problem and wanted to post the solution, for anyone else who:

我能够解决这个问题,并希望将解决方案发布给其他任何人:



  1. has a raspberry pi running Ubuntu

  2. needs to change the i2c clock speed (from 100000 to 400000)


After this excursion into dts and dtb it was decided to simply use dt-overlays.

在进入DTS和DTB之后,决定简单地使用DT覆盖。


First I created a new file "i2c1-frequency-overlay.dts"

首先,我创建了一个新文件“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)
};
};
};

Second I compiled this file using:

其次,我使用以下命令编译了该文件:


dtc -I dts -O dtb -o i2c1-frequency-overlay.dtbo i2c1-frequency-overlay.dts

This created the dtb file

这将创建DTB文件


i2c1-frequency-overlay.dtbo

which then was copied to /boot/firmware/overlays/

然后将其复制到/Boot/Firmware/Overays/


sudo cp i2c1-frequency-overlay.dtbo /boot/firmware/overlays/

Next I modified /boot/firmware/config.txt

接下来,我修改了/Boot/Firmware/config.txt


sudo nano /boot/firmware/config.txt

and appended the following:

并附上以下文件:


# Added below to change i2c frequency from 100kHz [default] to 400kHz [fast mode]
dtparam=i2c_arm_baudrate=400000
dtoverlay=i2c1-frequency-overlay

Last step reboot

最后一步重新启动


sudo reboot

Just to verify the i2c clock frequency updated:

只需验证更新的i2c时钟频率:


// this did not work:
cat /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency

did some more research and found the following

做了更多的研究,发现了以下几点


// 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

-> this confirms the bus is at 400kHz :-)

->这确认了公交车在400赫兹:-)


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com