Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Why is NRFX_CRITICAL_SECTION_ENTER not used inside nrfx_gpiote_in_init->channel_port_alloc?

I use nRF SDK 15. with FreeRTOS.

nrfx_gpiote.c
NRFX_CRITICAL_SECTION_ENTER()/NRFX_CRITICAL_SECTION_EXIT() is not use.

Is this exclusive among threads?


static int8_t channel_port_alloc(uint32_t pin, nrfx_gpiote_evt_handler_t handler, bool channel)
{
    int8_t   channel_id = NO_CHANNELS;
    uint32_t i;

    uint32_t start_idx = channel ? 0 : GPIOTE_CH_NUM;
    uint32_t end_idx   =
        channel ? GPIOTE_CH_NUM : (GPIOTE_CH_NUM + NRFX_GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS);

    // critical section

    for (i = start_idx; i < end_idx; i++)
    {
        if (m_cb.handlers[i] == FORBIDDEN_HANDLER_ADDRESS)
        {
            pin_in_use_by_te_set(pin, i, handler, channel);
            channel_id = i;
            break;
        }
    }
    // critical section
    return channel_id;
}

  • Hi,

    If channel_port_alloc() is interrupted by another channel_port_alloc() call from another thread or interrupt at just the wrong time, then there could be problems. That is why the comment is there I assume.

    This function is used in the configuration stage so I suspect it is not likely that it would happen in practice in most applications, though perhaps more in a RTOS based one than in a bare metal one (where you would typically do all initialization in the beginning from main).

    If this can be an issue in your application it makes sense to use a critical section. As this function is typically only called a few times and only in the beginning there should not be any practical downside of doing so.

  • So, to do the exclusion,
    Should I add NRFX_CRITICAL_SECTION_ENTER () / NRFX_CRITICAL_SECTION_EXIT ()?

    Also, does it make sense that other than GPIOTE、(PPI / WDT / SWI)  uses NRFX_CRITICAL_SECTION_ENTER () / EXIT () but does not use only GPIOTE?

  • loquat said:
    So, to do the exclusion,
    Should I add NRFX_CRITICAL_SECTION_ENTER () / NRFX_CRITICAL_SECTION_EXIT ()?

    That can make sense in some applications, and I do not see any down side of doing it (this is code that is typically only executed a few times at startup).

    You do not generally need critical sections in functions that operate on peripherals, but where there are/can be multiple users and you for instance pick the next available in a list as with GPIOTE channels etc, where it makes sense. For WDT that you mention for instance, it is already implemented (See implementation of nrfx_wdt_channel_alloc() in modules\nrfx\drivers\src\nrfx_wdt.c). It is also the same for nrfx_ppi_channel_alloc().

Related