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);
}