TWI Manager - TWI delayed after start condition

Hello,

I've used the TWI manager to implemented a burst of transfers to and from two devices on a single bus. Basically it waits for both devices to give a data-ready interrupt via GPIOTE, and then it schedules that block of transactions (36 in total). It runs reliably, but when I examine the bus, it looks like there is sometimes a large delay (several ms) between the start condition and the start of the transfer.

Here is a wide screenshot of the lines (so you can see how it generally runs smoothly) with the anomaly at the center:

Here's a zoomed in shot of the anomaly, with 2.26ms of delay shown:

This is especially problematic because after the interrupt, there is only 2.5ms before the data registers on the sensor will start to get overwritten (unfortunately there is no FIFO or SPI interface, otherwise this wouldn't be an issue).

My understanding is that using the TWI manager in scheduled/background mode is supposed to be non-blocking. It's clear that the transaction does start fairly quickly, but what else could be interfering with the operation like this? I do have a SPI bus configured as well (but it's not running currently, and it's using the SPI2 instance, and this manager is using TWI0, so there should be no interference). If it matters, I'm using SDK 17.1 and the latest S113 softdevice.

Thank you!

TWI Config below

#define I2C_MAX_PENDING_TRANSACTIONS    35
NRF_TWI_MNGR_DEF(m_nrf_twi0_mngr, I2C_MAX_PENDING_TRANSACTIONS, 0);

ret_code_t err_code;
    nrf_drv_twi_config_t config = {
            .scl                = SCL1_PIN_NUMBER,
            .sda                = SDA1_PIN_NUMBER,
            .frequency          = NRF_DRV_TWI_FREQ_400K,
            .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
            .clear_bus_init     = true
    };

    err_code = nrf_twi_mngr_init(&m_nrf_twi0_mngr, &config);
    APP_ERROR_CHECK(err_code);

  • Hi,

    Which SDK version are you using?

    Is the TWI instance configured to use EasyDMA? This is configured through the following configs in your sdk_config.h file:

    // <q> TWI0_USE_EASY_DMA  - Use EasyDMA (if present)
    #ifndef TWI0_USE_EASY_DMA
    #define TWI0_USE_EASY_DMA 0
    #endif
    
    
    // <q> TWI1_USE_EASY_DMA  - Use EasyDMA (if present)
    #ifndef TWI1_USE_EASY_DMA
    #define TWI1_USE_EASY_DMA 0
    #endif

    TWI will be more affected by other interrupts in your application, as you need to update the data pointer for each byte being sent/received. However, according to the transfer diagrams, both TWI and TWIM should send the address and send/receive at least one byte before there is any pause in transmission (TWI/TWIM).

    Which GPIOs are you using for SCL/SDA? Have you tried different pins?

    Are you seeing this behavior with other TWI/I2C slaves, or could it be the slave doing some clock stretching, etc? Would be a bit strange if this happens before the address is written to the bus.

    Can you share the full logic trace file that shows this happening?

    Best regards,
    Jørgen

  • Hi Jørgen,

    I'm using SDK 17.1 and Easy DMA is enabled in sdk_config.h (though each transfer loaded into the queue has its own static pointers to the data to send and received). Is it possible the softdevice is causing this interruption? I know that's theoretically possible, but it seems odd that it would cause an initiated TWI transfer to stall.

    I'm using pins 0.11 (SDA) and 0.12 (SCL). I could try different pins, but it would result in some fly-wires that might degrade performance.

    Regarding clock stretching, I don't have other devices to immediately test with this board, but as you mentioned it happens before the address (I find this is always the case), which seems to rule out the slave stretching the clock. The slave devices are TI FDC2214, for the record. I generally expect predicable behavior from TI chips.

    I'm attempting to attach the full Saleae trace file, but this isn't letting me upload it in this comment. I'll see if I can add it to the original post.

    [Edit]: It appears to only let me upload a CSV. The trace file is much smaller and probably easier to work with, but it won't allow me to upload it. Let me know if I can send it over somehow.

    digital.csv

    Channels 0 and 1 are SCL and SDA, respectively. Channels 2 and 3 are the interrupts that trigger the burst transfer.

    Thanks for your help,

    Dylan

  • Hi,

    The only thing that comes to mind would be that a higher priority AHB master accesses the same RAM block/AHB slave and blocks the TWIM peripheral. Just to rule this out, can you test to statically place all TWIM buffers in a RAM section not used by any other peripherals or the CPU, and see if the issue still persists? See Random access memory for details about which RAM addresses/sections are mapped to which AHB slave.

    Looks like the CSV file can't be imported in Saleae Logic. Can you try to zip the logic trace file and upload that? I belive the site should accept .zip files.

    Best regards,
    Jørgen

  • I'll give that a try and report back. In the meantime, here's the zipped logic trace.nrf52_TWI_delay_after_SC.sal.zip

    Just to clarify the static placements, do you mean the transfer buffers, the queue, every definition related to TWI, or just a subset of those?

    [Update]: As I'm looking into this, while the RAM/Memory map tells me which addresses belong to each AHB slave RAM region, I can't find anything indicating which peripherals are using which AHB slave. I can't tell if it's safer to put everything in RAM0 or RAM8. Also, what is the preferred method of specifying an address for static allocations? I tried using the syntax in the EasyDMA example (static foo[n]  __at__ 0xADDR), but it would not compile.

    [Update 2]: I was able to move the array of transfers as well as the other data used to support it into a section in AHB RAM8 (0x20018000, previously they were being mapped in the 0x20002--- range or so). The behavior is the same. I also notice that this glitch appears to be periodic, happening reliably every ~1100ms.

    [Update 3]: I have also tried disabling BLE advertising, as well as not initializing BLE to begin with, but it appears to still be happening.

    Thanks,

    Dylan

  • I've got another interesting example of this here:

    TWI0_1_delays.sal.zip

    This is running basically the same code, but on a different board with two TWI buses used. I have two TWI manager instances. On this board, the delay only seems to pop up in TWI1 (SCL: P0.11, SDA: P1.9) and does not appear in TWI0 (SCL: P0.5, SDA: P0.4). I also notice the periodicity is different.

    I don't know if this could be related, but pin 0.11 is the SDA line in the original ticket.

Related