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

TWI0 and SPI0 time multiplex

Hi there,

Is it possible to use TWI0 and SPI0 (or TWI1/SPI1) time multiplexed?

When enabling them both in sdk_config.h I get "Error: L6200E... IRQHandler multiply defined...", which can be resolved by defining PERIPHERAL_RESOURCE_SHARING_ENABLED. Is there any documentation available to find out exactly whats going on under the hood and what to be aware of?

Many thanks, Josef

  • You cannot use serveral peripheral instances that share the same IDs/base addresses at the same time, so if you want to use TWI and SPI at the same time, you should use e.g. TWI0 and SPI1. Here is the table of which IDs/base address the different peripheral instances use.

    What the peripheral resource sharing(PERIPHERAL_RESOURCE_SHARING_ENABLED) enables you to do is to first use e.g. TWI0, then you have to uninitialized it, and then you can use SPI0. Normally, this is not possible, because interrupt handlers are implemented in individual drivers. This functionality requires a more complicated interrupt handling and driver initialization, hence it is not always desirable to use it. The functionality was just recently added(SDK11), and there is not much documentation on it yet. If you want to take a look "under the hood", you can find the implementation in nrf_drv_common.c. The implementation is basically a kind of mutex, that will return NRF_ERROR_BUSY if you try to initialize several peripheral instances that use the same ID/Base Address.

  • Hi Sigurd,

    thanks for the answer, that is exactly what I was looking for. I figured that resource sharing plus un-initializing and initializing the other one would be the way forward, thanks for the confirmation. Documentation I was hoping to find was how to separate ppi and tasks from the 2, but maybe that's straight forward then. It's great that there is development going on there.

    Many thanks, Josef

  • Hi Sigurd,

    I was getting the similar issue while I am trying to use SPI and TWI at the same time. Actually I was using SPI0 and TWI0. So is was creating following error:

    duplicate definitions for "SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQHandler"
    

    Hence I changed TWI0 to TWI1. And now Its working fine.... This thread helped me for resolving the issue. Thanks..

  • Hello, what about the case where I must share SPIMO and TWIMO (ID 3, base addresss 0x40003000). I Went ahead and enabled PERIPHERAL_RESOURCE_SHARING_ENABLED, but then the SPI initialization fails with "NRF_ERROR_BUSY". Is there sample code or a white paper that can advice me what to do? Do I simply need to disable/enable TWIM0 while SPIMO is initializing? I'm not really sure how to proceed. Thank you.

  • Hi sami_cola, from the API refrence about nrf_drv_spi_init, you have that NRF_ERROR_BUSY is returned when:

    If some other peripheral with the same instance ID is already in use. This is possible only if PERIPHERAL_RESOURCE_SHARING_ENABLED is set to a value other than zero.

    You then need to uninit the other instance(e.g. TWIM0) before you can use e.g. the SPIM0

Related