Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
4 months, 3 weeks ago.
i2c.frequency(400000) causes huge slowdown
When I put this near the beginning of my program, sometimes it runs as expected (speed of I2C bus increases about 4 times the default), and other times it seems to cause I2C to run at about 1/20th the normal speed.
i2c.frequency( 400000 );
By 'sometimes' I mean about 50% of the time. Typically the problem appears when I change an unrelated part of the code somewhere else, like if I change the value parameters of a uart printf call. For example, simply changing this: pc.printf("%f\n", someFloat ); ... to this: pc.printf("%f\n", anotherFloat ); and compiling again is enough to trigger the problem. Once the problem has manifested, changing back to someFloat doesn't help, I need to move the i2c.frequency call a bit.
Almost every time, I can 'fix' this problem by slightly changing the location of the I2C frequency call among the other things my program does on startup, and then compile again. The other startup things are unrelated to I2C, for example just setting LED outputs to their initial off state.
If the i2c.frequency call is removed, the program always runs as expected, but it's not as fast as I need it to be.
Maybe I'm missing something here... are there other considerations I need to take into account when setting the I2C frequency? Do I need to wait for some time after setting it before using the bus? (I tried 2 seconds but it made no difference)
Side note: where is the API documentation for the I2C class? The main page (https://os.mbed.com/handbook/I2C) links to this page for the API details, but this page does not exist: https://os.mbed.com/users/mbed_official/code/mbed/docs/tip/classmbed_1_1I2C.html In the IDE under the mbed library it says "Documentation not ready" and running "Update docs" from under the Compile button makes no difference. How am I supposed to know what functions the I2C class has??
4 months, 3 weeks ago.
This is something found from the I2C Speed page: The I2C bus is intended for inter-IC communication and this usually means small data packets. Since the timing can never be determined exactly and the transmitted information is often short the accuracy of the bus clock is of very little relevance in most applications.
To be on the safe side it is – in case of doubt – a much better idea to keep the maximum bit rate below the maximum rating at any time rather than exceeding it occasionally.
So it might have something to do how long data packets you send and how long wires you have between I2C devices.