7 years, 9 months ago.

i2C is not working on single byte write

Here is code and result below, and first way is normal, I use stm32f070rbt6, is anyone know the answer?? the mbed version is 121, It should be simple use

/media/uploads/stu0219/fff.png

include the mbed library with this snippet

#include "mbed.h"
//DigitalIn ok(USER_BUTTON);
//Serial pc(SERIAL_TX, SERIAL_RX);
I2C i2c(PB_7,PB_6);
const int addr8bit =0x40;
    
int main() {
    char buffer[8];
//    pc.printf("go!!");

    while(1) {
        wait_us(100);
        buffer[0]=0x00;
        buffer[1]=0x81;
        i2c.write(addr8bit,buffer,2);
        
        wait_us(100);
        i2c.start();
        i2c.write(addr8bit);
        i2c.write(0x00);
        i2c.write(0x82);
        i2c.stop();      
             
    }
}

1 Answer

7 years, 9 months ago.

Unfortunately the I2C byte write/read operations have issues/bugs on several platforms. It works on the original LPC1768 and LPC11u24 mbed platforms, but there are problems on ST hardware and also on some of the later NXP platforms. This is mainly due to the I2C hardware engines that dont really support single byte write/read without preceding startcondition and slaveaddress. I recommend that you always use the I2C blockwrite and read operations instead. In case you really need single byte operations (eg if you want to continue transmitting even without an ACK) you should either use the original mbed platforms or first do thorough testing on the byte library of another platform or implement a bit banged I2C routine by yourself.

Accepted Answer

Thanks your answer, I also try the long size array write by the way (5000 byte data), and it doesn't work either, mbed might set some mechanism or reference to avoid this normally opreation?

posted by Cropse Ma 11 Jul 2016