2016-02-24 19:35:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-11-21 16:59:45 +00:00
|
|
|
/* Declare __STDC_LIMIT_MACROS so stdint.h defines UINT32_MAX when using C++ */
|
|
|
|
#define __STDC_LIMIT_MACROS
|
2017-10-19 12:32:18 +00:00
|
|
|
#include "hal/critical_section_api.h"
|
2016-07-05 16:44:06 +00:00
|
|
|
|
2016-02-24 19:35:16 +00:00
|
|
|
#include "cmsis.h"
|
2016-10-01 07:11:36 +00:00
|
|
|
#include "platform/mbed_assert.h"
|
2017-10-19 12:32:18 +00:00
|
|
|
#include "platform/mbed_critical.h"
|
2017-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_toolchain.h"
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2019-01-03 08:54:31 +00:00
|
|
|
static uint32_t critical_section_reentrancy_counter = 0;
|
2016-06-06 10:52:14 +00:00
|
|
|
|
2016-07-01 14:08:31 +00:00
|
|
|
bool core_util_are_interrupts_enabled(void)
|
2016-06-06 10:52:14 +00:00
|
|
|
{
|
|
|
|
#if defined(__CORTEX_A9)
|
2016-06-09 15:41:58 +00:00
|
|
|
return ((__get_CPSR() & 0x80) == 0);
|
2016-06-06 10:52:14 +00:00
|
|
|
#else
|
2016-06-09 15:41:58 +00:00
|
|
|
return ((__get_PRIMASK() & 0x1) == 0);
|
2016-06-06 10:52:14 +00:00
|
|
|
#endif
|
|
|
|
}
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2017-05-24 21:29:11 +00:00
|
|
|
bool core_util_is_isr_active(void)
|
|
|
|
{
|
|
|
|
#if defined(__CORTEX_A9)
|
2018-06-27 14:09:15 +00:00
|
|
|
switch (__get_CPSR() & 0x1FU) {
|
2017-07-25 14:45:11 +00:00
|
|
|
case CPSR_M_USR:
|
|
|
|
case CPSR_M_SYS:
|
2017-05-24 21:29:11 +00:00
|
|
|
return false;
|
2017-07-25 14:45:11 +00:00
|
|
|
case CPSR_M_SVC:
|
2017-05-24 21:29:11 +00:00
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return (__get_IPSR() != 0U);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
bool core_util_in_critical_section(void)
|
2016-02-24 19:35:16 +00:00
|
|
|
{
|
2018-01-04 13:59:44 +00:00
|
|
|
return hal_in_critical_section();
|
2017-10-19 12:32:18 +00:00
|
|
|
}
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
void core_util_critical_section_enter(void)
|
|
|
|
{
|
2019-01-03 08:54:31 +00:00
|
|
|
hal_critical_section_enter();
|
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
// If the reentrancy counter overflows something has gone badly wrong.
|
|
|
|
MBED_ASSERT(critical_section_reentrancy_counter < UINT32_MAX);
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2017-12-19 13:35:43 +00:00
|
|
|
++critical_section_reentrancy_counter;
|
2016-02-24 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
void core_util_critical_section_exit(void)
|
2016-02-24 19:35:16 +00:00
|
|
|
{
|
2017-10-25 17:04:40 +00:00
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
// If critical_section_enter has not previously been called, do nothing
|
|
|
|
if (critical_section_reentrancy_counter == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2017-12-19 13:35:43 +00:00
|
|
|
--critical_section_reentrancy_counter;
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2017-10-19 12:32:18 +00:00
|
|
|
if (critical_section_reentrancy_counter == 0) {
|
|
|
|
hal_critical_section_exit();
|
2016-02-24 19:35:16 +00:00
|
|
|
}
|
|
|
|
}
|