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

How to use both I2C pins on nRF52840 ?

Problem : 

I have two I2C devices and would like to connect them both to nRF52840 DK on different I2C pins. According to datasheet nRF52840 has two I2C pins (P1.00, P0.22) and (P0.27, 0.26). I want to connect one device on (P1.00, P0.22) and other on (P0.27, 0.26).

I am using SDK 15.3.0.

Current Code :

I have created two TWI Instance.

static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(0);
static const nrf_drv_twi_t m_twi2 = NRF_DRV_TWI_INSTANCE(1);

I have also initialized both of them.

/**
 * @brief TWI initialization.
 */
void 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_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);

    const nrf_drv_twi_config_t twi_config2 = {
       .scl                = NRF_GPIO_PIN_MAP(0,22),
       .sda                = NRF_GPIO_PIN_MAP(1,0),
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi2, &twi_config2, NULL, NULL);
    NRF_LOG_INFO("%d", err_code);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi2);
}

Further I try to scan for I2C devices on both TWI instances and fail to read on (P1.00, P0.22).

Things I have tried :

  1. Scan only one TWI instance.
    Scanning on (P0.27, P0.26) works but (P1.00, P0.22) doesn't work.
  2. Try using same NRF_DRV_TWI_INSTANCE
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(0);
    static const nrf_drv_twi_t m_twi2 = NRF_DRV_TWI_INSTANCE(0);

Some other questions I have :

  1. What is the use NRF_DRV_TWI_INSTANCE() ? What is TWI instance exactly ?
  2. What is the difference between the TWI hardware driver and TWI transaction Manager ?

Link to Source Code

Related