The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.
Issue: i2c unintended write (Closed: Fixed)
I guess that I have found the root cause for the problem described in
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
i2c_clear_SI(obj); should be deleted.
The clearing of the interrupt bit triggers an unintended write of an additional byte.
Usually this does not hurt, because in most cases this write is aborted by the STOP condition in the next line or a RESTART condition of a following read request. But if the task running the i2c sequence is interrupted e.g. by the rtos scheduler (see link above) or by a simple
wait as in the referenced example code, the i2c HW will write the unintended byte and mess up the following i2c request.
The example program simply reads from an SRF08 ultra sonic ranger one data byte by first writing the register address to the device and then reading back the requested data. If one adds a short wait (23µs) in between the write and the read, the read request fails: Program serial dump:
wStat=0x00 rStat=0x00 rData=0x0A OK!
The first attempt without the 23µs break works fine. The second one with the short pause fails and reports status "0x28 Data byte in I2DAT has been transmitted; ACK has been received."
The effect is also visible on the bus:
wStat=0x00 rStat=0x28 rData=0xFF OUCH!
If the the clearing of the serial interrupt bit in line 309 of i2c_api.c is removed, everything works fine.