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