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

Using twi vs app_twi

I am trying to set up a humidity sensor over I2C and I'm trying to use the twi interface. There are examples where the app_twi interface is used. I am however unable to get the sensor to respond when using the app_twi interface but can talk to it using the basic twi commands such as nrf_drv_twi_tx. I think the problem might be that the read command implemented as : 

#define MMA7660_READ(p_reg_addr, p_buffer, byte_cnt) \
    APP_TWI_WRITE(MMA7660_ADDR, p_reg_addr, 1,        APP_TWI_NO_STOP), \
    APP_TWI_READ (MMA7660_ADDR, p_buffer,   byte_cnt, 0)

doesn't have a wait between write and read(to account for conversion time and my sensor send a NACK). This is my guess as using the following code makes my sensor work:

        nrf_drv_twi_tx(&m_twi, device_address, dummy_data, sizeof(dummy_data), false);
        nrf_delay_ms(20);

        dummy_data[0] = 0x00;
        nrf_drv_twi_tx(&m_twi, device_address, dummy_data, 1, false);
        nrf_delay_ms(20);

        nrf_drv_twi_rx(&m_twi, device_address, m_sample, sizeof(m_sample));
        nrf_delay_ms(20);

Can you tell me if adding a delay is possible in the macro(if that indeed might be the problem)? How else can I debug this issue in app_twi?

Parents
  • Hi,

    It could also be that your sensor doesn't handle the no-stop condition (aka repeated start condition between TX and RX) used in your app_twi code. By using your first piece of code there won't be a stop bit between the write and read operation (APP_TWI_NO_STOP). In your second piece of code there will be a stop bit after all transfers (nrf_drv_twi_tx(..,..,..,false))

    Unfortunately you can't add delays using the app_twi macros. But what happens if you define APP_TWI_NO_STOP to 0 instead of 0x01?

Reply
  • Hi,

    It could also be that your sensor doesn't handle the no-stop condition (aka repeated start condition between TX and RX) used in your app_twi code. By using your first piece of code there won't be a stop bit between the write and read operation (APP_TWI_NO_STOP). In your second piece of code there will be a stop bit after all transfers (nrf_drv_twi_tx(..,..,..,false))

    Unfortunately you can't add delays using the app_twi macros. But what happens if you define APP_TWI_NO_STOP to 0 instead of 0x01?

Children
Related