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

Dynamic reconfiguration of peripheral HW from TWI to SPI

Our nRF52840 is communicating with a peripheral chip (let's call it FOO) through an SPI interface. The FOO chip can be configured to use either TWI or SPI on the same pins (well TWI uses a subset of the SPI pins), the default mode after FOO reset is TWI and a register TWI_DISABLE in FOO has to be configured to ensure that TWI does not risk to spoil SPI by accident.

We need SPI, because the TWI does not have sufficient throughput to use all of the features of the FOO chip.

When the FOO chip starts up, the TWI_DISABLE is not set, so FOO is in a sort of blind detection mode in which it will accept both SPI and TWI at the same time. In this blind mode, the TWI cannot be disturbed by the SPI, because the SPI needs chip select (CS) pin active, and the CS pin default setting is inactive when in TWI mode. However, the converse statement is not true : theoretically it is possible that bad luck causes that you accidentally have TWI acting while in SPI and make the SPI link fail. 

It is possible after startup of FOO to make a loop so that, while communicating in SPI, you write the TWI_DISABLE register and check it by reading it afterwards until TWI_DISABLE is really set, and then you are OK. We checked this solution and it is working fine to do so and seems to be the standard approach.

However that sounds like a dirty solution that we cannot guarantee will always in all circumstances and over millions of items work. We would like to make it really bulletproof and another solution is to start to communicating with FOO over TWI, write the TWI disable register of FOO so that the TWI is disabled in FOO, and then we are OK too for using SPI safely.

We checked this solution too with some quick and dirty test FW and it works fine.  However it is not possible to make it easily with the Nordic SDK 15 because the SDK ensures at compile time that you cannot use the same HW peripheral in TWI and in SPI. Say for instance that I want to use SPI0 to communicate with FOO, then I cannot enable TWI0 too, even though there would not be any conflict because the FW would use TWI0 only for a short time at the beginning just to set TWI_DISABLE FOO register and then be uninit'ed.

It is not possible for us to use TWI1 instead of TWI0 because all the HW ressources of nRF52840 are taken in our application, so if we go to the bulletproof solution we really need to reconfigure dynamically (at runtime, not at compile time) the same nRF52840 peripheral. The nRF52840 HW allows this, but this is a limitation of the SDK that makes it impossible. This limitation is really quite usefully and a very good idea for most of the cases because it prevents people from having HW conflicts in their design, however, in our very specific corner case it is preventing us to make it.

Your support is most welcome…

  • For completeness, here is the patch that makes it work for me (I replaced the copy to array by some delay which I empirically found):

    diff --git a/nrf-sdk/modules/nrfx/drivers/src/nrfx_twi.c b/nrf-sdk/modules/nrfx/drivers/src/nrfx_twi.c
    index c7492bd4..c9080024 100644
    --- a/nrf-sdk/modules/nrfx/drivers/src/nrfx_twi.c
    +++ b/nrf-sdk/modules/nrfx/drivers/src/nrfx_twi.c
    @@ -49,6 +49,8 @@
    #include <nrfx_twi.h>
    #include <hal/nrf_gpio.h>
    #include "prs/nrfx_prs.h"
    +#include "nrf_delay.h"
    +

    #define NRFX_LOG_MODULE TWI
    #include <nrfx_log.h>
    @@ -395,6 +397,10 @@ static nrfx_err_t twi_tx_start_transfer(twi_control_block_t * p_cb,

    // In case TWI is suspended resume its operation.
    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_RESUME);
    +
    + if(p_twi == (NRF_TWI_Type*)0x40003000)
    + nrf_delay_us(300);
    +
    nrf_twi_task_trigger(p_twi, NRF_TWI_TASK_STARTTX);

    (void)twi_send_byte(p_twi, p_data, length, &p_cb->bytes_transferred, no_stop);

    Please note that the « if(p_twi == (NRF_TWI_Type*)0x40003000) » is there just because I want to affect TWI0 only, which is the one concerned by my specific design, and minimize any side effect on the rest of the system. This patch is definitely not a fix, just some hack.

    Hopefully, you can help with this.

    BTW, as I wrote, I am currently still using sdk15,0,0, maybe this issue is already fixed in 15,0,2. 

  • Thank you for sharing your findings, we'll try to reproduce it on our end and see what we find :)

  • Dear   

    I am a bit confused to inform you that this discussion was just making a lot of noise for nothing. A few weeks ago I realized that the root cause of the problem was that the peripheral setup time (from the reset signal raising to the first access on the bus) was too short.
    For some reason this had gone unnoticed up to lately and I had sought the problem in the wrong direction and fixed it without understanding why the fix was making things work.
    At least I hope the discussion can be useful to other people in the sense that one should double check one more time that startup timings specs and other basic settings are fulfilled before blaming the SDK.

    Lots of thanks for your kind help anyway.

Related