Skip to content

Commit 597f104

Browse files
committed
TARGET_NRF51822 - Override critical section enter/exit when Nordic SDK v10 is used.
This change is not located in the hal because SDK v10 is not available from this place. It is safe to provide the implementation in BLE because the SDK V10 is defined there. Default implementations of critical section enter/exit are safe as long as the softdevice is not used.
1 parent c536392 commit 597f104

File tree

1 file changed

+80
-0
lines changed
  • features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_MCU_NRF51822/hal_patch

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <stdint.h> // uint32_t, UINT32_MAX
19+
#include <assert.h> // uint32_t, UINT32_MAX
20+
#include "cmsis.h"
21+
#include "nrf_soc.h"
22+
#include "nrf_sdm.h"
23+
24+
static volatile union {
25+
uint32_t _PRIMASK_state;
26+
uint8_t _sd_state;
27+
} _state = { 0 } ;
28+
static volatile uint32_t _entry_count = 0;
29+
static volatile bool _use_softdevice_routine = false;
30+
31+
void core_util_critical_section_enter()
32+
{
33+
// if a critical section has already been entered, just update the counter
34+
if (_entry_count) {
35+
++_entry_count;
36+
return;
37+
}
38+
39+
// in this path, a critical section has never been entered
40+
uint32_t primask = __get_PRIMASK();
41+
42+
// if interrupts are enabled, try to use the soft device
43+
uint8_t sd_enabled;
44+
if ((primask == 0) && (sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) && sd_enabled == 1) {
45+
// if the soft device can be use, use it
46+
sd_nvic_critical_region_enter(&_state._sd_state);
47+
_use_softdevice_routine = true;
48+
} else {
49+
// if interrupts where enabled, disable them
50+
if(primask == 0) {
51+
__disable_irq();
52+
}
53+
54+
// store the PRIMASK state, it will be restored at the end of the critical section
55+
_state._PRIMASK_state = primask;
56+
_use_softdevice_routine = false;
57+
}
58+
59+
assert(_entry_count == 0); // entry count should always be equal to 0 at this point
60+
++_entry_count;
61+
}
62+
63+
void core_util_critical_section_exit()
64+
{
65+
assert(_entry_count > 0);
66+
--_entry_count;
67+
68+
// If their is other segments which have entered the critical section, just leave
69+
if (_entry_count) {
70+
return;
71+
}
72+
73+
// This is the last segment of the critical section, state should be restored as before entering
74+
// the critical section
75+
if (_use_softdevice_routine) {
76+
sd_nvic_critical_region_exit(_state._sd_state);
77+
} else {
78+
__set_PRIMASK(_state._PRIMASK_state);
79+
}
80+
}

0 commit comments

Comments
 (0)