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

  • I see. There is one important point I forgot to ask about earlier. P0.22 is connected to the onboard

    Thank You. Everything is working fine now.

    What did I change ?
    I used some other digital pins for 2nd I2C instance. Other than (P1.00, P0.22).
    I also set TWI0_ENABLED and TWI1_ENABLED to 1.

    Maybe the problem was due to P0.22 being connected to external flash. 

    This is link to the source code I used. If anyone is trying to solve this problem in future.
    In this source code I have merged code of NFC Adafruit Tag Example (from SDK example) and OLED (Github Link to library). NFC Reader and OLED are working on two different I2C pins.

    I also tested on some other digital pins and some pins don't seem to work on nRF52840 DK. Maybe this is due to them being connected to some other things internally.

    This is my finding.
    *********TESTED PINS FOR OLED*************
    SCL    SDA    Result
    0.07    0.08    True (Working)
    1.09    0.08    True
    0.09    0.1     False
    0.04    0.05   True

Related