Question About I2C Start()
CJMJ
Posts: 226
I've been studying the I2C standard and I know that to issue a start() command the SCL must be HIGH. My question is how long should I wait for the SCL to go high before aborting my attempt at a start() command and returning an error?
Comments
So timeout is more device specific and if the SCL is being pulled low then there is normally nothing you can do about it anyway, other than to abort in some device dependent manner.
Which device are you talking to?
Are you sure there is a pull down.
I could just put the I2C calls in a separate cog and periodically poll the cog. If it becomes nonresponsive I could stop the cog and report a failure on the I2C bus.
As you've demonstrated a potential failure mode in your system, you might want to modify your code to verify that SDA is high -- the slave should release the SDA line after providing its final data bit in a read command. If I were to add a return value, I might do it like this:
-Phil
con
scl_pin = 8
sda_pin = 9
khz = 100
obj
i2c : "jm_i2c"
PUB go() | result
i2c.setup(scl_pin, sda_pin, khz, {pullup} i2c.PU_NONE)
result := i2c.present($A5)
if result
PINL(56)
else
PINL(57)
repeat
I guess the bottom line is: I didn't expect the driver to go into an infinite loop if I loss power to the slave. I guess I was expected i2c.present($A5) to return false, but I don't see how that could be implemented without a timeout spec. I guess I could add pullups on the P2's pins and see what that buys me. I should have also tried JonnyMac's scanner code. I'll do both of those tonight if I get a chance.
BTW, where is it documented that the P2's EVAL LEDs are active low? This was a head scratcher for a bit.
After thinking about it, there's no reason to modify the I2C object. If losing power to a device on your bus is a possible problem, you can easily check the bus pins before entering into any I2C sequences. Both pins should be high. Something like this, perhaps:
Some excerpts from I2C Spec rev 6
Table 2. Clock stretching is a feature of some slaves. If no slaves in a system can stretch the clock (hold SCL LOW),
the master need not be designed to handle this procedure.
3.1.1 For a single master application, the master’s SCL output can be a push-pull driver design
if there are no devices on the bus which would stretch the clock.
(my note: this means no pull-up required - so P1 EEPROM only systems don't really need it)
3.1.9 Clock stretching
Clock stretching pauses a transaction by holding the SCL line LOW. The transaction
cannot continue until the line is released HIGH again. Clock stretching is optional and in
fact, most slave devices do not include an SCL driver so they are unable to stretch the
clock.
I put the hold timeout in Spin i2c code as follows with a timeout.
The check for high levels on sda and scl is also a good idea, certainly as a part of the initialization, as well as a quick "hello" scan to verify that the expected devices are present on the bus. If multiple cogs are going to access one i2c bus, then something by design has to insure that they will not access and collide.