1 | /* mbed Microcontroller Library
|
---|
2 | * Copyright (c) 2017 ARM Limited
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | #include "cmsis.h"
|
---|
17 | #include "hal/critical_section_api.h"
|
---|
18 | #include "platform/mbed_assert.h"
|
---|
19 | #include "platform/mbed_toolchain.h"
|
---|
20 |
|
---|
21 | #include <stdbool.h>
|
---|
22 |
|
---|
23 | static volatile bool critical_interrupts_enabled = false;
|
---|
24 | static volatile bool state_saved = false;
|
---|
25 |
|
---|
26 | static bool are_interrupts_enabled(void)
|
---|
27 | {
|
---|
28 | #if defined(__CORTEX_A9)
|
---|
29 | return ((__get_CPSR() & 0x80) == 0);
|
---|
30 | #else
|
---|
31 | return ((__get_PRIMASK() & 0x1) == 0);
|
---|
32 | #endif
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | MBED_WEAK void hal_critical_section_enter(void)
|
---|
37 | {
|
---|
38 | const bool interrupt_state = are_interrupts_enabled();
|
---|
39 |
|
---|
40 | __disable_irq();
|
---|
41 |
|
---|
42 | if (state_saved == true) {
|
---|
43 | return;
|
---|
44 | }
|
---|
45 |
|
---|
46 | critical_interrupts_enabled = interrupt_state;
|
---|
47 | state_saved = true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | MBED_WEAK void hal_critical_section_exit(void)
|
---|
51 | {
|
---|
52 | // Interrupts must be disabled on invoking an exit from a critical section
|
---|
53 | MBED_ASSERT(!are_interrupts_enabled());
|
---|
54 | state_saved = false;
|
---|
55 |
|
---|
56 | // Restore the IRQs to their state prior to entering the critical section
|
---|
57 | if (critical_interrupts_enabled == true) {
|
---|
58 | __enable_irq();
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | MBED_WEAK bool hal_in_critical_section(void)
|
---|
63 | {
|
---|
64 | return (state_saved == true);
|
---|
65 | }
|
---|