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

nfr52832 TWI scanner doesn't work properly when softdevice is flashed into the chip

Hi,

I tried to use the exactly same code in the TWI scanner example for nrf52832, SDK15.3. Everything works fine when I flashed only the example code, which has no softdevice needed. It prints out the 22 chip addresses on my board on the RTT console. But when I tried to integrate the example code into my own project, I got only three, and the addresses printed on console are wrong ones. Here is the code, everything is the same:

void u_i2c_scanner(void) {
  ret_code_t err_code;
  uint8_t address;
  uint8_t sample_data;
  
  for (address = 1; address <= TWI_ADDRESSES; address++)
    {
        err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
        if (err_code == NRF_SUCCESS)
        {
            //detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
        }
        NRF_LOG_FLUSH();
    }
}

And one thing that is different with the example code is the twi_init(). Instead of using NULL as the call_back function, I have my own one:

void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event->type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
            {
                //data_handler(m_sample_arr); //do nothing
            }
            m_xfer_done = true;
            break;
        default:
            break;
    }
}

void u_twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

Parents Reply Children
  • Hi Kenneth,

    twi_handler() keeps giving me trouble. I modified the code as following, but the app freezes.

    void u_i2c_scanner(void) {
      ret_code_t err_code;
      uint8_t address;
      uint8_t sample_data;
      
      for (address = 1; address <= TWI_ADDRESSES; address++)
        {
            m_xfer_done = false;
            err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
            APP_ERROR_CHECK(err_code);
            while (m_xfer_done == false); //wait for the handler to be done
    
            if (err_code == NRF_SUCCESS)
            {
                //detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
            }
            NRF_LOG_FLUSH();
        }
    }

    It seems that the m_xfer_done was never set in the handler(), because if I comment out while (m_xfer_done == false), it will print out all the addresses, i.e., 1-127. But I just have 22 sensors on board.

  • Have in mind that m_xfer_done should be cleared for any event in twi_handler().

    Kenneth

Related