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

TWI0 + TWI1 + SPIx

Hey Guys,

In my custom board I need to use two different I2C bus and one SPI.

At the moment i'm using twi_master to comunicate with one of the TWI bus, but to switch TWI the only option I've seen is to change:

TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (13U)

TWI_MASTER_CONFIG_DATA_PIN_NUMBER (14U)

how can i configure two istances of the twi_master?

Furthermore I need to use an SPI beside these the TWI, what SPI should I be using? Is it the SPI2 due to the concurrent use of TWI0 and TWI1?

I'm alittle bit confused.

Thanks.

  • For a start what chip are you using, nRF51 or nRF52? The 51 doesn't have an SPI2 so I'm assuming nRF52.

    Those TWI_MASTER_.. defines are only found in the old deprecated driver for TWI which only uses TWI1 (look at the source code). If you want to use more than one TWI, then use the recent driver in the recent SDKs. Actually use it anyway and ignore anything in 'deprecated' directories.

    Why do you need two TWIs anyway? It's a bus, it allows multiple slaves on it with different slave addresses, that's kind of the whole point of TWI.

  • To use two independent TWI instances, you probably need to implement your own twi master driver (it's easy, can be based on the original one). There are just few things to change like pins (obviously) and shorts that are used (not obvious for me at first :) ). Maybe something more, I don't remember. But like RK said, it is better to use one TWI instance to communicate with more devices - in my situation it was needed because of not expandable hardware design prototype.

    If you are using nRF51 and need both SPI and TWI, you have to stick to only one instance of TWI or switch between TWI/SPI during runtime.

  • Thanks RK. I'm using nRF52 with SDK 0.9.2. I've seen that it was using TWI1. I need two TWI because i want to semparate sensors on the two busses. Anyway, you are referring to something like this? // Initate and configure TWI instance const nrf_drv_twi_t p_twi_instance = NRF_DRV_TWI_INSTANCE(1); nrf_drv_twi_config_t p_twi_config = NRF_DRV_TWI_DEFAULT_CONFIG(1); p_twi_config.scl = 1; p_twi_config.sda = 2;

    // Initate TWI module using event handler. 
    ret_code = nrf_drv_twi_init(&p_twi_instance, &p_twi_config, twi_event_handler); // Initiate twi driver with instance and configuration values
    APP_ERROR_CHECK(ret_code); // Check for errors in return value
    nrf_drv_twi_enable(&p_twi_instance); // Enable the TWI instance
    

    right? Then I need to go on SPI2 or not?

Related