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