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
  • I think this should be doable with the SDK's Peripheral Resource Sharing (PRS) module. If I have understood correctly the main purpose of the PRS module is to allow you to use shared resources like this.

  • 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…

  • 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. 

  • Hello,

    Thank you vey much for the feedback, I will try to answer point by point.

    Do you see any strange behavior on the TWI lines?

    Well, I cannot say this now, I need to make some setup so that I can observes the line on the scope, I am afraid I don't have easy easy test point to get them right away.

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

    The first function that is called is nrfx_twi_xfer, when I am at line 601 of file nrfx_twi.c here is what I can print in gdb:

    nrfx_twi_xfer (p_instance=0x1d210 <m_FOO_twi_transient_i2c>, p_xfer_desc=0x200041cc, flags=0)
    at /Users/vincentbelaiche/Projects/biosency-fw_dev3.4/boraconnect-firmware/nrf-sdk/modules/nrfx/drivers/src/nrfx_twi.c:583
    583 nrfx_err_t err_code = NRFX_SUCCESS;
    (gdb) until
    until
    584 twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
    (gdb) until
    until
    601 err_code = twi_xfer(p_cb, (NRF_TWI_Type *)p_instance->p_twi, p_xfer_desc, flags);
    (gdb) print *p_cb
    print *p_cb
    $1 = {handler = 0x7461 <FOO_transient_twi_cb>, p_context = 0x20002790 <m_FOO_context_s>, int_mask = 0, xfer_desc = {
    type = NRFX_TWI_XFER_TX, address = 0 '\000', primary_length = 0, secondary_length = 0, p_primary_buf = 0x0 <__isr_vector>,
    p_secondary_buf = 0x0 <__isr_vector>}, flags = 0, p_curr_buf = 0x0 <__isr_vector>, curr_length = 0 '\000',
    curr_no_stop = false, state = NRFX_DRV_STATE_POWERED_ON, error = false, busy = false, repeated = false,
    bytes_transferred = 0 '\000', hold_bus_uninit = true}
    (gdb) print /x *((NRF_TWI_Type *)p_instance->p_twi)
    print /x *((NRF_TWI_Type *)p_instance->p_twi)
    $3 = {TASKS_STARTRX = 0x0, RESERVED0 = 0x0, TASKS_STARTTX = 0x0, RESERVED1 = {0x0, 0x0}, TASKS_STOP = 0x0, RESERVED2 = 0x0,
    TASKS_SUSPEND = 0x0, TASKS_RESUME = 0x0, RESERVED3 = {0x0 <repeats 56 times>}, EVENTS_STOPPED = 0x0, EVENTS_RXDREADY = 0x0,
    RESERVED4 = {0x0, 0x0, 0x0, 0x0}, EVENTS_TXDSENT = 0x0, RESERVED5 = 0x0, EVENTS_ERROR = 0x0, RESERVED6 = {0x0, 0x0, 0x0, 0x0},
    EVENTS_BB = 0x0, RESERVED7 = {0x0, 0x0, 0x0}, EVENTS_SUSPENDED = 0x0, RESERVED8 = {0x0 <repeats 45 times>}, SHORTS = 0x0,
    RESERVED9 = {0x0 <repeats 64 times>}, INTENSET = 0x0, INTENCLR = 0x0, RESERVED10 = {0x0 <repeats 61 times>, 0x1,
    0x0 <repeats 48 times>}, ERRORSRC = 0x0, RESERVED11 = {0x0 <repeats 14 times>}, ENABLE = 0x5, RESERVED12 = 0x0, PSEL = {
    SCL = 0x1b, SDA = 0x8}, RESERVED13 = {0xffffffff, 0xffffffff}, RXD = 0x0, TXD = 0x0, RESERVED14 = 0x0, FREQUENCY = 0x6680000,
    RESERVED15 = {0x0 <repeats 12 times>, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0}, ADDRESS = 0x0}
    (gdb)

    Please note that in the above I have renamed some of my symbols with …_FOO_… to be consistent with the beginning of this discussion.

    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. 

    No, I have not tried that yet, though it is a good idea certainly.

    The FW which I am using is not the full application FW, it is a test FW with only the modules at stake. There is no SoftDevice in it, but the RTOS is there.

    I must admit that the NRF_LOGGER is used and may take some resource, however I don't think that it is the root cause because for some other module with TWI interfacing in a similar way and quite intensive logging there is no problem. 

  • To be more complete, I also printed the transfer descriptor, it is as follows (I just obfuscated the device address):

    (gdb) print /x *p_xfer_desc
    print /x *p_xfer_desc
    $6 = {type = 0x2, address = 0x…, primary_length = 0x1, secondary_length = 0x1, p_primary_buf = 0x200041e3,
    p_secondary_buf = 0x200041e0}

  • Ooops… SOMETHING has happened, it has worked !

    I must admit that it is not exactly the same FW as that on which I observed the issue when I opened the thread, I will try to make a git clone of the same version to see if I can reproduce the problem again…

  • OK, I was a bit too quick in writing that it had worked. In fact there is exactly the same problem in the same conditions.

    When I run the program with this sort of command line:

    nrfjprog --reset && JLinkRTTLogger -Device nrf52840_XXAA -If SWD -Speed 4000 -RTTCHannel 0 /dev/stdout

    then I get the problem occurring (ie my FreeRTOS queue is timed out, which calls the app error handler).

    We are using SEGGER backend for the NRF LOGGER, and it seems that this is what creates the condition that makes the problem happen.

    When I run the same program under the debugger (which is what I did to post the content of registers as you required), then I can get through the line at which the problem occurs without it occurring --- that is why I thought in my previous post that the problem had disappeared.

    In fact it is still there but I was not in the conditions in which it happens.

    Maybe I should push the TWI registers content to the logger so that we can see what is their content in the conditions when the problem happens.

Reply
  • OK, I was a bit too quick in writing that it had worked. In fact there is exactly the same problem in the same conditions.

    When I run the program with this sort of command line:

    nrfjprog --reset && JLinkRTTLogger -Device nrf52840_XXAA -If SWD -Speed 4000 -RTTCHannel 0 /dev/stdout

    then I get the problem occurring (ie my FreeRTOS queue is timed out, which calls the app error handler).

    We are using SEGGER backend for the NRF LOGGER, and it seems that this is what creates the condition that makes the problem happen.

    When I run the same program under the debugger (which is what I did to post the content of registers as you required), then I can get through the line at which the problem occurs without it occurring --- that is why I thought in my previous post that the problem had disappeared.

    In fact it is still there but I was not in the conditions in which it happens.

    Maybe I should push the TWI registers content to the logger so that we can see what is their content in the conditions when the problem happens.

Children
No Data
Related