
BBR 1 Ebene
mbed-os/platform/mbed_critical.c@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved |
borlanic | 0:fbdae7e6d805 | 3 | * SPDX-License-Identifier: Apache-2.0 |
borlanic | 0:fbdae7e6d805 | 4 | * |
borlanic | 0:fbdae7e6d805 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
borlanic | 0:fbdae7e6d805 | 6 | * not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 7 | * You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 8 | * |
borlanic | 0:fbdae7e6d805 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 10 | * |
borlanic | 0:fbdae7e6d805 | 11 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
borlanic | 0:fbdae7e6d805 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 14 | * See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 15 | * limitations under the License. |
borlanic | 0:fbdae7e6d805 | 16 | */ |
borlanic | 0:fbdae7e6d805 | 17 | |
borlanic | 0:fbdae7e6d805 | 18 | /* Declare __STDC_LIMIT_MACROS so stdint.h defines UINT32_MAX when using C++ */ |
borlanic | 0:fbdae7e6d805 | 19 | #define __STDC_LIMIT_MACROS |
borlanic | 0:fbdae7e6d805 | 20 | #include "hal/critical_section_api.h" |
borlanic | 0:fbdae7e6d805 | 21 | |
borlanic | 0:fbdae7e6d805 | 22 | #include "cmsis.h" |
borlanic | 0:fbdae7e6d805 | 23 | #include "platform/mbed_assert.h" |
borlanic | 0:fbdae7e6d805 | 24 | #include "platform/mbed_critical.h" |
borlanic | 0:fbdae7e6d805 | 25 | #include "platform/mbed_toolchain.h" |
borlanic | 0:fbdae7e6d805 | 26 | |
borlanic | 0:fbdae7e6d805 | 27 | // if __EXCLUSIVE_ACCESS rtx macro not defined, we need to get this via own-set architecture macros |
borlanic | 0:fbdae7e6d805 | 28 | #ifndef MBED_EXCLUSIVE_ACCESS |
borlanic | 0:fbdae7e6d805 | 29 | #ifndef __EXCLUSIVE_ACCESS |
borlanic | 0:fbdae7e6d805 | 30 | #if ((__ARM_ARCH_7M__ == 1U) || \ |
borlanic | 0:fbdae7e6d805 | 31 | (__ARM_ARCH_7EM__ == 1U) || \ |
borlanic | 0:fbdae7e6d805 | 32 | (__ARM_ARCH_8M_BASE__ == 1U) || \ |
borlanic | 0:fbdae7e6d805 | 33 | (__ARM_ARCH_8M_MAIN__ == 1U)) || \ |
borlanic | 0:fbdae7e6d805 | 34 | (__ARM_ARCH_7A__ == 1U) |
borlanic | 0:fbdae7e6d805 | 35 | #define MBED_EXCLUSIVE_ACCESS 1U |
borlanic | 0:fbdae7e6d805 | 36 | #elif (__ARM_ARCH_6M__ == 1U) |
borlanic | 0:fbdae7e6d805 | 37 | #define MBED_EXCLUSIVE_ACCESS 0U |
borlanic | 0:fbdae7e6d805 | 38 | #else |
borlanic | 0:fbdae7e6d805 | 39 | #error "Unknown architecture for exclusive access" |
borlanic | 0:fbdae7e6d805 | 40 | #endif |
borlanic | 0:fbdae7e6d805 | 41 | #else |
borlanic | 0:fbdae7e6d805 | 42 | #define MBED_EXCLUSIVE_ACCESS __EXCLUSIVE_ACCESS |
borlanic | 0:fbdae7e6d805 | 43 | #endif |
borlanic | 0:fbdae7e6d805 | 44 | #endif |
borlanic | 0:fbdae7e6d805 | 45 | |
borlanic | 0:fbdae7e6d805 | 46 | static volatile uint32_t critical_section_reentrancy_counter = 0; |
borlanic | 0:fbdae7e6d805 | 47 | |
borlanic | 0:fbdae7e6d805 | 48 | bool core_util_are_interrupts_enabled(void) |
borlanic | 0:fbdae7e6d805 | 49 | { |
borlanic | 0:fbdae7e6d805 | 50 | #if defined(__CORTEX_A9) |
borlanic | 0:fbdae7e6d805 | 51 | return ((__get_CPSR() & 0x80) == 0); |
borlanic | 0:fbdae7e6d805 | 52 | #else |
borlanic | 0:fbdae7e6d805 | 53 | return ((__get_PRIMASK() & 0x1) == 0); |
borlanic | 0:fbdae7e6d805 | 54 | #endif |
borlanic | 0:fbdae7e6d805 | 55 | } |
borlanic | 0:fbdae7e6d805 | 56 | |
borlanic | 0:fbdae7e6d805 | 57 | bool core_util_is_isr_active(void) |
borlanic | 0:fbdae7e6d805 | 58 | { |
borlanic | 0:fbdae7e6d805 | 59 | #if defined(__CORTEX_A9) |
borlanic | 0:fbdae7e6d805 | 60 | switch(__get_CPSR() & 0x1FU) { |
borlanic | 0:fbdae7e6d805 | 61 | case CPSR_M_USR: |
borlanic | 0:fbdae7e6d805 | 62 | case CPSR_M_SYS: |
borlanic | 0:fbdae7e6d805 | 63 | return false; |
borlanic | 0:fbdae7e6d805 | 64 | case CPSR_M_SVC: |
borlanic | 0:fbdae7e6d805 | 65 | default: |
borlanic | 0:fbdae7e6d805 | 66 | return true; |
borlanic | 0:fbdae7e6d805 | 67 | } |
borlanic | 0:fbdae7e6d805 | 68 | #else |
borlanic | 0:fbdae7e6d805 | 69 | return (__get_IPSR() != 0U); |
borlanic | 0:fbdae7e6d805 | 70 | #endif |
borlanic | 0:fbdae7e6d805 | 71 | } |
borlanic | 0:fbdae7e6d805 | 72 | |
borlanic | 0:fbdae7e6d805 | 73 | bool core_util_in_critical_section(void) |
borlanic | 0:fbdae7e6d805 | 74 | { |
borlanic | 0:fbdae7e6d805 | 75 | return hal_in_critical_section(); |
borlanic | 0:fbdae7e6d805 | 76 | } |
borlanic | 0:fbdae7e6d805 | 77 | |
borlanic | 0:fbdae7e6d805 | 78 | void core_util_critical_section_enter(void) |
borlanic | 0:fbdae7e6d805 | 79 | { |
borlanic | 0:fbdae7e6d805 | 80 | // FIXME |
borlanic | 0:fbdae7e6d805 | 81 | #ifdef FEATURE_UVISOR |
borlanic | 0:fbdae7e6d805 | 82 | #warning "core_util_critical_section_enter needs fixing to work from unprivileged code" |
borlanic | 0:fbdae7e6d805 | 83 | #else |
borlanic | 0:fbdae7e6d805 | 84 | // If the reentrancy counter overflows something has gone badly wrong. |
borlanic | 0:fbdae7e6d805 | 85 | MBED_ASSERT(critical_section_reentrancy_counter < UINT32_MAX); |
borlanic | 0:fbdae7e6d805 | 86 | #endif /* FEATURE_UVISOR */ |
borlanic | 0:fbdae7e6d805 | 87 | |
borlanic | 0:fbdae7e6d805 | 88 | hal_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 89 | |
borlanic | 0:fbdae7e6d805 | 90 | ++critical_section_reentrancy_counter; |
borlanic | 0:fbdae7e6d805 | 91 | } |
borlanic | 0:fbdae7e6d805 | 92 | |
borlanic | 0:fbdae7e6d805 | 93 | void core_util_critical_section_exit(void) |
borlanic | 0:fbdae7e6d805 | 94 | { |
borlanic | 0:fbdae7e6d805 | 95 | // FIXME |
borlanic | 0:fbdae7e6d805 | 96 | #ifdef FEATURE_UVISOR |
borlanic | 0:fbdae7e6d805 | 97 | #warning "core_util_critical_section_exit needs fixing to work from unprivileged code" |
borlanic | 0:fbdae7e6d805 | 98 | #endif /* FEATURE_UVISOR */ |
borlanic | 0:fbdae7e6d805 | 99 | |
borlanic | 0:fbdae7e6d805 | 100 | // If critical_section_enter has not previously been called, do nothing |
borlanic | 0:fbdae7e6d805 | 101 | if (critical_section_reentrancy_counter == 0) { |
borlanic | 0:fbdae7e6d805 | 102 | return; |
borlanic | 0:fbdae7e6d805 | 103 | } |
borlanic | 0:fbdae7e6d805 | 104 | |
borlanic | 0:fbdae7e6d805 | 105 | --critical_section_reentrancy_counter; |
borlanic | 0:fbdae7e6d805 | 106 | |
borlanic | 0:fbdae7e6d805 | 107 | if (critical_section_reentrancy_counter == 0) { |
borlanic | 0:fbdae7e6d805 | 108 | hal_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 109 | } |
borlanic | 0:fbdae7e6d805 | 110 | } |
borlanic | 0:fbdae7e6d805 | 111 | |
borlanic | 0:fbdae7e6d805 | 112 | #if MBED_EXCLUSIVE_ACCESS |
borlanic | 0:fbdae7e6d805 | 113 | |
borlanic | 0:fbdae7e6d805 | 114 | /* Supress __ldrex and __strex deprecated warnings - "#3731-D: intrinsic is deprecated" */ |
borlanic | 0:fbdae7e6d805 | 115 | #if defined (__CC_ARM) |
borlanic | 0:fbdae7e6d805 | 116 | #pragma diag_suppress 3731 |
borlanic | 0:fbdae7e6d805 | 117 | #endif |
borlanic | 0:fbdae7e6d805 | 118 | |
borlanic | 0:fbdae7e6d805 | 119 | bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 120 | { |
borlanic | 0:fbdae7e6d805 | 121 | do { |
borlanic | 0:fbdae7e6d805 | 122 | uint8_t currentValue = __LDREXB(ptr); |
borlanic | 0:fbdae7e6d805 | 123 | if (currentValue != *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 124 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 125 | __CLREX(); |
borlanic | 0:fbdae7e6d805 | 126 | return false; |
borlanic | 0:fbdae7e6d805 | 127 | } |
borlanic | 0:fbdae7e6d805 | 128 | } while (__STREXB(desiredValue, ptr)); |
borlanic | 0:fbdae7e6d805 | 129 | return true; |
borlanic | 0:fbdae7e6d805 | 130 | } |
borlanic | 0:fbdae7e6d805 | 131 | |
borlanic | 0:fbdae7e6d805 | 132 | bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 133 | { |
borlanic | 0:fbdae7e6d805 | 134 | do { |
borlanic | 0:fbdae7e6d805 | 135 | uint16_t currentValue = __LDREXH(ptr); |
borlanic | 0:fbdae7e6d805 | 136 | if (currentValue != *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 137 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 138 | __CLREX(); |
borlanic | 0:fbdae7e6d805 | 139 | return false; |
borlanic | 0:fbdae7e6d805 | 140 | } |
borlanic | 0:fbdae7e6d805 | 141 | } while (__STREXH(desiredValue, ptr)); |
borlanic | 0:fbdae7e6d805 | 142 | return true; |
borlanic | 0:fbdae7e6d805 | 143 | } |
borlanic | 0:fbdae7e6d805 | 144 | |
borlanic | 0:fbdae7e6d805 | 145 | |
borlanic | 0:fbdae7e6d805 | 146 | bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 147 | { |
borlanic | 0:fbdae7e6d805 | 148 | do { |
borlanic | 0:fbdae7e6d805 | 149 | uint32_t currentValue = __LDREXW(ptr); |
borlanic | 0:fbdae7e6d805 | 150 | if (currentValue != *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 151 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 152 | __CLREX(); |
borlanic | 0:fbdae7e6d805 | 153 | return false; |
borlanic | 0:fbdae7e6d805 | 154 | } |
borlanic | 0:fbdae7e6d805 | 155 | } while (__STREXW(desiredValue, ptr)); |
borlanic | 0:fbdae7e6d805 | 156 | return true; |
borlanic | 0:fbdae7e6d805 | 157 | } |
borlanic | 0:fbdae7e6d805 | 158 | |
borlanic | 0:fbdae7e6d805 | 159 | uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta) |
borlanic | 0:fbdae7e6d805 | 160 | { |
borlanic | 0:fbdae7e6d805 | 161 | uint8_t newValue; |
borlanic | 0:fbdae7e6d805 | 162 | do { |
borlanic | 0:fbdae7e6d805 | 163 | newValue = __LDREXB(valuePtr) + delta; |
borlanic | 0:fbdae7e6d805 | 164 | } while (__STREXB(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 165 | return newValue; |
borlanic | 0:fbdae7e6d805 | 166 | } |
borlanic | 0:fbdae7e6d805 | 167 | |
borlanic | 0:fbdae7e6d805 | 168 | uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta) |
borlanic | 0:fbdae7e6d805 | 169 | { |
borlanic | 0:fbdae7e6d805 | 170 | uint16_t newValue; |
borlanic | 0:fbdae7e6d805 | 171 | do { |
borlanic | 0:fbdae7e6d805 | 172 | newValue = __LDREXH(valuePtr) + delta; |
borlanic | 0:fbdae7e6d805 | 173 | } while (__STREXH(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 174 | return newValue; |
borlanic | 0:fbdae7e6d805 | 175 | } |
borlanic | 0:fbdae7e6d805 | 176 | |
borlanic | 0:fbdae7e6d805 | 177 | uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta) |
borlanic | 0:fbdae7e6d805 | 178 | { |
borlanic | 0:fbdae7e6d805 | 179 | uint32_t newValue; |
borlanic | 0:fbdae7e6d805 | 180 | do { |
borlanic | 0:fbdae7e6d805 | 181 | newValue = __LDREXW(valuePtr) + delta; |
borlanic | 0:fbdae7e6d805 | 182 | } while (__STREXW(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 183 | return newValue; |
borlanic | 0:fbdae7e6d805 | 184 | } |
borlanic | 0:fbdae7e6d805 | 185 | |
borlanic | 0:fbdae7e6d805 | 186 | |
borlanic | 0:fbdae7e6d805 | 187 | uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta) |
borlanic | 0:fbdae7e6d805 | 188 | { |
borlanic | 0:fbdae7e6d805 | 189 | uint8_t newValue; |
borlanic | 0:fbdae7e6d805 | 190 | do { |
borlanic | 0:fbdae7e6d805 | 191 | newValue = __LDREXB(valuePtr) - delta; |
borlanic | 0:fbdae7e6d805 | 192 | } while (__STREXB(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 193 | return newValue; |
borlanic | 0:fbdae7e6d805 | 194 | } |
borlanic | 0:fbdae7e6d805 | 195 | |
borlanic | 0:fbdae7e6d805 | 196 | uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta) |
borlanic | 0:fbdae7e6d805 | 197 | { |
borlanic | 0:fbdae7e6d805 | 198 | uint16_t newValue; |
borlanic | 0:fbdae7e6d805 | 199 | do { |
borlanic | 0:fbdae7e6d805 | 200 | newValue = __LDREXH(valuePtr) - delta; |
borlanic | 0:fbdae7e6d805 | 201 | } while (__STREXH(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 202 | return newValue; |
borlanic | 0:fbdae7e6d805 | 203 | } |
borlanic | 0:fbdae7e6d805 | 204 | |
borlanic | 0:fbdae7e6d805 | 205 | uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta) |
borlanic | 0:fbdae7e6d805 | 206 | { |
borlanic | 0:fbdae7e6d805 | 207 | uint32_t newValue; |
borlanic | 0:fbdae7e6d805 | 208 | do { |
borlanic | 0:fbdae7e6d805 | 209 | newValue = __LDREXW(valuePtr) - delta; |
borlanic | 0:fbdae7e6d805 | 210 | } while (__STREXW(newValue, valuePtr)); |
borlanic | 0:fbdae7e6d805 | 211 | return newValue; |
borlanic | 0:fbdae7e6d805 | 212 | } |
borlanic | 0:fbdae7e6d805 | 213 | |
borlanic | 0:fbdae7e6d805 | 214 | #else |
borlanic | 0:fbdae7e6d805 | 215 | |
borlanic | 0:fbdae7e6d805 | 216 | bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 217 | { |
borlanic | 0:fbdae7e6d805 | 218 | bool success; |
borlanic | 0:fbdae7e6d805 | 219 | uint8_t currentValue; |
borlanic | 0:fbdae7e6d805 | 220 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 221 | currentValue = *ptr; |
borlanic | 0:fbdae7e6d805 | 222 | if (currentValue == *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 223 | *ptr = desiredValue; |
borlanic | 0:fbdae7e6d805 | 224 | success = true; |
borlanic | 0:fbdae7e6d805 | 225 | } else { |
borlanic | 0:fbdae7e6d805 | 226 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 227 | success = false; |
borlanic | 0:fbdae7e6d805 | 228 | } |
borlanic | 0:fbdae7e6d805 | 229 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 230 | return success; |
borlanic | 0:fbdae7e6d805 | 231 | } |
borlanic | 0:fbdae7e6d805 | 232 | |
borlanic | 0:fbdae7e6d805 | 233 | bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 234 | { |
borlanic | 0:fbdae7e6d805 | 235 | bool success; |
borlanic | 0:fbdae7e6d805 | 236 | uint16_t currentValue; |
borlanic | 0:fbdae7e6d805 | 237 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 238 | currentValue = *ptr; |
borlanic | 0:fbdae7e6d805 | 239 | if (currentValue == *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 240 | *ptr = desiredValue; |
borlanic | 0:fbdae7e6d805 | 241 | success = true; |
borlanic | 0:fbdae7e6d805 | 242 | } else { |
borlanic | 0:fbdae7e6d805 | 243 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 244 | success = false; |
borlanic | 0:fbdae7e6d805 | 245 | } |
borlanic | 0:fbdae7e6d805 | 246 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 247 | return success; |
borlanic | 0:fbdae7e6d805 | 248 | } |
borlanic | 0:fbdae7e6d805 | 249 | |
borlanic | 0:fbdae7e6d805 | 250 | |
borlanic | 0:fbdae7e6d805 | 251 | bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
borlanic | 0:fbdae7e6d805 | 252 | { |
borlanic | 0:fbdae7e6d805 | 253 | bool success; |
borlanic | 0:fbdae7e6d805 | 254 | uint32_t currentValue; |
borlanic | 0:fbdae7e6d805 | 255 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 256 | currentValue = *ptr; |
borlanic | 0:fbdae7e6d805 | 257 | if (currentValue == *expectedCurrentValue) { |
borlanic | 0:fbdae7e6d805 | 258 | *ptr = desiredValue; |
borlanic | 0:fbdae7e6d805 | 259 | success = true; |
borlanic | 0:fbdae7e6d805 | 260 | } else { |
borlanic | 0:fbdae7e6d805 | 261 | *expectedCurrentValue = currentValue; |
borlanic | 0:fbdae7e6d805 | 262 | success = false; |
borlanic | 0:fbdae7e6d805 | 263 | } |
borlanic | 0:fbdae7e6d805 | 264 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 265 | return success; |
borlanic | 0:fbdae7e6d805 | 266 | } |
borlanic | 0:fbdae7e6d805 | 267 | |
borlanic | 0:fbdae7e6d805 | 268 | |
borlanic | 0:fbdae7e6d805 | 269 | uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta) |
borlanic | 0:fbdae7e6d805 | 270 | { |
borlanic | 0:fbdae7e6d805 | 271 | uint8_t newValue; |
borlanic | 0:fbdae7e6d805 | 272 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 273 | newValue = *valuePtr + delta; |
borlanic | 0:fbdae7e6d805 | 274 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 275 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 276 | return newValue; |
borlanic | 0:fbdae7e6d805 | 277 | } |
borlanic | 0:fbdae7e6d805 | 278 | |
borlanic | 0:fbdae7e6d805 | 279 | uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta) |
borlanic | 0:fbdae7e6d805 | 280 | { |
borlanic | 0:fbdae7e6d805 | 281 | uint16_t newValue; |
borlanic | 0:fbdae7e6d805 | 282 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 283 | newValue = *valuePtr + delta; |
borlanic | 0:fbdae7e6d805 | 284 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 285 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 286 | return newValue; |
borlanic | 0:fbdae7e6d805 | 287 | } |
borlanic | 0:fbdae7e6d805 | 288 | |
borlanic | 0:fbdae7e6d805 | 289 | uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta) |
borlanic | 0:fbdae7e6d805 | 290 | { |
borlanic | 0:fbdae7e6d805 | 291 | uint32_t newValue; |
borlanic | 0:fbdae7e6d805 | 292 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 293 | newValue = *valuePtr + delta; |
borlanic | 0:fbdae7e6d805 | 294 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 295 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 296 | return newValue; |
borlanic | 0:fbdae7e6d805 | 297 | } |
borlanic | 0:fbdae7e6d805 | 298 | |
borlanic | 0:fbdae7e6d805 | 299 | |
borlanic | 0:fbdae7e6d805 | 300 | uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta) |
borlanic | 0:fbdae7e6d805 | 301 | { |
borlanic | 0:fbdae7e6d805 | 302 | uint8_t newValue; |
borlanic | 0:fbdae7e6d805 | 303 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 304 | newValue = *valuePtr - delta; |
borlanic | 0:fbdae7e6d805 | 305 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 306 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 307 | return newValue; |
borlanic | 0:fbdae7e6d805 | 308 | } |
borlanic | 0:fbdae7e6d805 | 309 | |
borlanic | 0:fbdae7e6d805 | 310 | uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta) |
borlanic | 0:fbdae7e6d805 | 311 | { |
borlanic | 0:fbdae7e6d805 | 312 | uint16_t newValue; |
borlanic | 0:fbdae7e6d805 | 313 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 314 | newValue = *valuePtr - delta; |
borlanic | 0:fbdae7e6d805 | 315 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 316 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 317 | return newValue; |
borlanic | 0:fbdae7e6d805 | 318 | } |
borlanic | 0:fbdae7e6d805 | 319 | |
borlanic | 0:fbdae7e6d805 | 320 | uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta) |
borlanic | 0:fbdae7e6d805 | 321 | { |
borlanic | 0:fbdae7e6d805 | 322 | uint32_t newValue; |
borlanic | 0:fbdae7e6d805 | 323 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 324 | newValue = *valuePtr - delta; |
borlanic | 0:fbdae7e6d805 | 325 | *valuePtr = newValue; |
borlanic | 0:fbdae7e6d805 | 326 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 327 | return newValue; |
borlanic | 0:fbdae7e6d805 | 328 | } |
borlanic | 0:fbdae7e6d805 | 329 | |
borlanic | 0:fbdae7e6d805 | 330 | #endif |
borlanic | 0:fbdae7e6d805 | 331 | |
borlanic | 0:fbdae7e6d805 | 332 | |
borlanic | 0:fbdae7e6d805 | 333 | bool core_util_atomic_cas_ptr(void * volatile *ptr, void **expectedCurrentValue, void *desiredValue) { |
borlanic | 0:fbdae7e6d805 | 334 | return core_util_atomic_cas_u32( |
borlanic | 0:fbdae7e6d805 | 335 | (volatile uint32_t *)ptr, |
borlanic | 0:fbdae7e6d805 | 336 | (uint32_t *)expectedCurrentValue, |
borlanic | 0:fbdae7e6d805 | 337 | (uint32_t)desiredValue); |
borlanic | 0:fbdae7e6d805 | 338 | } |
borlanic | 0:fbdae7e6d805 | 339 | |
borlanic | 0:fbdae7e6d805 | 340 | void *core_util_atomic_incr_ptr(void * volatile *valuePtr, ptrdiff_t delta) { |
borlanic | 0:fbdae7e6d805 | 341 | return (void *)core_util_atomic_incr_u32((volatile uint32_t *)valuePtr, (uint32_t)delta); |
borlanic | 0:fbdae7e6d805 | 342 | } |
borlanic | 0:fbdae7e6d805 | 343 | |
borlanic | 0:fbdae7e6d805 | 344 | void *core_util_atomic_decr_ptr(void * volatile *valuePtr, ptrdiff_t delta) { |
borlanic | 0:fbdae7e6d805 | 345 | return (void *)core_util_atomic_decr_u32((volatile uint32_t *)valuePtr, (uint32_t)delta); |
borlanic | 0:fbdae7e6d805 | 346 | } |
borlanic | 0:fbdae7e6d805 | 347 |