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

TWI. Write to registers that are not consecutive

Hello everyone. I'm creating a setup function to configure LSM9DS1 accelerometer and gyroscope by writing to the corresponding registers as shown below

uint8_t config_packet[] = {CTRL_REG1_G, 0b00101000,  // Setting ODR = 14.9 Hz | Full-scale = 500 dps | BW = default
                           CTRL_REG9,   0b00000010,  // FIFO enable without threshold limit
                           FIFO_CTRL,   0b00100000}; // FIFO mode enable

nrfx_twi_xfer_desc_t lsm9ds_config_desc = NRFX_TWI_XFER_DESC_TX(LSM9DS1_AG_ADDR, NULL, 2); //Create descriptor
nrfx_err_t err_code;

for(int i = 0; i < 5; i += 2){

    lsm9ds_config_desc.p_primary_buf = &config_packet[i]; //Select register to configure
    err_code = nrfx_twi_xfer(&m_twi, &lsm9ds_config_desc, 0); //Send configurations 
    nrf_delay_ms(1);
//    while(err_code != NRFX_SUCCESS);
}

Although this code works, I was wondering if there is a better way to do this by using the TXTX function or something similar so I can get rid of the for loop and the delay. I know that if the configuration registers were consecutive, I would only need to specify the first one sin this sensor increments the register address after transmission but this is not the case.

Either way, I want to get rid of the delay by all means. I thought about getting idle in a while loop when error code in not success since error code was error busy but it didn't work. I'm not quite sure what indicator I need to consider in the event handler, so far it only prints the error returned.

Thank you in advance,

Adiran G

  • Hi,

    You can use the TXTX macro to setup two consecutive transfers. If you want to do more transfers than this, you could use the advanced driver features, as described in the driver documentation, given that you switch to using the TWIM peripheral/driver. However, if this is a configuration sequence that is not timing-critical and the main point is to remove the for-loop, I think this will only overcomplicate your application. You will need to use PPI to trigger the transfers and a timer in count mode to stop the transfers when they are completed.

    Best regards,
    Jørgen

Related