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
  • @martinbl Here it is! Please ignore macro function.

    ret_code_t i2c_init(){
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : start");
    #endif
    
        ret_code_t err_code;
        const nrf_drv_twi_config_t config =
        {
           .scl                = I2C_SCL_M,
           .sda                = I2C_SDA_M,
           .frequency          = NRF_TWI_FREQ_400K,
           .interrupt_priority = APP_IRQ_PRIORITY_LOW,
           .clear_bus_init     = false
        };
        
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : nrf_drv_twi_init");
    #endif
    
        err_code = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : nrf_drv_twi_enable");
    #endif
    
        nrf_drv_twi_enable(&m_twi_master);
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : end");
    #endif
    
        return err_code;
    }
    
Reply
  • @martinbl Here it is! Please ignore macro function.

    ret_code_t i2c_init(){
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : start");
    #endif
    
        ret_code_t err_code;
        const nrf_drv_twi_config_t config =
        {
           .scl                = I2C_SCL_M,
           .sda                = I2C_SDA_M,
           .frequency          = NRF_TWI_FREQ_400K,
           .interrupt_priority = APP_IRQ_PRIORITY_LOW,
           .clear_bus_init     = false
        };
        
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : nrf_drv_twi_init");
    #endif
    
        err_code = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : nrf_drv_twi_enable");
    #endif
    
        nrf_drv_twi_enable(&m_twi_master);
    
    #if NEOSARCHIZO_DEBUG == 1
        NRF_LOG_INFO("i2c_init : end");
    #endif
    
        return err_code;
    }
    
Children
No Data
Related