I'm trying to read temperature sensor data through TWI (I2C) from SHT3x using NRF52-DK and SDK v16.0.
Observing an oscillator, the address and the return value with CLK looks correct, but the buffered read data (rx_buf[]) by nrf_drv_twi_rx is zero.
That result is not different between before and after the waiting "while ( m_xfer_done == false );"
What should I check and correct?
sdk_config.h
NRFX_TWIM_ENABLED 1
NRFX_TWI_ENABLED 1
TWI_ENABLED 1
TWI0_ENABLED 1
TWI0_USE_EASY_DMA 1
static volatile bool m_xfer_done = false; static uint8_t rx_buf[6] = {0}; static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID); void twi_init (void) { ret_code_t err_code; /** TWI driver configuration */ const nrf_drv_twi_config_t twi_config = { .scl = TWI_SCL_PIN, .sda = TWI_SDA_PIN, .frequency = NRF_DRV_TWI_FREQ_100K, .interrupt_priority = APP_IRQ_PRIORITY_HIGH, .clear_bus_init = false }; /** Initializing TWI Driver. */ err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL); NRF_LOG_INFO(" -- nrf_drv_twi_init(): Return %d.", err_code ); APP_ERROR_CHECK(err_code); nrf_drv_twi_enable(&m_twi); } void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context) { switch (p_event->type) { case NRF_DRV_TWI_EVT_DONE: m_xfer_done = true; break; default: break; } } void SHT3x_readout(void) { ret_code_t err_code; uint8_t reg[2] = {0}; // Measurement Command nrf_delay_ms(100); reg[0] = 0x24; reg[1] = 0x00; m_xfer_done = false; err_code = nrf_drv_twi_tx( &m_twi, 0x45, reg, 2, false ); NRF_LOG_INFO(" -- Function set: Return %d, m_xfer_done %d.", err_code, (uint16_t)m_xfer_done ); APP_ERROR_CHECK( err_code ); while ( m_xfer_done == false ); // Readout of Measurement Results nrf_delay_ms(100); memset(rx_buf, 0, sizeof(rx_buf)); err_code = nrf_drv_twi_rx( &m_twi, 0x45, rx_buf, 6 ); APP_ERROR_CHECK( err_code ); NRF_LOG_INFO(" -- Function read: Return %d.", err_code ); NRF_LOG_INFO(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x.", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3], rx_buf[4], rx_buf[5] ); while ( m_xfer_done == false ); NRF_LOG_INFO(" -- Function read: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x.", rx_buf[0], rx_buf[1], rx_buf[2], rx_buf[3], rx_buf[4], rx_buf[5] ); }
Results on UART:
app: -- nrf_drv_twi_init(): Return 0.
app: -- Function set: Return 0, m_xfer_done 0.
app: -- Function read: Return 0.
app: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00.
app: -- Function read: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00.