EVE 1.0
critreg.c
Go to the documentation of this file.
1 /**********************************************************************/
2 /*
3  * Copyright (c) 2016, Jetro AS
4  * All rights reserved.
5  *
6  * Reproduction or redistribution of this file is prohibited, unless such reproduction
7  * or redistribution is explicitly permitted by a license agreement.
8  */
9 
10  /**
11  * \file
12  * \brief Fast version of nRF soft-device-friendly critical regions.
13  * Original Nordic critical region implementation masks user interrupts in NVIC,
14  * which is a perfectly fine, but quite slow method. This implementation uses
15  * other approach: it disables application interrupts via BASEPRI register,
16  * which is much faster. High-priority softdevice interrupts are not affected.
17  *
18  * \author DT, Jetro AS
19  */ /******************************************************************/
20 
21 #include <app_util_platform.h>
22 #include <app_util.h>
23 #include <lib/assert.h>
24 
25 static uint32_t IrqDisableCounter;
26 
27 STATIC_ASSERT(APP_IRQ_PRIORITY_HIGH != 0);
28 
29 void app_util_disable_irq(void)
30 {
31  __disable_irq();
32  ++IrqDisableCounter;
33 }
34 
35 void app_util_enable_irq(void)
36 {
37  assert(IrqDisableCounter);
38  if (--IrqDisableCounter == 0)
39  __enable_irq();
40 }
41 
42 void app_util_critical_region_enter(uint8_t *p_nested)
43 {
44  __disable_irq();
45  *p_nested = __get_BASEPRI();
46  __set_BASEPRI_MAX(APP_IRQ_PRIORITY_HIGH << (8U - __NVIC_PRIO_BITS));
47  __enable_irq();
48 }
49 
50 void app_util_critical_region_exit(uint8_t nested)
51 {
52  __set_BASEPRI(nested);
53 }