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

Port Arduino I2C code with nrf TWI Driver

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.

  Wire.beginTransmission(address);
  Wire.write(reg);
  Wire.write(value);
  last_status = Wire.endTransmission();

Similary

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

Parents
  • Hi,

    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,

    Jørgen

Reply
  • Hi,

    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,

    Jørgen

Children
Related