Skip to content

Commit e869cbc

Browse files
committed
Merge pull request #66 from 0xc0170/critical_section
Critical section
2 parents 4c7142b + 3a59a07 commit e869cbc

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

core/util/critical/critical.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
// This critical section implementation is generic for mbed OS targets,
19+
// except Nordic ones
20+
#if defined(TARGET_LIKE_MBED) && !defined(TARGET_NORDIC)
21+
22+
#include <stdint.h>
23+
#include <stddef.h>
24+
#include "cmsis.h"
25+
#include <assert.h>
26+
27+
// Module include
28+
#include "critical.h"
29+
30+
static volatile uint32_t interrupt_enable_counter = 0;
31+
static volatile uint32_t critical_primask = 0;
32+
33+
void core_util_critical_section_enter()
34+
{
35+
uint32_t primask = __get_PRIMASK(); /* get the current interrupt enabled state */
36+
__disable_irq();
37+
38+
/* Save the interrupt enabled state as it was prior to any nested critical section lock use */
39+
if (!interrupt_enable_counter) {
40+
critical_primask = primask & 0x1;
41+
}
42+
43+
/* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
44+
are enabled, then something has gone badly wrong thus assert an error.
45+
*/
46+
assert(interrupt_enable_counter < UINT32_MAX);
47+
if (interrupt_enable_counter > 0) {
48+
assert(primask & 0x1);
49+
}
50+
interrupt_enable_counter++;
51+
}
52+
53+
void core_util_critical_section_exit()
54+
{
55+
/* If critical_section_enter has not previously been called, do nothing */
56+
if (interrupt_enable_counter) {
57+
58+
uint32_t primask = __get_PRIMASK(); /* get the current interrupt enabled state */
59+
60+
assert(primask & 0x1); /* Interrupts must be disabled on invoking an exit from a critical section */
61+
62+
interrupt_enable_counter--;
63+
64+
/* Only re-enable interrupts if we are exiting the last of the nested critical sections and
65+
interrupts were enabled on entry to the first critical section.
66+
*/
67+
if (!interrupt_enable_counter && !critical_primask) {
68+
__enable_irq();
69+
}
70+
}
71+
}
72+
73+
#endif // defined(TARGET_LIKE_MBED) && !defined(TARGET_NORDIC)

core/util/critical/critical.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
#ifndef __MBED_UTIL_CRITICAL_H__
19+
#define __MBED_UTIL_CRITICAL_H__
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
/** Mark the start of a critical section
26+
*
27+
* This function should be called to mark the start of a critical section of code.
28+
* \note
29+
* NOTES:
30+
* 1) The use of this style of critical section is targetted at C based implementations.
31+
* 2) These critical sections can be nested.
32+
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
33+
* section) will be preserved on exit from the section.
34+
* 4) This implementation will currently only work on code running in privileged mode.
35+
*/
36+
void core_util_critical_section_enter();
37+
38+
/** Mark the end of a critical section
39+
*
40+
* This function should be called to mark the end of a critical section of code.
41+
* \note
42+
* NOTES:
43+
* 1) The use of this style of critical section is targetted at C based implementations.
44+
* 2) These critical sections can be nested.
45+
* 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
46+
* section) will be preserved on exit from the section.
47+
* 4) This implementation will currently only work on code running in privileged mode.
48+
*/
49+
void core_util_critical_section_exit();
50+
51+
#ifdef __cplusplus
52+
} // extern "C"
53+
#endif
54+
55+
56+
#endif // __MBED_UTIL_CRITICAL_H__

core/util/critical/critical_nordic.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
#ifdef TARGET_NORDIC
19+
20+
#include <stdint.h>
21+
#include <stddef.h>
22+
#include "cmsis.h"
23+
#include <assert.h>
24+
#include <stdbool.h>
25+
#include <nrf_soc.h>
26+
#include <nrf_sdm.h>
27+
28+
#include "critical.h"
29+
30+
static volatile union {
31+
uint32_t _PRIMASK_state;
32+
uint8_t _sd_state;
33+
} _state = { 0 } ;
34+
static volatile uint32_t _entry_count = 0;
35+
static volatile bool _use_softdevice_routine = false;
36+
37+
void core_util_critical_section_enter()
38+
{
39+
// if a critical section has already been entered, just update the counter
40+
if (_entry_count) {
41+
++_entry_count;
42+
return;
43+
}
44+
45+
// in this path, a critical section has never been entered
46+
uint32_t primask = __get_PRIMASK();
47+
48+
// if interrupts are enabled, try to use the soft device
49+
uint8_t sd_enabled;
50+
if ((primask == 0) && (sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) && sd_enabled == 1) {
51+
// if the soft device can be use, use it
52+
sd_nvic_critical_region_enter(&_state._sd_state);
53+
_use_softdevice_routine = true;
54+
} else {
55+
// if interrupts where enabled, disable them
56+
if(primask == 0) {
57+
__disable_irq();
58+
}
59+
60+
// store the PRIMASK state, it will be restored at the end of the critical section
61+
_state._PRIMASK_state = primask;
62+
_use_softdevice_routine = false;
63+
}
64+
65+
assert(_entry_count == 0); // entry count should always be equal to 0 at this point
66+
++_entry_count;
67+
}
68+
69+
void core_util_critical_section_exit()
70+
{
71+
assert(_entry_count > 0);
72+
--_entry_count;
73+
74+
// If their is other segments which have entered the critical section, just leave
75+
if (_entry_count) {
76+
return;
77+
}
78+
79+
// This is the last segment of the critical section, state should be restored as before entering
80+
// the critical section
81+
if (_use_softdevice_routine) {
82+
sd_nvic_critical_region_exit(_state._sd_state);
83+
} else {
84+
__set_PRIMASK(_state._PRIMASK_state);
85+
}
86+
}
87+
88+
#endif //TARGET_NORDIC

0 commit comments

Comments
 (0)