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