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

Why isn't nrf_drv_twi_xfer() working configured like this?

Hello,

I am using NRF52 TWI master to read data from my sensor, I have read the twi_sensor example but it doesn't make use of nrf_drv_twi_xfer().

My question is quite straightforward, I have noticed that my settings to usenrf_drv_twi_xfer() like on Case A does not work, in that the register value written on the device is different from the one retrieved after reading the same register

Case A

int8_t twi_write_transfer(unsigned char slaveDeviceId, unsigned char reg_addr, unsigned char *txData, uint16_t bytesNumberTx)
  nrf_drv_twi_xfer_desc_t twi_WriteTransferData;

  twi_WriteTransferData.address          = slaveDeviceId;
  twi_WriteTransferData.p_primary_buf    = &reg_addr;
  twi_WriteTransferData.primary_length   = sizeof(reg_addr);
  twi_WriteTransferData.p_secondary_buf  = txData;
  twi_WriteTransferData.secondary_length = bytesNumberTx;
  twi_WriteTransferData.type             = NRF_DRV_TWI_XFER_TXTX;
  uint32_t flags = NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER;
  

  err_code = nrf_drv_twi_xfer(&twi, &twi_WriteTransferData, flags);
 APP_ERROR_CHECK(err_code);
 return err_code;

}

whereas this settings does work, Case B

Case B

int8_t twi1_write_transfer(unsigned char slaveDeviceId, unsigned char reg_addr, unsigned char *txData, uint16_t bytesNumberTx) {
  
  ret_code_t err_code;

  
  unsigned char aux[bytesNumberTx + 1];
  
  aux[0] = reg_addr;
  
  memcpy(&aux[1],txData,bytesNumberTx);

  nrf_drv_twi_xfer_desc_t twi_WriteTransferData;

  twi_WriteTransferData.address          = slaveDeviceId;
  twi_WriteTransferData.p_primary_buf    = aux;
  twi_WriteTransferData.primary_length   = sizeof(reg_addr)+bytesNumberTx;
  twi_WriteTransferData.p_secondary_buf  = NULL;
  twi_WriteTransferData.secondary_length = 0;
  twi_WriteTransferData.type             = NRF_DRV_TWI_XFER_TX;

  uint32_t flags = NRF_DRV_TWI_FLAG_NO_XFER_EVT_HANDLER;
  
err_code = nrf_drv_twi_xfer(&twi, &twi_WriteTransferData, flags);

  APP_ERROR_CHECK(err_code);
  
  return ((int8_t)err_code);
}

I am aware that the main difference is that on Case A more than one I2C transfer happens, however, I have check that with the first case the write into the device register failes, since reading the setting I tried to write returns the default value of that register on the device.

So, what am I setting wrong in case A?

Related