When master device transfers a piece of data, slave will acknowledge back once the data has been received by slave. So it's not possible to ensure the data integrity by only polling ACK.
I implemented a C function:
unsigned char i2c_write_byte(unsigned char s);
Here I'm returning the ACKSTAT bit. So if there is not an ACK sent by the slave, I will try to send the data again by using:
while(!i2c_write_byte(data));
I know the ninth clock is for ACK, but what if there is no ACK sent back by slave device. Then it should be detected as NACK meaning the slave didn't drive SDA low which is slave never received data transferred.
So on this occasion, should I send data again or send data after a restart?
I can only think of re-send data to work around NACK problem and still can't solve the error happened in the data while in a transfer.
So how to ensure data integrity in I2C?