Includes library modifications to allow access to AIN_4 (AIN_0 / 5)
mbd_os/hal/common/mbed_critical.c@0:eafc3fd41f75, 2016-09-20 (annotated)
- Committer:
- bryantaylor
- Date:
- Tue Sep 20 21:26:12 2016 +0000
- Revision:
- 0:eafc3fd41f75
hackathon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bryantaylor | 0:eafc3fd41f75 | 1 | /* |
bryantaylor | 0:eafc3fd41f75 | 2 | * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved |
bryantaylor | 0:eafc3fd41f75 | 3 | * SPDX-License-Identifier: Apache-2.0 |
bryantaylor | 0:eafc3fd41f75 | 4 | * |
bryantaylor | 0:eafc3fd41f75 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
bryantaylor | 0:eafc3fd41f75 | 6 | * not use this file except in compliance with the License. |
bryantaylor | 0:eafc3fd41f75 | 7 | * You may obtain a copy of the License at |
bryantaylor | 0:eafc3fd41f75 | 8 | * |
bryantaylor | 0:eafc3fd41f75 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
bryantaylor | 0:eafc3fd41f75 | 10 | * |
bryantaylor | 0:eafc3fd41f75 | 11 | * Unless required by applicable law or agreed to in writing, software |
bryantaylor | 0:eafc3fd41f75 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
bryantaylor | 0:eafc3fd41f75 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bryantaylor | 0:eafc3fd41f75 | 14 | * See the License for the specific language governing permissions and |
bryantaylor | 0:eafc3fd41f75 | 15 | * limitations under the License. |
bryantaylor | 0:eafc3fd41f75 | 16 | */ |
bryantaylor | 0:eafc3fd41f75 | 17 | |
bryantaylor | 0:eafc3fd41f75 | 18 | #include "critical.h" |
bryantaylor | 0:eafc3fd41f75 | 19 | |
bryantaylor | 0:eafc3fd41f75 | 20 | #include "cmsis.h" |
bryantaylor | 0:eafc3fd41f75 | 21 | #include "mbed_assert.h" |
bryantaylor | 0:eafc3fd41f75 | 22 | |
bryantaylor | 0:eafc3fd41f75 | 23 | #define EXCLUSIVE_ACCESS (!defined (__CORTEX_M0) && !defined (__CORTEX_M0PLUS)) |
bryantaylor | 0:eafc3fd41f75 | 24 | |
bryantaylor | 0:eafc3fd41f75 | 25 | static volatile uint32_t interrupt_enable_counter = 0; |
bryantaylor | 0:eafc3fd41f75 | 26 | static volatile bool critical_interrupts_disabled = false; |
bryantaylor | 0:eafc3fd41f75 | 27 | |
bryantaylor | 0:eafc3fd41f75 | 28 | bool core_util_are_interrupts_enabled(void) |
bryantaylor | 0:eafc3fd41f75 | 29 | { |
bryantaylor | 0:eafc3fd41f75 | 30 | #if defined(__CORTEX_A9) |
bryantaylor | 0:eafc3fd41f75 | 31 | return ((__get_CPSR() & 0x80) == 0); |
bryantaylor | 0:eafc3fd41f75 | 32 | #else |
bryantaylor | 0:eafc3fd41f75 | 33 | return ((__get_PRIMASK() & 0x1) == 0); |
bryantaylor | 0:eafc3fd41f75 | 34 | #endif |
bryantaylor | 0:eafc3fd41f75 | 35 | } |
bryantaylor | 0:eafc3fd41f75 | 36 | |
bryantaylor | 0:eafc3fd41f75 | 37 | void core_util_critical_section_enter(void) |
bryantaylor | 0:eafc3fd41f75 | 38 | { |
bryantaylor | 0:eafc3fd41f75 | 39 | bool interrupts_disabled = !core_util_are_interrupts_enabled(); |
bryantaylor | 0:eafc3fd41f75 | 40 | __disable_irq(); |
bryantaylor | 0:eafc3fd41f75 | 41 | |
bryantaylor | 0:eafc3fd41f75 | 42 | /* Save the interrupt disabled state as it was prior to any nested critical section lock use */ |
bryantaylor | 0:eafc3fd41f75 | 43 | if (!interrupt_enable_counter) { |
bryantaylor | 0:eafc3fd41f75 | 44 | critical_interrupts_disabled = interrupts_disabled; |
bryantaylor | 0:eafc3fd41f75 | 45 | } |
bryantaylor | 0:eafc3fd41f75 | 46 | |
bryantaylor | 0:eafc3fd41f75 | 47 | /* If the interrupt_enable_counter overflows or we are in a nested critical section and interrupts |
bryantaylor | 0:eafc3fd41f75 | 48 | are enabled, then something has gone badly wrong thus assert an error. |
bryantaylor | 0:eafc3fd41f75 | 49 | */ |
bryantaylor | 0:eafc3fd41f75 | 50 | MBED_ASSERT(interrupt_enable_counter < UINT32_MAX); |
bryantaylor | 0:eafc3fd41f75 | 51 | // FIXME |
bryantaylor | 0:eafc3fd41f75 | 52 | #ifndef FEATURE_UVISOR |
bryantaylor | 0:eafc3fd41f75 | 53 | if (interrupt_enable_counter > 0) { |
bryantaylor | 0:eafc3fd41f75 | 54 | MBED_ASSERT(interrupts_disabled); |
bryantaylor | 0:eafc3fd41f75 | 55 | } |
bryantaylor | 0:eafc3fd41f75 | 56 | #else |
bryantaylor | 0:eafc3fd41f75 | 57 | #warning "core_util_critical_section_enter needs fixing to work from unprivileged code" |
bryantaylor | 0:eafc3fd41f75 | 58 | #endif /* FEATURE_UVISOR */ |
bryantaylor | 0:eafc3fd41f75 | 59 | interrupt_enable_counter++; |
bryantaylor | 0:eafc3fd41f75 | 60 | } |
bryantaylor | 0:eafc3fd41f75 | 61 | |
bryantaylor | 0:eafc3fd41f75 | 62 | void core_util_critical_section_exit(void) |
bryantaylor | 0:eafc3fd41f75 | 63 | { |
bryantaylor | 0:eafc3fd41f75 | 64 | /* If critical_section_enter has not previously been called, do nothing */ |
bryantaylor | 0:eafc3fd41f75 | 65 | if (interrupt_enable_counter) { |
bryantaylor | 0:eafc3fd41f75 | 66 | |
bryantaylor | 0:eafc3fd41f75 | 67 | // FIXME |
bryantaylor | 0:eafc3fd41f75 | 68 | #ifndef FEATURE_UVISOR |
bryantaylor | 0:eafc3fd41f75 | 69 | bool interrupts_disabled = !core_util_are_interrupts_enabled(); /* get the current interrupt disabled state */ |
bryantaylor | 0:eafc3fd41f75 | 70 | |
bryantaylor | 0:eafc3fd41f75 | 71 | MBED_ASSERT(interrupts_disabled); /* Interrupts must be disabled on invoking an exit from a critical section */ |
bryantaylor | 0:eafc3fd41f75 | 72 | #else |
bryantaylor | 0:eafc3fd41f75 | 73 | #warning "core_util_critical_section_exit needs fixing to work from unprivileged code" |
bryantaylor | 0:eafc3fd41f75 | 74 | #endif /* FEATURE_UVISOR */ |
bryantaylor | 0:eafc3fd41f75 | 75 | |
bryantaylor | 0:eafc3fd41f75 | 76 | interrupt_enable_counter--; |
bryantaylor | 0:eafc3fd41f75 | 77 | |
bryantaylor | 0:eafc3fd41f75 | 78 | /* Only re-enable interrupts if we are exiting the last of the nested critical sections and |
bryantaylor | 0:eafc3fd41f75 | 79 | interrupts were enabled on entry to the first critical section. |
bryantaylor | 0:eafc3fd41f75 | 80 | */ |
bryantaylor | 0:eafc3fd41f75 | 81 | if (!interrupt_enable_counter && !critical_interrupts_disabled) { |
bryantaylor | 0:eafc3fd41f75 | 82 | __enable_irq(); |
bryantaylor | 0:eafc3fd41f75 | 83 | } |
bryantaylor | 0:eafc3fd41f75 | 84 | } |
bryantaylor | 0:eafc3fd41f75 | 85 | } |
bryantaylor | 0:eafc3fd41f75 | 86 | |
bryantaylor | 0:eafc3fd41f75 | 87 | #if EXCLUSIVE_ACCESS |
bryantaylor | 0:eafc3fd41f75 | 88 | |
bryantaylor | 0:eafc3fd41f75 | 89 | /* Supress __ldrex and __strex deprecated warnings - "#3731-D: intrinsic is deprecated" */ |
bryantaylor | 0:eafc3fd41f75 | 90 | #if defined (__CC_ARM) |
bryantaylor | 0:eafc3fd41f75 | 91 | #pragma diag_suppress 3731 |
bryantaylor | 0:eafc3fd41f75 | 92 | #endif |
bryantaylor | 0:eafc3fd41f75 | 93 | |
bryantaylor | 0:eafc3fd41f75 | 94 | bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 95 | { |
bryantaylor | 0:eafc3fd41f75 | 96 | uint8_t currentValue = __LDREXB((volatile uint8_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 97 | if (currentValue != *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 98 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 99 | __CLREX(); |
bryantaylor | 0:eafc3fd41f75 | 100 | return false; |
bryantaylor | 0:eafc3fd41f75 | 101 | } |
bryantaylor | 0:eafc3fd41f75 | 102 | |
bryantaylor | 0:eafc3fd41f75 | 103 | return !__STREXB(desiredValue, (volatile uint8_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 104 | } |
bryantaylor | 0:eafc3fd41f75 | 105 | |
bryantaylor | 0:eafc3fd41f75 | 106 | bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 107 | { |
bryantaylor | 0:eafc3fd41f75 | 108 | uint16_t currentValue = __LDREXH((volatile uint16_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 109 | if (currentValue != *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 110 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 111 | __CLREX(); |
bryantaylor | 0:eafc3fd41f75 | 112 | return false; |
bryantaylor | 0:eafc3fd41f75 | 113 | } |
bryantaylor | 0:eafc3fd41f75 | 114 | |
bryantaylor | 0:eafc3fd41f75 | 115 | return !__STREXH(desiredValue, (volatile uint16_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 116 | } |
bryantaylor | 0:eafc3fd41f75 | 117 | |
bryantaylor | 0:eafc3fd41f75 | 118 | |
bryantaylor | 0:eafc3fd41f75 | 119 | bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 120 | { |
bryantaylor | 0:eafc3fd41f75 | 121 | uint32_t currentValue = __LDREXW((volatile uint32_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 122 | if (currentValue != *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 123 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 124 | __CLREX(); |
bryantaylor | 0:eafc3fd41f75 | 125 | return false; |
bryantaylor | 0:eafc3fd41f75 | 126 | } |
bryantaylor | 0:eafc3fd41f75 | 127 | |
bryantaylor | 0:eafc3fd41f75 | 128 | return !__STREXW(desiredValue, (volatile uint32_t*)ptr); |
bryantaylor | 0:eafc3fd41f75 | 129 | } |
bryantaylor | 0:eafc3fd41f75 | 130 | |
bryantaylor | 0:eafc3fd41f75 | 131 | uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta) |
bryantaylor | 0:eafc3fd41f75 | 132 | { |
bryantaylor | 0:eafc3fd41f75 | 133 | uint8_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 134 | do { |
bryantaylor | 0:eafc3fd41f75 | 135 | newValue = __LDREXB((volatile uint8_t*)valuePtr) + delta; |
bryantaylor | 0:eafc3fd41f75 | 136 | } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 137 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 138 | } |
bryantaylor | 0:eafc3fd41f75 | 139 | |
bryantaylor | 0:eafc3fd41f75 | 140 | uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta) |
bryantaylor | 0:eafc3fd41f75 | 141 | { |
bryantaylor | 0:eafc3fd41f75 | 142 | uint16_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 143 | do { |
bryantaylor | 0:eafc3fd41f75 | 144 | newValue = __LDREXH((volatile uint16_t*)valuePtr) + delta; |
bryantaylor | 0:eafc3fd41f75 | 145 | } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 146 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 147 | } |
bryantaylor | 0:eafc3fd41f75 | 148 | |
bryantaylor | 0:eafc3fd41f75 | 149 | uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta) |
bryantaylor | 0:eafc3fd41f75 | 150 | { |
bryantaylor | 0:eafc3fd41f75 | 151 | uint32_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 152 | do { |
bryantaylor | 0:eafc3fd41f75 | 153 | newValue = __LDREXW((volatile uint32_t*)valuePtr) + delta; |
bryantaylor | 0:eafc3fd41f75 | 154 | } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 155 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 156 | } |
bryantaylor | 0:eafc3fd41f75 | 157 | |
bryantaylor | 0:eafc3fd41f75 | 158 | |
bryantaylor | 0:eafc3fd41f75 | 159 | uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta) |
bryantaylor | 0:eafc3fd41f75 | 160 | { |
bryantaylor | 0:eafc3fd41f75 | 161 | uint8_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 162 | do { |
bryantaylor | 0:eafc3fd41f75 | 163 | newValue = __LDREXB((volatile uint8_t*)valuePtr) - delta; |
bryantaylor | 0:eafc3fd41f75 | 164 | } while (__STREXB(newValue, (volatile uint8_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 165 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 166 | } |
bryantaylor | 0:eafc3fd41f75 | 167 | |
bryantaylor | 0:eafc3fd41f75 | 168 | uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta) |
bryantaylor | 0:eafc3fd41f75 | 169 | { |
bryantaylor | 0:eafc3fd41f75 | 170 | uint16_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 171 | do { |
bryantaylor | 0:eafc3fd41f75 | 172 | newValue = __LDREXH((volatile uint16_t*)valuePtr) - delta; |
bryantaylor | 0:eafc3fd41f75 | 173 | } while (__STREXH(newValue, (volatile uint16_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 174 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 175 | } |
bryantaylor | 0:eafc3fd41f75 | 176 | |
bryantaylor | 0:eafc3fd41f75 | 177 | uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta) |
bryantaylor | 0:eafc3fd41f75 | 178 | { |
bryantaylor | 0:eafc3fd41f75 | 179 | uint32_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 180 | do { |
bryantaylor | 0:eafc3fd41f75 | 181 | newValue = __LDREXW((volatile uint32_t*)valuePtr) - delta; |
bryantaylor | 0:eafc3fd41f75 | 182 | } while (__STREXW(newValue, (volatile uint32_t*)valuePtr)); |
bryantaylor | 0:eafc3fd41f75 | 183 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 184 | } |
bryantaylor | 0:eafc3fd41f75 | 185 | |
bryantaylor | 0:eafc3fd41f75 | 186 | #else |
bryantaylor | 0:eafc3fd41f75 | 187 | |
bryantaylor | 0:eafc3fd41f75 | 188 | bool core_util_atomic_cas_u8(uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 189 | { |
bryantaylor | 0:eafc3fd41f75 | 190 | bool success; |
bryantaylor | 0:eafc3fd41f75 | 191 | uint8_t currentValue; |
bryantaylor | 0:eafc3fd41f75 | 192 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 193 | currentValue = *ptr; |
bryantaylor | 0:eafc3fd41f75 | 194 | if (currentValue == *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 195 | *ptr = desiredValue; |
bryantaylor | 0:eafc3fd41f75 | 196 | success = true; |
bryantaylor | 0:eafc3fd41f75 | 197 | } else { |
bryantaylor | 0:eafc3fd41f75 | 198 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 199 | success = false; |
bryantaylor | 0:eafc3fd41f75 | 200 | } |
bryantaylor | 0:eafc3fd41f75 | 201 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 202 | return success; |
bryantaylor | 0:eafc3fd41f75 | 203 | } |
bryantaylor | 0:eafc3fd41f75 | 204 | |
bryantaylor | 0:eafc3fd41f75 | 205 | bool core_util_atomic_cas_u16(uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 206 | { |
bryantaylor | 0:eafc3fd41f75 | 207 | bool success; |
bryantaylor | 0:eafc3fd41f75 | 208 | uint16_t currentValue; |
bryantaylor | 0:eafc3fd41f75 | 209 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 210 | currentValue = *ptr; |
bryantaylor | 0:eafc3fd41f75 | 211 | if (currentValue == *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 212 | *ptr = desiredValue; |
bryantaylor | 0:eafc3fd41f75 | 213 | success = true; |
bryantaylor | 0:eafc3fd41f75 | 214 | } else { |
bryantaylor | 0:eafc3fd41f75 | 215 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 216 | success = false; |
bryantaylor | 0:eafc3fd41f75 | 217 | } |
bryantaylor | 0:eafc3fd41f75 | 218 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 219 | return success; |
bryantaylor | 0:eafc3fd41f75 | 220 | } |
bryantaylor | 0:eafc3fd41f75 | 221 | |
bryantaylor | 0:eafc3fd41f75 | 222 | |
bryantaylor | 0:eafc3fd41f75 | 223 | bool core_util_atomic_cas_u32(uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue) |
bryantaylor | 0:eafc3fd41f75 | 224 | { |
bryantaylor | 0:eafc3fd41f75 | 225 | bool success; |
bryantaylor | 0:eafc3fd41f75 | 226 | uint32_t currentValue; |
bryantaylor | 0:eafc3fd41f75 | 227 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 228 | currentValue = *ptr; |
bryantaylor | 0:eafc3fd41f75 | 229 | if (currentValue == *expectedCurrentValue) { |
bryantaylor | 0:eafc3fd41f75 | 230 | *ptr = desiredValue; |
bryantaylor | 0:eafc3fd41f75 | 231 | success = true; |
bryantaylor | 0:eafc3fd41f75 | 232 | } else { |
bryantaylor | 0:eafc3fd41f75 | 233 | *expectedCurrentValue = currentValue; |
bryantaylor | 0:eafc3fd41f75 | 234 | success = false; |
bryantaylor | 0:eafc3fd41f75 | 235 | } |
bryantaylor | 0:eafc3fd41f75 | 236 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 237 | return success; |
bryantaylor | 0:eafc3fd41f75 | 238 | } |
bryantaylor | 0:eafc3fd41f75 | 239 | |
bryantaylor | 0:eafc3fd41f75 | 240 | |
bryantaylor | 0:eafc3fd41f75 | 241 | uint8_t core_util_atomic_incr_u8(uint8_t *valuePtr, uint8_t delta) |
bryantaylor | 0:eafc3fd41f75 | 242 | { |
bryantaylor | 0:eafc3fd41f75 | 243 | uint8_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 244 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 245 | newValue = *valuePtr + delta; |
bryantaylor | 0:eafc3fd41f75 | 246 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 247 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 248 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 249 | } |
bryantaylor | 0:eafc3fd41f75 | 250 | |
bryantaylor | 0:eafc3fd41f75 | 251 | uint16_t core_util_atomic_incr_u16(uint16_t *valuePtr, uint16_t delta) |
bryantaylor | 0:eafc3fd41f75 | 252 | { |
bryantaylor | 0:eafc3fd41f75 | 253 | uint16_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 254 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 255 | newValue = *valuePtr + delta; |
bryantaylor | 0:eafc3fd41f75 | 256 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 257 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 258 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 259 | } |
bryantaylor | 0:eafc3fd41f75 | 260 | |
bryantaylor | 0:eafc3fd41f75 | 261 | uint32_t core_util_atomic_incr_u32(uint32_t *valuePtr, uint32_t delta) |
bryantaylor | 0:eafc3fd41f75 | 262 | { |
bryantaylor | 0:eafc3fd41f75 | 263 | uint32_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 264 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 265 | newValue = *valuePtr + delta; |
bryantaylor | 0:eafc3fd41f75 | 266 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 267 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 268 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 269 | } |
bryantaylor | 0:eafc3fd41f75 | 270 | |
bryantaylor | 0:eafc3fd41f75 | 271 | |
bryantaylor | 0:eafc3fd41f75 | 272 | uint8_t core_util_atomic_decr_u8(uint8_t *valuePtr, uint8_t delta) |
bryantaylor | 0:eafc3fd41f75 | 273 | { |
bryantaylor | 0:eafc3fd41f75 | 274 | uint8_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 275 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 276 | newValue = *valuePtr - delta; |
bryantaylor | 0:eafc3fd41f75 | 277 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 278 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 279 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 280 | } |
bryantaylor | 0:eafc3fd41f75 | 281 | |
bryantaylor | 0:eafc3fd41f75 | 282 | uint16_t core_util_atomic_decr_u16(uint16_t *valuePtr, uint16_t delta) |
bryantaylor | 0:eafc3fd41f75 | 283 | { |
bryantaylor | 0:eafc3fd41f75 | 284 | uint16_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 285 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 286 | newValue = *valuePtr - delta; |
bryantaylor | 0:eafc3fd41f75 | 287 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 288 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 289 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 290 | } |
bryantaylor | 0:eafc3fd41f75 | 291 | |
bryantaylor | 0:eafc3fd41f75 | 292 | uint32_t core_util_atomic_decr_u32(uint32_t *valuePtr, uint32_t delta) |
bryantaylor | 0:eafc3fd41f75 | 293 | { |
bryantaylor | 0:eafc3fd41f75 | 294 | uint32_t newValue; |
bryantaylor | 0:eafc3fd41f75 | 295 | core_util_critical_section_enter(); |
bryantaylor | 0:eafc3fd41f75 | 296 | newValue = *valuePtr - delta; |
bryantaylor | 0:eafc3fd41f75 | 297 | *valuePtr = newValue; |
bryantaylor | 0:eafc3fd41f75 | 298 | core_util_critical_section_exit(); |
bryantaylor | 0:eafc3fd41f75 | 299 | return newValue; |
bryantaylor | 0:eafc3fd41f75 | 300 | } |
bryantaylor | 0:eafc3fd41f75 | 301 | |
bryantaylor | 0:eafc3fd41f75 | 302 | #endif |
bryantaylor | 0:eafc3fd41f75 | 303 | |
bryantaylor | 0:eafc3fd41f75 | 304 | |
bryantaylor | 0:eafc3fd41f75 | 305 | bool core_util_atomic_cas_ptr(void **ptr, void **expectedCurrentValue, void *desiredValue) { |
bryantaylor | 0:eafc3fd41f75 | 306 | return core_util_atomic_cas_u32( |
bryantaylor | 0:eafc3fd41f75 | 307 | (uint32_t *)ptr, |
bryantaylor | 0:eafc3fd41f75 | 308 | (uint32_t *)expectedCurrentValue, |
bryantaylor | 0:eafc3fd41f75 | 309 | (uint32_t)desiredValue); |
bryantaylor | 0:eafc3fd41f75 | 310 | } |
bryantaylor | 0:eafc3fd41f75 | 311 | |
bryantaylor | 0:eafc3fd41f75 | 312 | void *core_util_atomic_incr_ptr(void **valuePtr, ptrdiff_t delta) { |
bryantaylor | 0:eafc3fd41f75 | 313 | return (void *)core_util_atomic_incr_u32((uint32_t *)valuePtr, (uint32_t)delta); |
bryantaylor | 0:eafc3fd41f75 | 314 | } |
bryantaylor | 0:eafc3fd41f75 | 315 | |
bryantaylor | 0:eafc3fd41f75 | 316 | void *core_util_atomic_decr_ptr(void **valuePtr, ptrdiff_t delta) { |
bryantaylor | 0:eafc3fd41f75 | 317 | return (void *)core_util_atomic_decr_u32((uint32_t *)valuePtr, (uint32_t)delta); |
bryantaylor | 0:eafc3fd41f75 | 318 | } |
bryantaylor | 0:eafc3fd41f75 | 319 |