leaflabs / libmaple

[INACTIVE] C and C++ library for STM32 ARM Cortex-M3 development boards.
http://leaflabs.com/docs/libmaple.html
Other
304 stars 218 forks source link

Bug in Wire process() #100

Closed rogerclarkmelbourne closed 10 years ago

rogerclarkmelbourne commented 10 years ago

Hi, There is almost certainly a bug in Wire::process(), which occurs when you attempt to address a device that is not connected (and may occur under other circumstances)

What happens is that in i2c_get_ack, SCL is left low at the end of the function (see below)

bool TwoWire::i2c_get_ack() { set_scl(LOW); set_sda(HIGH); set_scl(HIGH); bool ret = !digitalRead(this->sda_pin); set_scl(LOW); return ret; }

And in process() the function returns if there has not been an ACT (this happens in 2 places in process()

if (!i2c_get_ack()) { return ENACKADDR; }

Then when the next call to process() occurs (via Wire::endTransmission() ) SCL is low when it should be high, and the first clock falling edge, doesn't occur, hence only 6 address bits and the RW flag are clocked out.

So once there has been an error where ACK has not been received.

Wire totally breaks and never recovers.

The simple solution appears to be to Set SCL HIGH before exiting if there has been a failure to get an ACT

e.g.

if (!i2c_get_ack()) { set_scl(HIGH);// Set SCL high ready for next transfer return ENACKADDR; }

This needs to happen in both locations.

On another associated note.

Please can someone tell me if this version of Wire is the latest version, as I've noticed its dated 2012, however on the LeafLabs server http://static.leaflabs.com/pub/leaflabs/maple-ide/ The latest IDE is dated 2013, but contains a completely different implementation of Wire

So either this Repo has not been updated with the latest wire from the static copy of IDE or vice versa

mbolivar commented 10 years ago

Hi,

I pushed 40bffe3, which should fix the issue. Please let me know if it works for you.

Regarding dates, the later IDE version was just to fix up some Windows issues; the version of Wire it contains is older than this one in libmaple.