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

nrf52810: TWI interfacing with SHTW2 Humidity/Temperature Sensor

Hi,

I am using a custom board with the nrf52810 and trying to interface with the SHTW2 sensor. The sensor has an I2C interface and hence I use the TWI peripheral for this. However, the transfer of data does not take place. Below is my read function:

void SHTW2_read(){
    uint8_t m_sample[2];
    uint8_t writeMode[] = {0x70};
    uint8_t readMode[] = {0x71};
    uint8_t reg[2] = {0x78, 0x66};
    ret_code_t err_code;
    //while (m_xfer_done == false);
              
    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, writeMode, sizeof(writeMode), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    nrf_delay_ms(5);

    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, reg, sizeof(reg), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    nrf_delay_ms(5);

    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, readMode, sizeof(readMode), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    m_xfer_done = false;

    err_code = nrf_drv_twi_rx(&m_twi, SHTW2_ADDR, m_sample, sizeof(m_sample));
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    temperature = m_sample[0] << 8 | m_sample[1];
}

I am using the TWI sensor example as base for this, but the code gets stuck at the while loop waiting for the handler to be called where the flag m_xfer_done is set. The handler never gets called. Can anyone point me as to where I am going wrong? May be my fundamental understanding of TWI is wrong. Thanks for the help in advance.

Parents
  • Hi,

     

    In which context are you calling the function SHTW2_read() ? If this is called from an interrupt context, your TWI handler, which sets the m_xfer_done = true, must be a higher priority.

    Since it looks like you are using the TWI driver in a block fashion, you can just omit the handler, like this:

    err_code = nrf_drv_twi_init(&m_my_twi_instance, &my_twi_config, NULL, NULL);

     

    This way, each call will only return when the bus itself it done.

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    In which context are you calling the function SHTW2_read() ? If this is called from an interrupt context, your TWI handler, which sets the m_xfer_done = true, must be a higher priority.

    Since it looks like you are using the TWI driver in a block fashion, you can just omit the handler, like this:

    err_code = nrf_drv_twi_init(&m_my_twi_instance, &my_twi_config, NULL, NULL);

     

    This way, each call will only return when the bus itself it done.

     

    Kind regards,

    Håkon

Children
Related