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

app_twi gets stuck in while(internal_transaction_in_progress)

Platform: Keil, SDK13.1, nRF52832

I'm investigating series of seemingly random hangs when app_twi gets permanently stuck in app_twi.c following code snippet:

Fullscreen
1
2
3
4
5
6
7
while (p_app_twi->p_app_twi_cb->internal_transaction_in_progress)
{
if (user_function)
{
user_function();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

When this happens SCL line is permanently pulled low. I thought that it may be due to clock stretching and checked if one of slave devices is misbehaving. I wasn't able to to find anything on slave side. 

One of my slave devices (Atmel Tiny MCU) has a way to tell if it's holding SCL line. While similar lock happened I was able to check that SCL was held by master (as far as you can trust ATTiny register values). I was also able to reset the slave and this haven't released SCL. However resetting nRF52 (master) caused SCL release.

So I think that's something wrong with app_twi master implemetation.

This code snippet can reliably cause symptoms I've described above:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "nrf_log.h"
#include "app_twi.h"
#include "sm_pin_mapper.h"
//TWI instance handle
APP_TWI_DEF(m_app_twi, APP_TWI_QUEUE_SIZE, 0);
//TWI slave address
#define TWI_SLAVE_ADDR 0x1D
//how many bytes to read from slave
#define TWI_SLAVE_REG_LEN 7
static ret_code_t twi_init(void)
{
ret_code_t err_code;
nrf_drv_twi_config_t twi_config=NRF_DRV_TWI_DEFAULT_CONFIG;
twi_config.sda=SM_PIN_TWI_SDA;
twi_config.scl=SM_PIN_TWI_SCL;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

test_case(1) works as expected while test_case(0) reliably causes app_twi to get stuck as on attached Saleae trace /cfs-file/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-010a9164018145d884c8f96862005e33/twi_5F00_hang.logicdata and register view screenshot

https://devzone.nordicsemi.com/resized-image/__size/1920x1080/__key/support-attachments/beef5d1b77644c448dabff31668f3a47-010a9164018145d884c8f96862005e33/twi_5F00_hang.png 

While this happens SCL is permanently pulled low. Resetting nRF52 gets communication working again.

This is just an example. Hangs I'm experiencing happen in various configurations of TWI read/write operations. 

Am I doing something wrong?

What can be done to debug this?

I'm observing similar problems in devices already deployed in the field and I'd appreciate pointer towards solving this.