Hi everyone,
I am looking the TWI examples and I am confused regarding the nrf_drv_twi_tx and nrf_drv_twi_rx. What I've understand so far is that the nrf_drv_twi_tx function is used either to write on a register or before reading a register right?
1. What I have seen so far is that, in order to read a specific register you have to first write on it, to instruct the slave which register it wishes to read from. After that you can read the register by using nrf_drv_twi_rx. So, fisrt send a write command using the nrf_drv_twi_tx. What is the length parameter that we send in this case?
For example looking this I2C Read wrapping function, the lengh parameter of nrf_drv_twi_tx is 1. In that case it writes 1 to reg_addr right? What is the purpose? This is always the case before reading a register?
int8_t Acc_i2c_Read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
int8_t rslt = 0;
rslt = nrf_drv_twi_tx(&m_twi, dev_id, ®_addr, 1, false);
APP_ERROR_CHECK(rslt);
if (rslt == 0) {
rslt = nrf_drv_twi_rx(&m_twi, dev_id, reg_data, len);
}
return rslt;
}
2. Then I was looking an I2C Write wrapping function. This is also confusing for me. Looking the function the data parameter holds both the reg_address and reg_data. Then it passes data to nrf_drv_twi_tx. This is always the case? The the first index of data parameter holds the register address and the rest of the indexes the data we wish to write?
int8_t Acc_i2c_Write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len) {
int8_t rslt = 0;
uint8_t data[len + 1];
data[0] = reg_addr;
for (uint16_t i = 0; i < len; i++) {
data[i + 1] = reg_data[i];
}
rslt = nrf_drv_twi_tx(&m_twi, dev_id, data, len + 1, false);
APP_ERROR_CHECK(rslt);
NRF_LOG_INFO("Print Here");
return rslt;
}
Sorry to be so naive, but I trying to understand the basics
Thanks in advance
Nick