Port Arduino I2C code with nrf TWI Driver

fenogy gravatar image

asked 2017-04-18 10:51:38 +0100

updated 2017-04-18 10:54:46 +0100

I am in the process of porting arduino library VL53LX01 to the nrf51822 using nrf_drv_twi functions. I will use this with S130 and thinking of using blocking mode.

I initialized without giving interrupt handler and hope it will run on blocking mode.

err_code = nrf_drv_twi_init(&m_twi, &twi_Vl530x_config, NULL, NULL);

I am thinking of using

err_code = nrf_drv_twi_tx(&m_twi, address, val, 1, false);

for the below code block in Arduino library to write.

  last_status = Wire.endTransmission();


ret_code_t err_code = nrf_drv_twi_rx(&m_twi, address, &value, 1);

for read byte instead the below code segment

  Wire.requestFrom(address, (uint8_t)1);
  value = Wire.read();

Anyone have any idea whether its correct to do it this way?

Is there any effect on using blocking mode for s130 functions ?

Pls help

edit retag flag offensive close delete report spam

1 answer

Sort by » oldest newest most voted
joh2 gravatar image

answered 2017-04-19 13:47:48 +0100


In the first call to nrf_drv_twi_tx, val should be an array containing both reg and value, like this:

uint8_t val[2] = {reg,value};

The fourth parameter should be the size the array, i.e., 2, or sizeof(val). Other than that, your code looks correct.

With regards to the softdevice, it should not matter if you use blocking or non-blocking mode. The softdevice will always have higher priority than the SPI transfer anyway. However, using blocking mode will will keep the CPU from doing other work while the transfer is performed, and usine more power than if the device was put in sleep mode.

Best regards,


edit flag offensive delete publish link more


Thanks Jorgan. Appreciate your help

fenogy ( 2017-04-20 10:54:45 +0100 )editconvert to answer

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]

Question Tools

1 follower


Asked: 2017-04-18 10:51:38 +0100

Seen: 218 times

Last updated: april 19