I am trying to use I2C (TWI) to communicate between my custom nRF52811 board with an Arduino. Currently my program hangs at the while (m_xfer_done == false); loop as m_xfer_done never gets set to true. I believe that the nrf chip gets stuck at the end of the transmission since the Arduino actually receives the message.
I based my code heavily on the twi_sensor example provided by Nordic and kept it very bare bone. It is using My Arduino is set as slave on address 0x08 and I am writing 0x016 (which gets received on the arduino side). The twi_init is also set as non-blocking.
Below is a snipet of my code that I believe are relevant to the problem.
// Arduino I2C test
#define ARDUI_ADDR 0x08U
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
m_xfer_done = true;
NRF_LOG_INFO("Handling....");
NRF_LOG_FLUSH();
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);
}
m_xfer_done = true;
break;
default:
break;
}
}
/**
* @brief UART initialization.
*/
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_lm75b_config = {
.scl = 16,
.sda = 15,
.frequency = NRF_DRV_TWI_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.clear_bus_init = false
};
err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi);
}
void test_i2c(void)
{
ret_code_t err_code;
uint8_t reg[1] = {0x016U};
err_code = nrf_drv_twi_tx(&m_twi, ARDUI_ADDR, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);
}
/**
* @brief Function for main application entry.
*/
int main(void)
{
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("\r\nTWI sensor example started.");
NRF_LOG_FLUSH();
twi_init();
while (true)
{
nrf_delay_ms(500);
test_i2c();
do
{
__WFE();
}while (m_xfer_done == false);
NRF_LOG_FLUSH();
}
}
/** @} */
Furthermore I added 1K pullup resistors for both communication line just in case. I also dont have any oscilloscope nor logic analyzer...so I cant provide any pictures of the actual signal.
Any help would be greatly appreciated!