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

Why there is no TWI NACK for wrong Slave address

Hello,

I am using nRF52832, SDK_15.3.0, S132 SoftDevice and Segger for flashing the image.

To understand TWI better, I loaded 'twi_sensor' example as is on nRF52832 development kit where there is no LM75B sensor.

As per below link, if there is a wrong slave address, there will be NACK.

https://blog.digilentinc.com/i2c-how-does-it-work/

But I did not receive any interrupt in twi_handler() of event type NRF_DRV_TWI_EVT_ADDRESS_NACK or NRF_DRV_TWI_EVT_DATA_NACK. When will these occur actually.

Because of this, code struck in while loop "while (m_xfer_done == false);"

Thanks & Regards

Vishnu Beema

  • Hi,

    My suggestion for debugging: First make sure to run the twi_scanner example (without any modifications) to identify the correct address, wrong addresses will be nacked yes. Also make sure that you have pull-up resistors on the twi pins.

    Best regards,
    Kenneth

  • Hello,

    Initially I used 'twi_scanner' and got the address of the slave device. But to debug for NACK, I kept debug print for else condition.

        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);
            }
            else
            {
                NRF_LOG_INFO("error %d", err_code);
            }
            NRF_LOG_FLUSH();
        }

    I am getting 33281. I am not sure what is this.

    <info> app: error 33281
    <info> app: error 33281
    <info> app: TWI device detected at address 0x40.
    <info> app: error 33281
    <info> app: error 33281
    <info> app: error 33281
    <info> app: error 33281

    Then to crosscheck I loaded 'twi_sensor'. Since there is no device with slave address 0x90, I expected NRF_DRV_TWI_EVT_ADDRESS_NACK or NRF_DRV_TWI_EVT_DATA_NACK as part of twi_handler(). But none occurred.

    The reason behind all these is to understand when NACK will trigger and once triggered how to handle.

    Thanks & Regards

    Vishnu Beema

  • Hi,

    Error code 33281 = 0x8201 -> NRF_ERROR_DRV_TWI_ERR_ANACK ( from sdk_errors.h).

    I am not sure why twi_sensor don't throw any errors, is it using the same GPIO's as twi_scanner and is there external pull ups? Having a logic analyzer (e.g. saleae) to look at the actual twi activity may be helpful here, then it's possible to decode the twi transfer.

    It may also be related to that twi_scanner use nrf_drv_twi_rx() and twi_sensor use nrf_drv_twi_tx(), though I would expect both to throw error or success.

    Best regards,
    Kenneth

  • Hello,

    I got the root cause. I added debug print under 'default' case. For wrong address I am getting NRF_DRV_TWI_EVT_ADDRESS_NACK. But this is not printing on terminal because of "while (m_xfer_done == false);"

    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);
                }
                m_xfer_done = true;
                break;
            default:
             NRF_LOG_INFO("twi_default %d", p_event->type);
                break;
        }
    }

    Thanks & Regards

    Vishnu Beema

Related