mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_critical_section_api.c Source File

mbed_critical_section_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "cmsis.h"
00018 #include "hal/critical_section_api.h"
00019 #include "platform/mbed_assert.h"
00020 #include "platform/mbed_toolchain.h"
00021 
00022 #include <stdbool.h>
00023 
00024 static volatile bool critical_interrupts_enabled = false;
00025 static volatile bool state_saved = false;
00026 
00027 static bool are_interrupts_enabled(void)
00028 {
00029 #if defined(__CORTEX_A9)
00030     return ((__get_CPSR() & 0x80) == 0);
00031 #else
00032     return ((__get_PRIMASK() & 0x1) == 0);
00033 #endif
00034 }
00035 
00036 
00037 MBED_WEAK void hal_critical_section_enter(void)
00038 {
00039     const bool interrupt_state = are_interrupts_enabled();
00040 
00041     __disable_irq();
00042 
00043     if (state_saved == true) {
00044         return;
00045     }
00046 
00047     critical_interrupts_enabled = interrupt_state;
00048     state_saved = true;
00049 }
00050 
00051 MBED_WEAK void hal_critical_section_exit(void)
00052 {
00053     // Interrupts must be disabled on invoking an exit from a critical section
00054     MBED_ASSERT(!are_interrupts_enabled());
00055     state_saved = false;
00056 
00057     // Restore the IRQs to their state prior to entering the critical section
00058     if (critical_interrupts_enabled == true) {
00059         __enable_irq();
00060     }
00061 }
00062 
00063 MBED_WEAK bool hal_in_critical_section(void)
00064 {
00065     return (state_saved == true);
00066 }