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

nrf_drv_twi_rx does not deliver the data seen at SDA

Hello, I have a strange behaviour in my project and can't find the cause: I have connected a TMP116 temperature sensor using TWI with SDA at P0.03 and SCL at P0.04 using internal Pull-Ups, 100 kHz clock frequency. At my oscilloscope at the SDA pin I see that the sensor delivers correct values; only nrf_drv_twi_rx() delivers all the time 0x00 and I don't know why...

The configuration is done by:

void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_tmp116_config = {
       .scl                = TWI_SCL_PORT, // P0.04
       .sda                = TWI_SDA_PORT, // P0.03
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_tmp116_config, twi_handler, NULL);

    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

Actually every 2 seconds the function read_sensor_data() is called:

static void read_sensor_data()
{
    int16_t l_i16HexWert;
    uint8_t rx_buf[4] = {0};
    ret_code_t err_code;
    m_xfer_done = false;

    /* Writing to pointer byte. */
	err_code = nrf_drv_twi_tx(&m_twi, TMP116_ADDR, &reg_Temp[0], 1, false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
	err_code = nrf_drv_twi_rx(&m_twi, TMP116_ADDR, &rx_buf[0], 2);
    APP_ERROR_CHECK(err_code);
/*    while (m_xfer_done == false);  */

// for additional debugging info
    ui32TWI_Enable = *(uint32_t *) 0x40003500UL; // Read TWI Enable
    ui32TWI_PSELSCL = *(uint32_t *) 0x40003508UL; // Read PSELSCL
    ui32TWI_PSELSDA = *(uint32_t *) 0x4000350CUL; // Read PSELSDA
    ui32GPIO_IN = *(uint32_t *) 0x50000510UL; // Read GPIO
    ui32GPIO_DIR = *(uint32_t *) 0x50000514UL; // Read GPIO
    ui32GPIO_CONFPORT3 = *(uint32_t *) 0x5000070CUL; // Read GPIO
    ui32GPIO_CONFPORT4 = *(uint32_t *) 0x50000710UL; // Read GPIO
    ui32TWI_ERRORSRC =  *(uint32_t *) 0x400034C4UL; // Read TWI ErrorSrc Register

    // calculate temperature
    l_i16HexWert = (int16_t)rx_buf[0];
    l_i16HexWert = l_i16HexWert << 8;
    l_i16HexWert += (int16_t)rx_buf[1];
    m_sample = (uint8_t)((int16_t) (l_i16HexWert >> 7));
}

When I check the registers I see that the

TWI_ENABLE register contains the value 5 -> ok
PSELSCL register contains the value 4 -> ok
PSELSDA register contains the value 3 -> ok
GPIO_IN register contains 0x201418, by this a '1' at the bit positions 3 and 4 -> probably ok with pull-ups
GPIO_DIR register contains 0x100000, by this a '0' at the bit positions 3 and 4 -> probably ok as inputs
configuration registers of Port 0.03 and 0.04 contain 0x060c -> S0D1 (standard for wired and connections), with pull-ups, input function, connect input buffer
TWI ERRORSRC: 0x00

so everything should be fine...

Does anybody have an idea?

Related