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-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_critical.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-01-27 11:10:28 +00:00
|
|
|
#include "platform/mbed_toolchain.h"
|
2016-02-24 19:35:16 +00:00
|
|
|
|
|
|
|
static volatile uint32_t interrupt_enable_counter = 0;
|
2016-06-08 17:41:19 +00:00
|
|
|
static volatile bool critical_interrupts_disabled = false;
|
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)
|
|
|
|
switch(__get_CPSR() & 0x1FU) {
|
|
|
|
case MODE_USR:
|
|
|
|
case MODE_SYS:
|
|
|
|
return false;
|
|
|
|
case MODE_SVC:
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return (__get_IPSR() != 0U);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-09-27 15:54:33 +00:00
|
|
|
MBED_WEAK void core_util_critical_section_enter(void)
|
2016-02-24 19:35:16 +00:00
|
|
|
{
|
2016-07-01 14:08:31 +00:00
|
|
|
bool interrupts_disabled = !core_util_are_interrupts_enabled();
|
2016-02-24 19:35:16 +00:00
|
|
|
__disable_irq();
|
|
|
|
|
2016-06-06 10:52:14 +00:00
|
|
|
/* Save the interrupt disabled state as it was prior to any nested critical section lock use */
|
2016-02-24 19:35:16 +00:00
|
|
|
if (!interrupt_enable_counter) {
|
2016-06-08 17:41:19 +00:00
|
|
|
critical_interrupts_disabled = interrupts_disabled;
|
2016-02-24 19:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts
|
|
|
|
are enabled, then something has gone badly wrong thus assert an error.
|
|
|
|
*/
|
2016-05-31 14:07:26 +00:00
|
|
|
MBED_ASSERT(interrupt_enable_counter < UINT32_MAX);
|
2016-06-13 15:52:37 +00:00
|
|
|
// FIXME
|
|
|
|
#ifndef FEATURE_UVISOR
|
2016-02-24 19:35:16 +00:00
|
|
|
if (interrupt_enable_counter > 0) {
|
2016-06-08 17:41:19 +00:00
|
|
|
MBED_ASSERT(interrupts_disabled);
|
2016-02-24 19:35:16 +00:00
|
|
|
}
|
2016-06-13 15:52:37 +00:00
|
|
|
#else
|
|
|
|
#warning "core_util_critical_section_enter needs fixing to work from unprivileged code"
|
|
|
|
#endif /* FEATURE_UVISOR */
|
2016-02-24 19:35:16 +00:00
|
|
|
interrupt_enable_counter++;
|
|
|
|
}
|
|
|
|
|
2016-09-27 15:54:33 +00:00
|
|
|
MBED_WEAK void core_util_critical_section_exit(void)
|
2016-02-24 19:35:16 +00:00
|
|
|
{
|
|
|
|
/* If critical_section_enter has not previously been called, do nothing */
|
|
|
|
if (interrupt_enable_counter) {
|
|
|
|
|
2016-06-13 15:52:37 +00:00
|
|
|
// FIXME
|
|
|
|
#ifndef FEATURE_UVISOR
|
2016-07-01 14:08:31 +00:00
|
|
|
bool interrupts_disabled = !core_util_are_interrupts_enabled(); /* get the current interrupt disabled state */
|
2016-02-24 19:35:16 +00:00
|
|
|
|
2016-06-08 17:41:19 +00:00
|
|
|
MBED_ASSERT(interrupts_disabled); /* Interrupts must be disabled on invoking an exit from a critical section */
|
2016-06-13 15:52:37 +00:00
|
|
|
#else
|
|
|
|
#warning "core_util_critical_section_exit needs fixing to work from unprivileged code"
|
|
|
|
#endif /* FEATURE_UVISOR */
|
2016-02-24 19:35:16 +00:00
|
|
|
|
|
|
|
interrupt_enable_counter--;
|
|
|
|
|
|
|
|
/* Only re-enable interrupts if we are exiting the last of the nested critical sections and
|
|
|
|
interrupts were enabled on entry to the first critical section.
|
|
|
|
*/
|
2016-06-06 10:52:14 +00:00
|
|
|
if (!interrupt_enable_counter && !critical_interrupts_disabled) {
|
2016-02-24 19:35:16 +00:00
|
|
|
__enable_irq();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 23:30:09 +00:00
|
|
|
|
2017-08-11 16:09:57 +00:00
|
|
|
#if __EXCLUSIVE_ACCESS
|
2016-06-01 23:30:09 +00:00
|
|
|
|
2016-08-31 18:10:01 +00:00
|
|
|
/* Supress __ldrex and __strex deprecated warnings - "#3731-D: intrinsic is deprecated" */
|
2016-08-31 19:30:45 +00:00
|
|
|
#if defined (__CC_ARM)
|
2016-08-31 18:10:01 +00:00
|
|
|
#pragma diag_suppress 3731
|
2016-08-31 19:30:45 +00:00
|
|
|
#endif
|
2016-08-31 18:10:01 +00:00
|
|
|
|
2016-06-01 23:30:09 +00:00
|
|
|
bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue)
|
|
|
|
{
|
|
|
|
uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr);
|
|
|
|
if (currentValue != *expectedCurrentValue) {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
__CLREX();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !__STREXB(desiredValue, (volatile uint8_t*)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue)
|
|
|
|
{
|
|
|
|
uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr);
|
|
|
|
if (currentValue != *expectedCurrentValue) {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
__CLREX();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !__STREXH(desiredValue, (volatile uint16_t*)ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue)
|
|
|
|
{
|
|
|
|
uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr);
|
|
|
|
if (currentValue != *expectedCurrentValue) {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
__CLREX();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !__STREXW(desiredValue, (volatile uint32_t*)ptr);
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint8_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta;
|
|
|
|
} while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint16_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta;
|
|
|
|
} while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint32_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta;
|
|
|
|
} while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint8_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta;
|
|
|
|
} while (__STREXB(newValue, (volatile uint8_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint16_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta;
|
|
|
|
} while (__STREXH(newValue, (volatile uint16_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint32_t newValue;
|
|
|
|
do {
|
|
|
|
newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta;
|
|
|
|
} while (__STREXW(newValue, (volatile uint32_t*)valuePtr));
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue)
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
uint8_t currentValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
currentValue = *ptr;
|
|
|
|
if (currentValue == *expectedCurrentValue) {
|
|
|
|
*ptr = desiredValue;
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue)
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
uint16_t currentValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
currentValue = *ptr;
|
|
|
|
if (currentValue == *expectedCurrentValue) {
|
|
|
|
*ptr = desiredValue;
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue)
|
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
uint32_t currentValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
currentValue = *ptr;
|
|
|
|
if (currentValue == *expectedCurrentValue) {
|
|
|
|
*ptr = desiredValue;
|
|
|
|
success = true;
|
|
|
|
} else {
|
|
|
|
*expectedCurrentValue = currentValue;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-06-25 15:58:08 +00:00
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint8_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr + delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint16_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr + delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint32_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr + delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint8_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr - delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint16_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr - delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-05 17:05:10 +00:00
|
|
|
uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta)
|
2016-06-01 23:30:09 +00:00
|
|
|
{
|
|
|
|
uint32_t newValue;
|
|
|
|
core_util_critical_section_enter();
|
|
|
|
newValue = *valuePtr - delta;
|
|
|
|
*valuePtr = newValue;
|
|
|
|
core_util_critical_section_exit();
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2016-07-08 19:50:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue) {
|
|
|
|
return core_util_atomic_cas_u32(
|
|
|
|
(uint32_t *)ptr,
|
|
|
|
(uint32_t *)expectedCurrentValue,
|
|
|
|
(uint32_t)desiredValue);
|
2016-06-25 15:58:08 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 19:50:03 +00:00
|
|
|
void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta) {
|
|
|
|
return (void *)core_util_atomic_incr_u32((uint32_t *)valuePtr, (uint32_t)delta);
|
|
|
|
}
|
2016-07-08 03:51:31 +00:00
|
|
|
|
2016-07-08 19:50:03 +00:00
|
|
|
void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta) {
|
|
|
|
return (void *)core_util_atomic_decr_u32((uint32_t *)valuePtr, (uint32_t)delta);
|
|
|
|
}
|
2016-06-01 23:30:09 +00:00
|
|
|
|