7 years, 1 month ago.

I2c documentation does not match findings

I'm having trouble with the I2C API in the mbed library for an LPC1768. The documentation tells me that the i2c.write(*multiple args*) returns an int showing the amount of bytes written. If I understand correctly, this should mean that if a NAK was received, either an error or 0 should be returned.

The thing is, I get a value for every address that is NOT attached to the bus and 0 for all addresses that ARE connected. Is this an error in the documentation or in my reasoning?

Below is the code I used when I discovered this. The output is a list of all addresses without 0xA6 and 0xA7 (LSB is forced 0 on write so this makes sence since the device connected is on address 0xA6).

I2c

#include mbed.h

Serial pc(USBTX, USBRX); // tx, rx
I2C i2c(p28,p27);

uint8_t buf[2];
char * bufPtr = (char *)&buf[0];

int main() {
    i2c.frequency(400000);
    pc.printf("starting\r\n");
    
    buf[0]=0x00;
    buf[1]=0x00;
      
    for (int i=0x05;i<=0xFF;i++){
            int status = i2c.write(i,bufPtr,2,false);
            if (status)
                pc.printf("Write address: %02x Message: %02x\r\n",i,buf[0]);
        }      
}
Be the first to answer this question.