STS30 temperature sensor I2C code

I am trying to read temperature value from STS30 temperature sensor using this simple code snippet: 

During debugging, my debugger stuck at line "if (STS30_read_temp(temperature)==true)". When I step into this line, I can see debugger is stuck at line "return result" of function "nrf_drv_twi_tx" in nrf_drv_twi.h file. 

Can you check the code and advise what am I doing wrong here. 

I have attached the command sequence for reading temperature from STS30 sensor. 

  

Thanks

  • Without a breakpoint, nothing different happens from the case I do not have APP_ERROR_CHECK. Debugger gets stuck in the while loop. 

  • I thought the address of this sensor was 0b0011xxxr where xxx is the state of the external pins A2/A1/A0. Try this (assuming all 3 address pins are tied low):

        uint8_t STS30_Address = 0x18; // address of the sensor

  • There is just 1 address pin on this device. Which if grounded gives address of 0x4A. I have already been able to successfully scan for STS30 on I2C address 0x4A. 

    I think the problem is to find if this code below is sending the Address/w/Ack/0x2C/Ack/0x10/Ack/Stop sequence as sensor expects to initiate a measurement? 

    tx_buf[0] = 0x2C;
    tx_buf[1] = 0x10;

    err_code = nrf_drv_twi_tx(&m_twi, STS30_Address, tx_buf, 2, false);

  • It seems the originally posted code works when I remove "while (m_xfer_done == false){}" lines in STS30_read_command() and STS30_write_command() functions. 

    Do I really need twi_handler() function in my code? 

  • Hello again, Kaveh.m

    Thank you for your patience with this.

    Kaveh.M said:
    I have already been able to successfully scan for STS30 on I2C address 0x4A. 
    Kaveh.M said:
    Without a breakpoint, nothing different happens from the case I do not have APP_ERROR_CHECK. Debugger gets stuck in the while loop. 

    Thank you for confirming that the addressing and hardware connections of the setup is working as expected, and that the APP_ERROR_CHECK does not trigger - this means that the function call returns NRF_SUCCESS as expected.

    Kaveh.M said:

    It seems the originally posted code works when I remove "while (m_xfer_done == false){}" lines in STS30_read_command() and STS30_write_command() functions. 

    Do I really need twi_handler() function in my code? 

    The reason for the m_xfer_done flag in the TWI peripheral examples is that the device might be doing other things in the meantime, while waiting for the transfer to complete.
    I just looked more closely through your code, and it seems that you do not provide the twi_handler function in your nrf_drv_twi_init call - this is likely the issue here.
    Since the nrf_drv_twi_init function is provided NULL as the callback function, there will be no twi events forwarded to the application layer.
    Please add twi_handler as your third argument to nrf_drv_twi_init, and see if this resolves your issue.

    Best regards,
    Karl

Related