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