WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Set I2C clock speed  (Read 326 times)

Offline vainamoinen

  • Newbie
  • *
  • Posts: 4
Set I2C clock speed
« on: February 07, 2025, 11:15:40 AM »
Hello all,

I'm new to tinyCore, and still discovering a lot of things...

I want to connect to a sensor in I2C, which is connected with a long cable (I know that's not a good practice, but I need this length...)
It first worked with a Raspberry Pi Zero WH. I then changed it to a Raspberry Pi Zero 2 WH. And it's not working anymore.
I suspect that the problem comes from the default speed for I2C bus : I may have increased between those boards, and want the Zero 2 I2C clock speed back to 100kHz, if it's feasible.

Can you give me hints on how to do that ?
I'm using a tinycore13 based OS.
If there is some info that can be useful, I'll be glad to post it here.

Thanks,
Vainamoinen

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11829
Re: Set I2C clock speed
« Reply #1 on: February 07, 2025, 12:19:57 PM »
Hi vainamoinen
Welcome to the forum.

If you are using the i2c-bcm2708 driver, this might work:
Code: [Select]
sudo modprobe -r i2c_bcm2708
sudo modprobe i2c_bcm2708 baudrate=100000

To see if that driver is in use, run:
Code: [Select]
lsmod

Offline vainamoinen

  • Newbie
  • *
  • Posts: 4
Re: Set I2C clock speed
« Reply #2 on: February 07, 2025, 05:09:42 PM »
Thanks :-)

I executed
Code: [Select]
[b]tc@pypilot:~$[/b] lsmod
Module                  Size  Used by    Not tainted
gpio_ir_recv           16384  0
ir_rc6_decoder         16384  0
brcmfmac              303104  0
brcmutil               20480  1 brcmfmac
cfg80211              798720  1 brcmfmac
rfkill                 32768  2 cfg80211
sha256_generic         16384  0
i2c_dev                20480  0
squashfs               40960 83
spidev                 20480  4
i2c_bcm2835            16384  0
raspberrypi_hwmon      16384  0
spi_bcm2835            20480  0
zram                   28672  1
zsmalloc               28672  1 zram

So I replaced i2c_bcm2708 by the one I've seen in lsmod i2c_bcm2835.
Didn't get any error, so I guess it worked.
But I don't see any improvements in seeing my sensor.

I also tried to remove i2c_bcm2835, and insert in place i2c_bcm2708.
But no more result.

I'll try to find a way to measure the clock with a logic analyzer I have, to check the clock speed.
I will probably not be able to do more test before 2 or 3 days.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11829
Re: Set I2C clock speed
« Reply #3 on: February 07, 2025, 05:21:12 PM »
Hi vainamoinen
I took a look through the i2c drivers and i2c-bcm2708
had baudrate as an optional parameter:
Code: [Select]
tc@E310:~$ modinfo PiCore/TC14/ARMv7l/Modules7/lib/modules/6.1.68-piCore-v7/kernel/drivers/i2c/busses/i2c-bcm2708.ko.gz | grep "parm:"
parm:           combined:Use combined transactions
parm:           baudrate:The I2C baudrate
tc@E310:~$

i2c_bcm2835 had no such parameter:
Code: [Select]
tc@E310:~$ modinfo PiCore/TC14/ARMv7l/Modules7/lib/modules/6.1.68-piCore-v7/kernel/drivers/i2c/busses/i2c-bcm2835.ko.gz | grep "parm:"
parm:           clk_tout_ms:clock-stretch timeout (mS)
parm:           debug:1=err, 2=isr, 3=xfer
tc@E310:~$

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1325
Re: Set I2C clock speed
« Reply #4 on: February 07, 2025, 07:05:00 PM »
The kernel drivers already default to 100000

CONFIG_I2C_BCM2708_BAUDRATE=100000

Raspberry Pi boards do not have a very precise clock rate for i2c.  You may need to set it differently. What are you using to driver your board.  Most userspace drivers can change the clock rate without having to play with the kernel driver.

« Last Edit: February 07, 2025, 07:07:25 PM by Paul_123 »

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1325
Re: Set I2C clock speed
« Reply #5 on: February 07, 2025, 07:21:13 PM »
I guess I should also say that the proper way in raspi to change the clockrate of the kernel drivers is through device-tree.

Which i2c channel are you connected too?  arm or videocore?

https://github.com/raspberrypi/linux/blob/fbd8b3facb36ce888b1cdcf5f45a78475a8208f2/arch/arm/boot/dts/overlays/README#L269
https://github.com/raspberrypi/linux/blob/fbd8b3facb36ce888b1cdcf5f45a78475a8208f2/arch/arm/boot/dts/overlays/README#L284

to fallback to the older bcm2708 driver, you also need to do that via device-tree
https://github.com/raspberrypi/linux/blob/fbd8b3facb36ce888b1cdcf5f45a78475a8208f2/arch/arm/boot/dts/overlays/README#L1995


Offline vainamoinen

  • Newbie
  • *
  • Posts: 4
Re: Set I2C clock speed
« Reply #6 on: February 09, 2025, 08:48:03 AM »
Hi,

Thanks for the hints.
It seems that the speed isn't the problem.
I'll explore some other paths.

The library used is RTIMULib. I don't know what I2C library that library use, but I didn't see any I2C clock speed parameter, only for SPI.
I'm using the channel that is out on pins 3 and 5. So I guess it's the ARM one, which is channel 1.

For my personal knowledge, as I'm a beginner, can you specify me some little things ?
Sorry, I'm a bit lost here... I don't know which device-tree modify.
Is it in a i2c specific file ?
Or is there somewhere a general file ?

Thanks for your lights.

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1325
Re: Set I2C clock speed
« Reply #7 on: February 09, 2025, 09:11:39 AM »
Read the links I gave you.  These settings would go in config.txt on the boot partition

Offline vainamoinen

  • Newbie
  • *
  • Posts: 4
Re: Set I2C clock speed
« Reply #8 on: February 11, 2025, 03:19:31 AM »
I found the config.txt file when reading the SD card, but didn't in ssh exploring.
Thanks !

I found the problem I had : there was no link with I2C configuration on the raspberry, the sensor was dead. After replacing it, it's working well.

Thanks for your help.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11829
Re: Set I2C clock speed
« Reply #9 on: February 11, 2025, 09:10:31 AM »
Hi vainamoinen
I found the config.txt file when reading the SD card, but didn't in ssh exploring. ...

That's because the mmcblk0p1 partition gets unmounted after
you boot because it's not needed while running the operating
system. You can remount it like this:
Code: [Select]
mount /dev/mmcblk0p1You should then find config.txt under /mnt/mmcblk0p1.