This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Two ways to send two or more bytes with nrf_drv_twi_tx

Hi! I use ICM-20602 motion sensor that is similar to MPU6050. Its communication type is also I2C.

I made I2C wrapping module for ICM-20602. In the module, I added i2c_write_byte and i2c_write_bytes functions like following code.

ret_code_t i2c_write_byte(uint8_t dev_addr, uint8_t reg_addr, uint8_t data) 
{
    ret_code_t err_code;
    uint8_t tx_data[2] = { reg_addr, data };

    return nrf_drv_twi_tx(&m_twi_master, dev_addr, tx_data, 2, false);
}

But i2c_write_byte was not like this code. i2c_write_byte was like following code.

ret_code_t i2c_write_byte(uint8_t dev_addr, uint8_t reg_addr, uint8_t data) 
{
    ret_code_t err_code;

    err_code = nrf_drv_twi_tx(&m_twi_master, dev_addr, &reg_addr, 1, true);
    if (err_code != NRF_SUCCESS) return err_code;
    
    err_code = nrf_drv_twi_tx(&m_twi_master, dev_addr, &data, 1, false);
    
    return err_code;
}

But this old i2c_write_byte function did not work well. I think that operations of these i2c_write_byte functions must be an exact match. I read and read again API documents. But results of these i2c_write_byte functions are different. I don't know why. Please explain why results of these i2c_write_byte functions are different.

Parents
  • Thanks. I figured that maybe you were using the driver in non-blocking mode with an event handler. Then it wouldn't work to call two tx functions right after one another like you do in i2c_write_byte(). But obviously you use your code in blocking mode so that can't be the problem. Please get back to us when you have tested with a logic analyzer then.

Reply
  • Thanks. I figured that maybe you were using the driver in non-blocking mode with an event handler. Then it wouldn't work to call two tx functions right after one another like you do in i2c_write_byte(). But obviously you use your code in blocking mode so that can't be the problem. Please get back to us when you have tested with a logic analyzer then.

Children
No Data
Related