WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

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

Offline vainamoinen

  • Newbie
  • *
  • Posts: 2
Set I2C clock speed
« on: Today at 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: 11818
Re: Set I2C clock speed
« Reply #1 on: Today at 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: 2
Re: Set I2C clock speed
« Reply #2 on: Today at 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: 11818
Re: Set I2C clock speed
« Reply #3 on: Today at 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: 1321
Re: Set I2C clock speed
« Reply #4 on: Today at 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: Today at 07:07:25 PM by Paul_123 »

Offline Paul_123

  • Administrator
  • Hero Member
  • *****
  • Posts: 1321
Re: Set I2C clock speed
« Reply #5 on: Today at 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