Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

CRITICAL_REGION_ENTER nesting support?

There are tickets from 3 years ago that claim that nested CRITICAL_REGION_ENTER calls are not supported. Or they are supported if you write your own macros. 

It appears to me that they are supported as long as SOFTDEVICE_PRESENT is defined. The macro takes a pointer to a uint8_t which is updated by app_util_critical_region_enter() to indicate that you are in a nested region. Since this is a output, not a input it appears that the current implementation does support nesting.

Please confirm....

  • Hi,

     

    I can confirm that when SOFTDEVICE_PRESENT is set, the CRITICAL_REGION_* supports nesting.

    __STATIC_INLINE uint32_t sd_nvic_critical_region_enter(uint8_t * p_is_nested_critical_region)
    {
      int was_masked = __sd_nvic_irq_disable();
      if (!nrf_nvic_state.__cr_flag)
      {
        nrf_nvic_state.__cr_flag = 1;
        nrf_nvic_state.__irq_masks[0] = ( NVIC->ICER[0] & __NRF_NVIC_APP_IRQS_0 );
        NVIC->ICER[0] = __NRF_NVIC_APP_IRQS_0;
        nrf_nvic_state.__irq_masks[1] = ( NVIC->ICER[1] & __NRF_NVIC_APP_IRQS_1 );
        NVIC->ICER[1] = __NRF_NVIC_APP_IRQS_1;
        *p_is_nested_critical_region = 0;
      }
      else
      {
        *p_is_nested_critical_region = 1;
      }
      if (!was_masked)
      {
        __sd_nvic_irq_enable();
      }
      return NRF_SUCCESS;
    }
    
    __STATIC_INLINE uint32_t sd_nvic_critical_region_exit(uint8_t is_nested_critical_region)
    {
      if (nrf_nvic_state.__cr_flag && (is_nested_critical_region == 0))
      {
        int was_masked = __sd_nvic_irq_disable();
        NVIC->ISER[0] = nrf_nvic_state.__irq_masks[0];
        NVIC->ISER[1] = nrf_nvic_state.__irq_masks[1];
        nrf_nvic_state.__cr_flag = 0;
        if (!was_masked)
        {
          __sd_nvic_irq_enable();
        }
      }
    
      return NRF_SUCCESS;
    }

    Cheers,

    Håkon

  • Yup...  Thanks for the confirmation. It would be nice if issues were either removed or updated to reflect the new status. This isn't the first time that I got bad information from this forum.

Related