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…

Parents Reply
  • Hello,

    I tried to use the PRS library. I ported my entire peripheral chip handler to the new API, so that one just have to set the NRFX_PRS_ENABLED flag for the nrfx_prs_acquire to be called under the hood by nrfx_twi_init. I am using SDK 15,0,0.

    It does not work for me, the call back passed to nrfx_twi_init is never called, and after the first call to nrfx_twi_xfer my OS task get stuck…

Children
  • Have you set NRFX_PRS_BOX_X_ENABLED ? 

    Another option is to uninitialize the TWI driver before initializing the SPI driver. If the both use the same instance number (0-2) they will in fact share the same peripheral and their drivers will not initialize unless the peripheral unless no other driver has claimed the instance. I believe this is what the PRS library does, only with semaphores and atomic operations. BOX 0 refers to SPIM0, SPIS0, TWIM0, TWIS0, SPI0, and TWI0. 




  • Hello,

    Yes the NRF_PRS_BOX_0_ENABLED is set. This was anyway a good point to mention, as in the first place I did the mistake of not setting it. I have made a suggestion to improve the sdk_validation.h to that respect.

    I think I have made some progress in understanding what happens.

    First of all the twi_irq_handler is not called, I have tried with a debugger and a JLink, with putting a breakpoint on this function.

    I also checked that p_cb->handler and p_cb->context are correctly set to my callback function (but anyway, that does not help as the irq handler is not called).

    Then, I saw that when nrfx_twi_xfer is called by my FreeRTOS task, it calls in turn twi_xfer, which in turn calls twi_tx_start_transfer. In twi_tx_start_transfer, p_cb->int_mask is set to 0x286 which should be NRF_TWI_INT_STOPPED_MASK | NRF_TWI_INT_ERROR_MASK | NRF_TWI_INT_TXDSENT_MASK | NRF_TWI_INT_RXDREADY_MASK, and nrf_twi_int_enable(p_twi, p_cb->int_mask) is called.

    However, I noticed that this call occurs after the call to (void)twi_send_byte(p_twi, p_data, length, &p_cb->bytes_transferred, no_stop). Hopefully it is not too late, and the interrupt is still pending.

    Well, if there is any experiment (like reading some memory) you would like me to do, please feel free to ask !

  • Just as a side comment, I am rather confident in the way that the TWI is configured, because I first did the experiment with two different peripherals, TWI1 and SPI0 and the legacy API. And in this case the TWI transfer works OK.

    My problem occurred after porting to the new API, and trying to use the PRS stuff to share TWI0 + SPI0. 

  • I've reported your issue to the SDK developers, but It will take some time before they can look into it. We are nevertheless very greatful for your report. 

  • The only two reasons I can think of is that the interrupts are disabled or the CPU is stuck in a higher priority ISR. The TWI peripheral should fire the ERROR event if any operation outside the spec is occuring. 

    Do you see any strange behavior on the TWI lines?

    What is the state of the TWI0 registers before the STARTRX or TX  is triggered?

    Have you tried using the TWIM/TWIS drivers? As they use EasyDMA and PPI shortcuts they require less CPU resources, whom I assume you are in dire need of if your running an RTOS on top of the SoftDevice. 

Related