0

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

ndarkness gravatar image

asked 2017-12-07 13:38:03 +0100

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?

edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
0
joh2 gravatar image

answered 2017-12-08 16:39:58 +0100

updated 2017-12-08 16:40:40 +0100

Hi,

You are using different transfer types in the two cases. In case A, you set the type to NRF_DRV_TWI_XFER_TXTX, which by default will generate a stop condition and a repeated start between the two transfers. If your slave device expect both register address and data to be received before the stop condition, the first case will not write the data to the register. If case B works, this is the way to do your transfers. Please check the datasheet of the slave for more details on how to setup the correct TWI/I2C transfer.

Best regards,

Jørgen

edit flag offensive delete publish link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer. Do not ask a new question or reply to an answer here.

[hide preview]

User menu

    or sign up

Recent questions

Question Tools

1 follower

Stats

Asked: 2017-12-07 13:38:03 +0100

Seen: 41 times

Last updated: des. 08