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

Can we connect two I2C(TWI) masters at same time to nrf52?

I am specific about master as I have to connect two simultaneously(obviously to two different pins pair). Plz provide any code outline if you have, Thank you in Advance

  • Why do you need to connect the devices to different pin pairs? The TWI bus can interface multiple devices using addresses.

  • I want them(two i2c masters) to work simultaneously. As I need a higher data transfer rate

  • Hi,

    Given that you use SDK version 12.0.0 or higher, the following twi_init should work:

    /* TWI PIN numbers. */
    #define SCL_PIN_1			  26
    #define SDA_PIN_1			  27
    #define SCL_PIN_2			  28
    #define SDA_PIN_2			  29
    
    /* TWI instance ID. */
    #define TWI_INSTANCE_ID_1     0
    #define TWI_INSTANCE_ID_2     1
    
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi_1 = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID_1);
    static const nrf_drv_twi_t m_twi_1 = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID_2);
    
    /**
     * @brief TWI initialization.
     */
    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_1_config = {
           .scl                = SCL_PIN_1,
           .sda                = SDA_PIN_1,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    	
    	const nrf_drv_twi_config_t twi_2_config = {
           .scl                = SCL_PIN_2,
           .sda                = SDA_PIN_2,
           .frequency          = NRF_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi_1, &twi_1_config, twi_handler_1, NULL);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_drv_twi_init(&m_twi_2, &twi_2_config, twi_handler_2, NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi_1);
        nrf_drv_twi_enable(&m_twi_2);
    }
    

    Note that you need to enable two TWI interfaces in sdk_config.h:

    #define TWI0_ENABLED 1
    #define TWI1_ENABLED 1
    

    Best regards,

    Jørgen

Related