nRF5 SDK is not maintained anymore
More Info: Consider nRF Connect SDK for new designs

Explanation on CRITICAL_REGION_ETER() and CRITICAL_REGION_EXIT() in app_util_platform.h

Hii. I was using scheuler and wanted to know how this part of code works. The #define CRITICAL_REGION_ENTER() has opening bracket but it is closing after #define CRITICAL_REGION_EXIT(). So, why it is this done this way? And what exaclty happens when we call CRITICAL_REGION_ENTER()?

/**@brief Macro for entering a critical region.
 *
 * @note Due to implementation details, there must exist one and only one call to
 *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
 *       in the same scope.
 */
#ifdef SOFTDEVICE_PRESENT
#define CRITICAL_REGION_ENTER()                                                             \
    {                                                                                       \
        uint8_t __CR_NESTED = 0;                                                            \
        app_util_critical_region_enter(&__CR_NESTED);
#else
#define CRITICAL_REGION_ENTER() app_util_critical_region_enter(NULL)
#endif

/**@brief Macro for leaving a critical region.
 *
 * @note Due to implementation details, there must exist one and only one call to
 *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
 *       in the same scope.
 */
#ifdef SOFTDEVICE_PRESENT
#define CRITICAL_REGION_EXIT()                                                              \
        app_util_critical_region_exit(__CR_NESTED);                                         \
    }
#else
#define CRITICAL_REGION_EXIT() app_util_critical_region_exit(0)
#endif

  • Hi,

    The critical region will disable (application) interrupts, to prevent interrupts from interrupting a critical region of code. You should have exactly one CRITICAL_REGION_EXIT() call for each CRITICAL_REGION_ENTER() call. Then the interrupts are enabled when exiting the critical region.

    The curly brackets will generate compile errors if you do not follow these requirements.

    Best regards,
    Jørgen

Related