I2C Lower Level Routines?

10 Jun 2010

Are the lower-level routines which the class i2c. is built available?

I'm building a peripheral slave device around a Microchip PIC 18F microcontroller chip. I need finer control than just i2c.read and i2c.write. In particular I am dealing with SCL clock-stretching while the slave gathers up and formats a slave response. I would like to be able to poll the SCL line and determine when the slave is ready to send back a data packet.

Doug

11 Jun 2010

The blocks in the LPC1768/LPC2368 that implement the I2C, alas, do not give you access to check the SCL line. It hides all of this from you with a state machine (which is one reason we provide the I2C interface we do currently). They do, however, support clock stretching.

You could use a third line as an input, to sample the clock line yourself (much like the example I used to get the Mindstorm sensor working, which I turned into an output to override the clock line):

http://mbed.org/forum/mbed/topic/804/?page=1#comment-3944

We are looking at a finer grained byte level control for the I2C in addition to the existing interface, but this still won't let you control the clock line, I am afraid.

11 Jun 2010

Thanks for your detailed response, Jon. Your idea of using a third line as a monitor is interesting.

You mentioned that clock stretching IS supported in the current i2c class. Does it timeout, because that may be my problem? On receiving a read request from the mbed, the PIC must read 2 digital temperature sensors and two 16 bit counters and format them into a byte string. During this period of data gathering and formatting the PIC will keep the SCL line low. Once the byte string is ready the PIC will send each byte and release the SCL line. I'm wondering if the mbed times out before the PIC returns with the data. It would make sense for the mbed to have a reasonable timeout so it doesn't hang up.

Doug