Arm Mbed OS support forum

I2C Async Transfer

I’m having a problem getting I2C transfer working properly. Wondering if someone can give me a pointer. Not sure if it’s something in my code or not. I’ve written a basic program just so see if I can get this to work.

void i2cCallback(int event)
{
    // Check for errors
    if(event & I2C_EVENT_ERROR_NO_SLAVE) {
        state = ST_NOSLAVE;
    } else if (event & I2C_EVENT_ERROR) {
        state = ST_EVNTERROR;
    } else if (event & I2C_EVENT_TRANSFER_EARLY_NACK) {
        state = ST_EARLYNAK;
    } else if (event & I2C_EVENT_TRANSFER_COMPLETE) {
        if(state == ST_IDLE) {
            tx1[0] = LSM9DS1_WHO_AM_I;

            int rval = i2c.transfer(LSM9DS1_ADDRESS << 1, tx1, 1, rxbuf1, 1, i2cCallback, I2C_EVENT_ALL);

            if(rval != 0) {
                state = ST_I2CBUSY;
            } else {
                state = ST_READWAMI;
            }

        } else if (state == ST_READWAMI) {
            // RX Buffer should have data?
            if(rxbuf1[0] == 0x68)
                state = ST_DONE;
            else
                state = ST_FAIL;
        }
    }
}

In the above code works as expected.
States go ST_IDLE->ST_READWAMI->ST_DONE

void i2cCallback(int event)
{
    // Check for errors
    if(event & I2C_EVENT_ERROR_NO_SLAVE) {
        state = ST_NOSLAVE;
    } else if (event & I2C_EVENT_ERROR) {
        state = ST_EVNTERROR;
    } else if (event & I2C_EVENT_TRANSFER_EARLY_NACK) {
        state = ST_EARLYNAK;
    } else if (event & I2C_EVENT_TRANSFER_COMPLETE) {
        if(state == ST_IDLE) {
            tx1[0] = LSM9DS1_WHO_AM_I;

            int rval = i2c.transfer(LSM9DS1_ADDRESS << 1, tx1, 1, rxbuf1, 1, i2cCallback, I2C_EVENT_ALL);

            if(rval != 0) {
                state = ST_I2CBUSY;
            } else {
                state = ST_REQWAMI;
            }

        } else if (state == ST_REQWAMI) {
            tx2[0] = LSM9DS1_WHO_AM_I;

            int rval = i2c.transfer(LSM9DS1_ADDRESS << 1, tx2, 1, rxbuf2, 1, i2cCallback, I2C_EVENT_ALL);

            if(rval != 0) {
                state = ST_I2CBUSY;
            } else {
                state = ST_READWAMI;
            }

        } else if (state == ST_READWAMI) {
            // RX Buffer should have data?
            if(rxbuf2[0] == 0x68)
                state = ST_DONE;
            else
                state = ST_FAIL;
        }
    }
}

But if I try to do a second transfer(Code Above) I never get a callback even after the first one.

States Go
ST_IDLE->ST_REQWAMI

and gets stuck

I’m pulling my hair out on this. Can you not do a repeat callbacks or something.

I’m using this on an Arduino Nano 33 BLE (NRF52 device)

Thank you for any help.