samtal
Posts: 22
Joined: Thu Dec 24, 2015 2:49 pm

Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 5:45 am

I am unable to modify the Raspberry Pi 3 I2C baud rate. I have tried many options, but while it looks like the baud rate is being changed, in monitoring the actual signals it always remains at 100kHz.

I use the native raspberry Pi3 SMBus commands that seem to work ok, except that there is no rate setting. The various rate change commands I have found on the web are all related to the Pi2, equipped with the BCM2708. The root commands like 'dtparam=i2c_baudrate=50000' and similar relate specifically to the Pi2, and from my tests, they do not affect the Pi3, whether entered inline or saved to the config file and reboot.

Coming from Arduino where I could easily manipulate the core registers, i am unable to access the Broadcom BCM2837 ARM peripherals registers to modify the rate (any advice?).

I must reduce the rate to 40 Khz for an old slave device, although I realize that the problem has not been shown as solved by anyone. I still need help from someone who has done it and can show me how to modify the rate for Pi3
Thx

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 6:08 am

There's a control in /boot/config.txt

Code: Select all

dtparam=i2c_arm=on,i2c_arm_baudrate=40000
default is 100000

That should work on every model. If it doesn't open an issue at https://github.com/raspberrypi/firmware
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 7:20 am

DougieLawson wrote:
Sat Aug 04, 2018 6:08 am
There's a control in /boot/config.txt

Code: Select all

dtparam=i2c_arm=on,i2c_arm_baudrate=40000

I am using Rpi3B+ stretch 2018apr.

I changed I2c speed from default of 100kHz to 40kHz and ran a short test program to write to one register (Day register, with address 0x03) of RTC DS3231, read it back, and print it. I found everything OK.

Before changing speed to 40KHz, I ran the same test at 100kHz, also without any problem.

A year ago I played with a very slow sound chip which works at 50 kHz, and I did not find any problems using 50kHz. Sorry, I remember wrongly, it was only with SPI, not I2C.

Just now I tried another short test program to repeatedly write to the same register, with 10mS pauses, and use a scope to display the waveform and hoped to see the clock SCL 100kHz or 40Hz. But I immediately got I2C error message when I touch my scope probe to SCL or SDA.

If you can suggest me a way to use the scope to display the I2C SCL, SDA signals. I can try it out.

To summarize, my Rpi3b and Rpi3b+ work with 50kHz OK.

Just a thought, did you reboot after changing the I2C speed?


Sorry for the above wrong information. I checked that you are right, setting I2C speed to 50kHz or 40kHz has no effect.
Last edited by tlfong01 on Sat Aug 04, 2018 8:23 am, edited 1 time in total.
I am an electronics and smart home hobbyist.

User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 7:29 am

samtal wrote:
Sat Aug 04, 2018 5:45 am
I am unable to modify the Raspberry Pi 3 I2C baud rate. I have tried many options, but while it looks like the baud rate is being changed, in monitoring the actual signals it always remains at 100kHz.

I use the native raspberry Pi3 SMBus commands that seem to work ok, except that there is no rate setting.

I am using stretch 2018apr, python 3.5, importing smbus. Everything seems OK.

Last year I found my slow device did not work with 100kHz, so I changed to 50kHz, and the slow device worked. This shows that I could indeed change the speed, modifying /boot config.txt

How do you monitor the actual signals? Do you use a scope?


Errata - I remembered wrongly. It was SPI, not I2C, my apologies.
Last edited by tlfong01 on Sat Aug 04, 2018 8:24 am, edited 1 time in total.
I am an electronics and smart home hobbyist.

User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 8:19 am

tlfong01 wrote:
Sat Aug 04, 2018 7:29 am
I have tried many options, but while it looks like the baud rate is being changed, in monitoring the actual signals it always remains at 100kHz.
Last year I found my slow device did not work with 100kHz, so I changed to 50kHz, and the slow device worked. This shows that I could indeed change the speed, modifying /boot config.txt

Errata - I mixed up I2C with SPI. I only found changing SPI speed to 50kHz OK. My apologies

How do you monitor the actual signals? Do you use a scope?

Just now I fed SCL to HC04 input inverter and used my scope to check inverter. As you mentioned,

Setting I2C speed to 40kHz or 50kHz has not effect. I2C speed remains at default 100kHz.

Ah very sorry, now I remember that last time I changed speed to SPI, not I2C. In other words, I never succeeded to change I2C speed to 50kHz.
I am an electronics and smart home hobbyist.

User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 8:33 am

tlfong01 wrote:
Sat Aug 04, 2018 7:29 am
I have tried many options, but while it looks like the baud rate is being changed, in monitoring the actual signals it always remains at 100kHz.

I found the following post you might be interested. It says I2C only can work with 400kHz and 100kHz.

LOWER I2C CLOCK SPEED ON RASPBERRY PI 3 STRETCH Postby Phasitron477 » 2017-Nov-06 Mon 4:43 am
viewtopic.php?p=1231112#p1231112

This is a little late, but I do not belive that the I2C protocol lets you set just any baudrate. Actually, to be technical, SMBus only allows 100K and 400K (K=1000) baudrate, and that may be it. SMBus is an Intel protocol, vs Phillip / NXP's I2C ) is more restrictive than the revamped I2C which support much faster rates. ...
I am an electronics and smart home hobbyist.

samtal
Posts: 22
Joined: Thu Dec 24, 2015 2:49 pm

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 10:05 am

Thanks all for the prompt replies.
.
Unfortunately, as I indicated, I have tried the commonly known solutions. I tested it both as an inline command, and have it in my config.txt file (with reboot), but it does not affect my Raspberry 3B actual I2C rate as can be shown on my scope or logic analyzer. It keeps running at 100kHz.
From all the replies I received so far, it looks really as a dead end, unless someone will try low-level registry manipulation. (As I do need the lower speed, and although I have no experience whatsoever with ARM and BCM2837/5, I may be the one to try.., but it will take its time, if at all.)
From what I learned (briefly), the I2C reg can only accept I^2 dividers (OK with me if I can make it 100/2=50Khz), and it seems as if this will also affect the main cpu speed (?).
If true, this is very strange behavior.
I'll wait for more replies, and soon start to find my own way, if any....

User avatar
DougieLawson
Posts: 35789
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 10:07 am

Have you posted an issue at https://github.com/raspberrypi/firmware?

Because it ain't ever getting fixed if the RPF folks don't know about it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

samtal
Posts: 22
Joined: Thu Dec 24, 2015 2:49 pm

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 10:25 am

Raising an issue is a good idea, but I must first wait a few days to be 100% sure there is a problem. Don't want to make a fool of myself.....

User avatar
tlfong01
Posts: 1312
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 1:40 pm

samtal wrote:
Sat Aug 04, 2018 10:05 am
have it in my config.txt file (with reboot), but it does not affect my Raspberry 3B actual I2C rate as can be shown on my scope or logic analyzer. It keeps running at 100kHz.

From all the replies I received so far, it looks really as a dead end, ...
From what I learned (briefly), the I2C reg can only accept I^2 dividers (OK with me if I can make it 100/2=50Khz),

Just now I also tried setting 400kHz, but no effect. To summarize, whether I set 40kHz, 50kHz, 100kHz, or 400kHz, Rpi always runs at 100kHz (checked with a scope).

Some years ago I played with PyBoard using MicroPython. The document said I2C speed could be set to "any integer". But I read users reporting that they even could not set to 200kHz. So PyBoard has the same problem.

I also have a BBC Micro which can run MicroPython I2C. But I don't bother to check, because it is unlikely that BBC Micro can do better than PyBoard or Rpi.
I am an electronics and smart home hobbyist.

User avatar
bensimmo
Posts: 4152
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 3:39 pm

I would open an issue and the engineer and them in the know will be answering.

Forgive my slight ignorance here,
But isn't 100 (standard) and 400 (full speed) just maximum speeds and not actual speed you set. They should just run at what ever the device needs below that.
All you are doing is setting the speed grade...


...
Seems you may need to delay in the address ACK phase if clock stretching or switch to i2c-gpio overlay

https://github.com/raspberrypi/linux/issues/254

samtal
Posts: 22
Joined: Thu Dec 24, 2015 2:49 pm

Re: Raspberry Pi3 I2C baud rate setting

Sat Aug 04, 2018 5:58 pm

There is a dedicated BCM2835 I/O driver (C++) where the baud rate (actually the clock divider) can be changed in 4 steps, but its minimum is 100kHz.
It is true that in I2C protocol, a slave device can hold the clock line to lower the master clock rate, (clock stretching) but this can only be done for minor changes, not half the speed.
I will probably have to consider to use bit-banging to make my slower I2C.
If anyone has such ready program or experience, I'd appreciate any support. As bit-bang program is not hardware dependent, any program will do (preferably in Python or C++, if available).
I found one here: https://github.com/robotrovsky/rpi_i2c_bit_banging, and will have to test it.

I would once again like to express my disappointment with Broadcom's design. (Yes, the problem is with the chip design, not with the software).

Thanks
Samtal

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7131
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Raspberry Pi3 I2C baud rate setting

Sun Aug 05, 2018 8:40 am

Huh? A fair amount of rubbish being posted here.

The hardware is driven off PLLs and divisors. It will go down to about 10kHz IIRC.

Dedicated I/O driver C++ API? Er, the kernel driver is the correct thing to be looking at https://github.com/raspberrypi/linux/bl ... -bcm2835.c. Line 454 is where it is reading the clock-frequency (baud rate) property from device tree. Feel free to check device-tree (/proc/device-tree/soc/i2c@...) for yourself to see whether you have set up the property correctly.
If you're not using the kernel driver then you only have yourself to blame.

Raise an issue on https://github.com/raspberrypi/linux/issues (not firmware as the Linux kernel is the one dealing with this peripheral).
I'm 99% certain that this was working, but I'll check tomorrow anyway.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

samtal
Posts: 22
Joined: Thu Dec 24, 2015 2:49 pm

Re: Raspberry Pi3 I2C baud rate setting

Sun Aug 05, 2018 8:55 pm

Hello 6by9,
"A fair amount of rubbish being posted here"? is that true? Seems like many users had a problem that Broadcom had no solution to.
I wish it was 'rubbish', which would have saved me lots of time and hard work.
If you have a way to reduce the RaspberryPi 3B I2C to 40-50Khz please show us how you do it.
I have thoroughly read the Broadcom's BCM2835 Peripherals datasheet, and could not find a single word about the actual settings.
The only thing I found was that there are 4 bits in the register to set, and the minimum is 100kHz.

in the BCM2835 library (that looks professional) I found the following:

Code: Select all

bcm2835I2 ClockDivider Specifies the divider used to generate the I2C clock from the system clock. 
Clock divided is based on nominal base clock rate of 250MHz

BCM2835_I2C_CLOCK_DIVIDER_2500 	2500 = 10us = 100 kHz

BCM2835_I2C_CLOCK_DIVIDER_626 	622 = 2.504us = 399.3610 kHz

BCM2835_I2C_CLOCK_DIVIDER_150 	150 = 60ns = 1.666 MHz (default at reset)

BCM2835_I2C_CLOCK_DIVIDER_148 	148 = 59ns = 1.689 MHz

Return to “Advanced users”