All works OK at 100kHz. I can verify by scope that the clock frequency is 98 kHz
If I set the clock speed to 400kHz, program still works, but the frequency does not change (still 98 kHz measured).
Same happens if I set it to 1MHz - works, but only at 100kHz.
If I set the value to a non supported value (say 10 kHz), or 200 kHz, the board crashes with a flashing SOS, and the "I2CSpeed Set to .." string is never shown on the Serial monitor.
The code is below
Serial.print(F("Program Start\n"));
Wire.begin();
Wire.setClock(400000L);
Serial.print("I2C Speed Set to ");
Serial.println(I2C_CLOCK);
I think, I saw the same issue (and have never used Wire.setClock() therefore in my project).
In my case: I want to setup also 4 Mbit/s (yes, really fast and outside spec. for MCU - but it works). This was anyway not intended to be working (but it does).
So, I have implemented my own "int I2C_ClockSpeed(int KHzEnum)" function, based on STM32 HAL drivers (but used in an Arduino mbed project).
Here my code for I2C:
I2C.cpp (6.9 KB)
I2C.h (919 Bytes)
Info: what I do:
higher I2C settings need to configure analog and digital filters. At higher frequency - the analog filter might not become "reasonable" anymore.
I select between FOUR different frequencies whereby the config values (real low level value for I2C device register) come from a config array. Some values disable analog filter (on higher frequencies, there is a set of two values for the config).
So, I can choose: 100 KHz, 400 KHz, 1000 KHz, 4 MHz (an enum value 0..3).
I can tweak the real speed with the config value (e.g. also 4.2 MHz for enum 3). The real speed
depends on the config values I set in array. In order to understand these values - you need the MCU chip datasheet (very low level, direct on MCU).