Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Thu Oct 11 02:27:46 2018 +0000
Revision:
3:f3764f852aa8
Parent:
0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1
kadonotakashi 0:8fdf9a60065b 2 /*
kadonotakashi 0:8fdf9a60065b 3 * Copyright (c) 2015-2016, ARM Limited, All Rights Reserved
kadonotakashi 0:8fdf9a60065b 4 * SPDX-License-Identifier: Apache-2.0
kadonotakashi 0:8fdf9a60065b 5 *
kadonotakashi 0:8fdf9a60065b 6 * Licensed under the Apache License, Version 2.0 (the "License"); you may
kadonotakashi 0:8fdf9a60065b 7 * not use this file except in compliance with the License.
kadonotakashi 0:8fdf9a60065b 8 * You may obtain a copy of the License at
kadonotakashi 0:8fdf9a60065b 9 *
kadonotakashi 0:8fdf9a60065b 10 * http://www.apache.org/licenses/LICENSE-2.0
kadonotakashi 0:8fdf9a60065b 11 *
kadonotakashi 0:8fdf9a60065b 12 * Unless required by applicable law or agreed to in writing, software
kadonotakashi 0:8fdf9a60065b 13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
kadonotakashi 0:8fdf9a60065b 14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kadonotakashi 0:8fdf9a60065b 15 * See the License for the specific language governing permissions and
kadonotakashi 0:8fdf9a60065b 16 * limitations under the License.
kadonotakashi 0:8fdf9a60065b 17 */
kadonotakashi 0:8fdf9a60065b 18
kadonotakashi 0:8fdf9a60065b 19 #ifndef __MBED_UTIL_CRITICAL_H__
kadonotakashi 0:8fdf9a60065b 20 #define __MBED_UTIL_CRITICAL_H__
kadonotakashi 0:8fdf9a60065b 21
kadonotakashi 0:8fdf9a60065b 22 #include <stdbool.h>
kadonotakashi 0:8fdf9a60065b 23 #include <stdint.h>
kadonotakashi 0:8fdf9a60065b 24 #include <stddef.h>
kadonotakashi 0:8fdf9a60065b 25
kadonotakashi 0:8fdf9a60065b 26 #ifdef __cplusplus
kadonotakashi 0:8fdf9a60065b 27 extern "C" {
kadonotakashi 0:8fdf9a60065b 28 #endif
kadonotakashi 0:8fdf9a60065b 29
kadonotakashi 0:8fdf9a60065b 30 /** \addtogroup platform */
kadonotakashi 0:8fdf9a60065b 31 /** @{*/
kadonotakashi 0:8fdf9a60065b 32 /**
kadonotakashi 0:8fdf9a60065b 33 * \defgroup platform_critical critical section function
kadonotakashi 0:8fdf9a60065b 34 * @{
kadonotakashi 0:8fdf9a60065b 35 */
kadonotakashi 0:8fdf9a60065b 36
kadonotakashi 0:8fdf9a60065b 37 /** Determine the current interrupts enabled state
kadonotakashi 0:8fdf9a60065b 38 *
kadonotakashi 0:8fdf9a60065b 39 * This function can be called to determine whether or not interrupts are currently enabled.
kadonotakashi 0:8fdf9a60065b 40 * @note
kadonotakashi 0:8fdf9a60065b 41 * NOTE:
kadonotakashi 0:8fdf9a60065b 42 * This function works for both cortex-A and cortex-M, although the underlyng implementation
kadonotakashi 0:8fdf9a60065b 43 * differs.
kadonotakashi 0:8fdf9a60065b 44 * @return true if interrupts are enabled, false otherwise
kadonotakashi 0:8fdf9a60065b 45 */
kadonotakashi 0:8fdf9a60065b 46 bool core_util_are_interrupts_enabled(void);
kadonotakashi 0:8fdf9a60065b 47
kadonotakashi 0:8fdf9a60065b 48 /** Determine if this code is executing from an interrupt
kadonotakashi 0:8fdf9a60065b 49 *
kadonotakashi 0:8fdf9a60065b 50 * This function can be called to determine if the code is running on interrupt context.
kadonotakashi 0:8fdf9a60065b 51 * @note
kadonotakashi 0:8fdf9a60065b 52 * NOTE:
kadonotakashi 0:8fdf9a60065b 53 * This function works for both cortex-A and cortex-M, although the underlyng implementation
kadonotakashi 0:8fdf9a60065b 54 * differs.
kadonotakashi 0:8fdf9a60065b 55 * @return true if in an isr, false otherwise
kadonotakashi 0:8fdf9a60065b 56 */
kadonotakashi 0:8fdf9a60065b 57 bool core_util_is_isr_active(void);
kadonotakashi 0:8fdf9a60065b 58
kadonotakashi 0:8fdf9a60065b 59 /** Mark the start of a critical section
kadonotakashi 0:8fdf9a60065b 60 *
kadonotakashi 0:8fdf9a60065b 61 * This function should be called to mark the start of a critical section of code.
kadonotakashi 0:8fdf9a60065b 62 * @note
kadonotakashi 0:8fdf9a60065b 63 * NOTES:
kadonotakashi 0:8fdf9a60065b 64 * 1) The use of this style of critical section is targetted at C based implementations.
kadonotakashi 0:8fdf9a60065b 65 * 2) These critical sections can be nested.
kadonotakashi 0:8fdf9a60065b 66 * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
kadonotakashi 0:8fdf9a60065b 67 * section) will be preserved on exit from the section.
kadonotakashi 0:8fdf9a60065b 68 * 4) This implementation will currently only work on code running in privileged mode.
kadonotakashi 0:8fdf9a60065b 69 */
kadonotakashi 0:8fdf9a60065b 70 void core_util_critical_section_enter(void);
kadonotakashi 0:8fdf9a60065b 71
kadonotakashi 0:8fdf9a60065b 72 /** Mark the end of a critical section
kadonotakashi 0:8fdf9a60065b 73 *
kadonotakashi 0:8fdf9a60065b 74 * This function should be called to mark the end of a critical section of code.
kadonotakashi 0:8fdf9a60065b 75 * @note
kadonotakashi 0:8fdf9a60065b 76 * NOTES:
kadonotakashi 0:8fdf9a60065b 77 * 1) The use of this style of critical section is targetted at C based implementations.
kadonotakashi 0:8fdf9a60065b 78 * 2) These critical sections can be nested.
kadonotakashi 0:8fdf9a60065b 79 * 3) The interrupt enable state on entry to the first critical section (of a nested set, or single
kadonotakashi 0:8fdf9a60065b 80 * section) will be preserved on exit from the section.
kadonotakashi 0:8fdf9a60065b 81 * 4) This implementation will currently only work on code running in privileged mode.
kadonotakashi 0:8fdf9a60065b 82 */
kadonotakashi 0:8fdf9a60065b 83 void core_util_critical_section_exit(void);
kadonotakashi 0:8fdf9a60065b 84
kadonotakashi 0:8fdf9a60065b 85 /**
kadonotakashi 0:8fdf9a60065b 86 * Determine if we are currently in a critical section
kadonotakashi 0:8fdf9a60065b 87 *
kadonotakashi 0:8fdf9a60065b 88 * @return true if in a critical section, false otherwise.
kadonotakashi 0:8fdf9a60065b 89 */
kadonotakashi 0:8fdf9a60065b 90 bool core_util_in_critical_section(void);
kadonotakashi 0:8fdf9a60065b 91
kadonotakashi 0:8fdf9a60065b 92 /**
kadonotakashi 0:8fdf9a60065b 93 * Atomic compare and set. It compares the contents of a memory location to a
kadonotakashi 0:8fdf9a60065b 94 * given value and, only if they are the same, modifies the contents of that
kadonotakashi 0:8fdf9a60065b 95 * memory location to a given new value. This is done as a single atomic
kadonotakashi 0:8fdf9a60065b 96 * operation. The atomicity guarantees that the new value is calculated based on
kadonotakashi 0:8fdf9a60065b 97 * up-to-date information; if the value had been updated by another thread in
kadonotakashi 0:8fdf9a60065b 98 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
kadonotakashi 0:8fdf9a60065b 99 *
kadonotakashi 0:8fdf9a60065b 100 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
kadonotakashi 0:8fdf9a60065b 101 * you to the article on compare-and swap].
kadonotakashi 0:8fdf9a60065b 102 *
kadonotakashi 0:8fdf9a60065b 103 * @param ptr The target memory location.
kadonotakashi 0:8fdf9a60065b 104 * @param[in,out] expectedCurrentValue A pointer to some location holding the
kadonotakashi 0:8fdf9a60065b 105 * expected current value of the data being set atomically.
kadonotakashi 0:8fdf9a60065b 106 * The computed 'desiredValue' should be a function of this current value.
kadonotakashi 0:8fdf9a60065b 107 * @note: This is an in-out parameter. In the
kadonotakashi 0:8fdf9a60065b 108 * failure case of atomic_cas (where the
kadonotakashi 0:8fdf9a60065b 109 * destination isn't set), the pointee of expectedCurrentValue is
kadonotakashi 0:8fdf9a60065b 110 * updated with the current value.
kadonotakashi 0:8fdf9a60065b 111 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
kadonotakashi 0:8fdf9a60065b 112 *
kadonotakashi 0:8fdf9a60065b 113 * @return true if the memory location was atomically
kadonotakashi 0:8fdf9a60065b 114 * updated with the desired value (after verifying
kadonotakashi 0:8fdf9a60065b 115 * that it contained the expectedCurrentValue),
kadonotakashi 0:8fdf9a60065b 116 * false otherwise. In the failure case,
kadonotakashi 0:8fdf9a60065b 117 * exepctedCurrentValue is updated with the new
kadonotakashi 0:8fdf9a60065b 118 * value of the target memory location.
kadonotakashi 0:8fdf9a60065b 119 *
kadonotakashi 0:8fdf9a60065b 120 * pseudocode:
kadonotakashi 0:8fdf9a60065b 121 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
kadonotakashi 0:8fdf9a60065b 122 * if *p != *old {
kadonotakashi 0:8fdf9a60065b 123 * *old = *p
kadonotakashi 0:8fdf9a60065b 124 * return false
kadonotakashi 0:8fdf9a60065b 125 * }
kadonotakashi 0:8fdf9a60065b 126 * *p = new
kadonotakashi 0:8fdf9a60065b 127 * return true
kadonotakashi 0:8fdf9a60065b 128 * }
kadonotakashi 0:8fdf9a60065b 129 *
kadonotakashi 0:8fdf9a60065b 130 * @note: In the failure case (where the destination isn't set), the value
kadonotakashi 0:8fdf9a60065b 131 * pointed to by expectedCurrentValue is instead updated with the current value.
kadonotakashi 0:8fdf9a60065b 132 * This property helps writing concise code for the following incr:
kadonotakashi 0:8fdf9a60065b 133 *
kadonotakashi 0:8fdf9a60065b 134 * function incr(p : pointer to int, a : int) returns int {
kadonotakashi 0:8fdf9a60065b 135 * done = false
kadonotakashi 0:8fdf9a60065b 136 * value = *p // This fetch operation need not be atomic.
kadonotakashi 0:8fdf9a60065b 137 * while not done {
kadonotakashi 0:8fdf9a60065b 138 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
kadonotakashi 0:8fdf9a60065b 139 * }
kadonotakashi 0:8fdf9a60065b 140 * return value + a
kadonotakashi 0:8fdf9a60065b 141 * }
kadonotakashi 0:8fdf9a60065b 142 *
kadonotakashi 0:8fdf9a60065b 143 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
kadonotakashi 0:8fdf9a60065b 144 * always succeeds if the current value is expected, as per the pseudocode
kadonotakashi 0:8fdf9a60065b 145 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
kadonotakashi 0:8fdf9a60065b 146 */
kadonotakashi 0:8fdf9a60065b 147 bool core_util_atomic_cas_u8(volatile uint8_t *ptr, uint8_t *expectedCurrentValue, uint8_t desiredValue);
kadonotakashi 0:8fdf9a60065b 148
kadonotakashi 0:8fdf9a60065b 149 /**
kadonotakashi 0:8fdf9a60065b 150 * Atomic compare and set. It compares the contents of a memory location to a
kadonotakashi 0:8fdf9a60065b 151 * given value and, only if they are the same, modifies the contents of that
kadonotakashi 0:8fdf9a60065b 152 * memory location to a given new value. This is done as a single atomic
kadonotakashi 0:8fdf9a60065b 153 * operation. The atomicity guarantees that the new value is calculated based on
kadonotakashi 0:8fdf9a60065b 154 * up-to-date information; if the value had been updated by another thread in
kadonotakashi 0:8fdf9a60065b 155 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
kadonotakashi 0:8fdf9a60065b 156 *
kadonotakashi 0:8fdf9a60065b 157 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
kadonotakashi 0:8fdf9a60065b 158 * you to the article on compare-and swap].
kadonotakashi 0:8fdf9a60065b 159 *
kadonotakashi 0:8fdf9a60065b 160 * @param ptr The target memory location.
kadonotakashi 0:8fdf9a60065b 161 * @param[in,out] expectedCurrentValue A pointer to some location holding the
kadonotakashi 0:8fdf9a60065b 162 * expected current value of the data being set atomically.
kadonotakashi 0:8fdf9a60065b 163 * The computed 'desiredValue' should be a function of this current value.
kadonotakashi 0:8fdf9a60065b 164 * @note: This is an in-out parameter. In the
kadonotakashi 0:8fdf9a60065b 165 * failure case of atomic_cas (where the
kadonotakashi 0:8fdf9a60065b 166 * destination isn't set), the pointee of expectedCurrentValue is
kadonotakashi 0:8fdf9a60065b 167 * updated with the current value.
kadonotakashi 0:8fdf9a60065b 168 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
kadonotakashi 0:8fdf9a60065b 169 *
kadonotakashi 0:8fdf9a60065b 170 * @return true if the memory location was atomically
kadonotakashi 0:8fdf9a60065b 171 * updated with the desired value (after verifying
kadonotakashi 0:8fdf9a60065b 172 * that it contained the expectedCurrentValue),
kadonotakashi 0:8fdf9a60065b 173 * false otherwise. In the failure case,
kadonotakashi 0:8fdf9a60065b 174 * exepctedCurrentValue is updated with the new
kadonotakashi 0:8fdf9a60065b 175 * value of the target memory location.
kadonotakashi 0:8fdf9a60065b 176 *
kadonotakashi 0:8fdf9a60065b 177 * pseudocode:
kadonotakashi 0:8fdf9a60065b 178 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
kadonotakashi 0:8fdf9a60065b 179 * if *p != *old {
kadonotakashi 0:8fdf9a60065b 180 * *old = *p
kadonotakashi 0:8fdf9a60065b 181 * return false
kadonotakashi 0:8fdf9a60065b 182 * }
kadonotakashi 0:8fdf9a60065b 183 * *p = new
kadonotakashi 0:8fdf9a60065b 184 * return true
kadonotakashi 0:8fdf9a60065b 185 * }
kadonotakashi 0:8fdf9a60065b 186 *
kadonotakashi 0:8fdf9a60065b 187 * @note: In the failure case (where the destination isn't set), the value
kadonotakashi 0:8fdf9a60065b 188 * pointed to by expectedCurrentValue is instead updated with the current value.
kadonotakashi 0:8fdf9a60065b 189 * This property helps writing concise code for the following incr:
kadonotakashi 0:8fdf9a60065b 190 *
kadonotakashi 0:8fdf9a60065b 191 * function incr(p : pointer to int, a : int) returns int {
kadonotakashi 0:8fdf9a60065b 192 * done = false
kadonotakashi 0:8fdf9a60065b 193 * value = *p // This fetch operation need not be atomic.
kadonotakashi 0:8fdf9a60065b 194 * while not done {
kadonotakashi 0:8fdf9a60065b 195 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
kadonotakashi 0:8fdf9a60065b 196 * }
kadonotakashi 0:8fdf9a60065b 197 * return value + a
kadonotakashi 0:8fdf9a60065b 198 * }
kadonotakashi 0:8fdf9a60065b 199 *
kadonotakashi 0:8fdf9a60065b 200 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
kadonotakashi 0:8fdf9a60065b 201 * always succeeds if the current value is expected, as per the pseudocode
kadonotakashi 0:8fdf9a60065b 202 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
kadonotakashi 0:8fdf9a60065b 203 */
kadonotakashi 0:8fdf9a60065b 204 bool core_util_atomic_cas_u16(volatile uint16_t *ptr, uint16_t *expectedCurrentValue, uint16_t desiredValue);
kadonotakashi 0:8fdf9a60065b 205
kadonotakashi 0:8fdf9a60065b 206 /**
kadonotakashi 0:8fdf9a60065b 207 * Atomic compare and set. It compares the contents of a memory location to a
kadonotakashi 0:8fdf9a60065b 208 * given value and, only if they are the same, modifies the contents of that
kadonotakashi 0:8fdf9a60065b 209 * memory location to a given new value. This is done as a single atomic
kadonotakashi 0:8fdf9a60065b 210 * operation. The atomicity guarantees that the new value is calculated based on
kadonotakashi 0:8fdf9a60065b 211 * up-to-date information; if the value had been updated by another thread in
kadonotakashi 0:8fdf9a60065b 212 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
kadonotakashi 0:8fdf9a60065b 213 *
kadonotakashi 0:8fdf9a60065b 214 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
kadonotakashi 0:8fdf9a60065b 215 * you to the article on compare-and swap].
kadonotakashi 0:8fdf9a60065b 216 *
kadonotakashi 0:8fdf9a60065b 217 * @param ptr The target memory location.
kadonotakashi 0:8fdf9a60065b 218 * @param[in,out] expectedCurrentValue A pointer to some location holding the
kadonotakashi 0:8fdf9a60065b 219 * expected current value of the data being set atomically.
kadonotakashi 0:8fdf9a60065b 220 * The computed 'desiredValue' should be a function of this current value.
kadonotakashi 0:8fdf9a60065b 221 * @note: This is an in-out parameter. In the
kadonotakashi 0:8fdf9a60065b 222 * failure case of atomic_cas (where the
kadonotakashi 0:8fdf9a60065b 223 * destination isn't set), the pointee of expectedCurrentValue is
kadonotakashi 0:8fdf9a60065b 224 * updated with the current value.
kadonotakashi 0:8fdf9a60065b 225 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
kadonotakashi 0:8fdf9a60065b 226 *
kadonotakashi 0:8fdf9a60065b 227 * @return true if the memory location was atomically
kadonotakashi 0:8fdf9a60065b 228 * updated with the desired value (after verifying
kadonotakashi 0:8fdf9a60065b 229 * that it contained the expectedCurrentValue),
kadonotakashi 0:8fdf9a60065b 230 * false otherwise. In the failure case,
kadonotakashi 0:8fdf9a60065b 231 * exepctedCurrentValue is updated with the new
kadonotakashi 0:8fdf9a60065b 232 * value of the target memory location.
kadonotakashi 0:8fdf9a60065b 233 *
kadonotakashi 0:8fdf9a60065b 234 * pseudocode:
kadonotakashi 0:8fdf9a60065b 235 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
kadonotakashi 0:8fdf9a60065b 236 * if *p != *old {
kadonotakashi 0:8fdf9a60065b 237 * *old = *p
kadonotakashi 0:8fdf9a60065b 238 * return false
kadonotakashi 0:8fdf9a60065b 239 * }
kadonotakashi 0:8fdf9a60065b 240 * *p = new
kadonotakashi 0:8fdf9a60065b 241 * return true
kadonotakashi 0:8fdf9a60065b 242 * }
kadonotakashi 0:8fdf9a60065b 243 *
kadonotakashi 0:8fdf9a60065b 244 * @note: In the failure case (where the destination isn't set), the value
kadonotakashi 0:8fdf9a60065b 245 * pointed to by expectedCurrentValue is instead updated with the current value.
kadonotakashi 0:8fdf9a60065b 246 * This property helps writing concise code for the following incr:
kadonotakashi 0:8fdf9a60065b 247 *
kadonotakashi 0:8fdf9a60065b 248 * function incr(p : pointer to int, a : int) returns int {
kadonotakashi 0:8fdf9a60065b 249 * done = false
kadonotakashi 0:8fdf9a60065b 250 * value = *p // This fetch operation need not be atomic.
kadonotakashi 0:8fdf9a60065b 251 * while not done {
kadonotakashi 0:8fdf9a60065b 252 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
kadonotakashi 0:8fdf9a60065b 253 * }
kadonotakashi 0:8fdf9a60065b 254 * return value + a
kadonotakashi 0:8fdf9a60065b 255 *
kadonotakashi 0:8fdf9a60065b 256 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
kadonotakashi 0:8fdf9a60065b 257 * always succeeds if the current value is expected, as per the pseudocode
kadonotakashi 0:8fdf9a60065b 258 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
kadonotakashi 0:8fdf9a60065b 259 * }
kadonotakashi 0:8fdf9a60065b 260 */
kadonotakashi 0:8fdf9a60065b 261 bool core_util_atomic_cas_u32(volatile uint32_t *ptr, uint32_t *expectedCurrentValue, uint32_t desiredValue);
kadonotakashi 0:8fdf9a60065b 262
kadonotakashi 0:8fdf9a60065b 263 /**
kadonotakashi 0:8fdf9a60065b 264 * Atomic compare and set. It compares the contents of a memory location to a
kadonotakashi 0:8fdf9a60065b 265 * given value and, only if they are the same, modifies the contents of that
kadonotakashi 0:8fdf9a60065b 266 * memory location to a given new value. This is done as a single atomic
kadonotakashi 0:8fdf9a60065b 267 * operation. The atomicity guarantees that the new value is calculated based on
kadonotakashi 0:8fdf9a60065b 268 * up-to-date information; if the value had been updated by another thread in
kadonotakashi 0:8fdf9a60065b 269 * the meantime, the write would fail due to a mismatched expectedCurrentValue.
kadonotakashi 0:8fdf9a60065b 270 *
kadonotakashi 0:8fdf9a60065b 271 * Refer to https://en.wikipedia.org/wiki/Compare-and-set [which may redirect
kadonotakashi 0:8fdf9a60065b 272 * you to the article on compare-and swap].
kadonotakashi 0:8fdf9a60065b 273 *
kadonotakashi 0:8fdf9a60065b 274 * @param ptr The target memory location.
kadonotakashi 0:8fdf9a60065b 275 * @param[in,out] expectedCurrentValue A pointer to some location holding the
kadonotakashi 0:8fdf9a60065b 276 * expected current value of the data being set atomically.
kadonotakashi 0:8fdf9a60065b 277 * The computed 'desiredValue' should be a function of this current value.
kadonotakashi 0:8fdf9a60065b 278 * @note: This is an in-out parameter. In the
kadonotakashi 0:8fdf9a60065b 279 * failure case of atomic_cas (where the
kadonotakashi 0:8fdf9a60065b 280 * destination isn't set), the pointee of expectedCurrentValue is
kadonotakashi 0:8fdf9a60065b 281 * updated with the current value.
kadonotakashi 0:8fdf9a60065b 282 * @param[in] desiredValue The new value computed based on '*expectedCurrentValue'.
kadonotakashi 0:8fdf9a60065b 283 *
kadonotakashi 0:8fdf9a60065b 284 * @return true if the memory location was atomically
kadonotakashi 0:8fdf9a60065b 285 * updated with the desired value (after verifying
kadonotakashi 0:8fdf9a60065b 286 * that it contained the expectedCurrentValue),
kadonotakashi 0:8fdf9a60065b 287 * false otherwise. In the failure case,
kadonotakashi 0:8fdf9a60065b 288 * exepctedCurrentValue is updated with the new
kadonotakashi 0:8fdf9a60065b 289 * value of the target memory location.
kadonotakashi 0:8fdf9a60065b 290 *
kadonotakashi 0:8fdf9a60065b 291 * pseudocode:
kadonotakashi 0:8fdf9a60065b 292 * function cas(p : pointer to int, old : pointer to int, new : int) returns bool {
kadonotakashi 0:8fdf9a60065b 293 * if *p != *old {
kadonotakashi 0:8fdf9a60065b 294 * *old = *p
kadonotakashi 0:8fdf9a60065b 295 * return false
kadonotakashi 0:8fdf9a60065b 296 * }
kadonotakashi 0:8fdf9a60065b 297 * *p = new
kadonotakashi 0:8fdf9a60065b 298 * return true
kadonotakashi 0:8fdf9a60065b 299 * }
kadonotakashi 0:8fdf9a60065b 300 *
kadonotakashi 0:8fdf9a60065b 301 * @note: In the failure case (where the destination isn't set), the value
kadonotakashi 0:8fdf9a60065b 302 * pointed to by expectedCurrentValue is instead updated with the current value.
kadonotakashi 0:8fdf9a60065b 303 * This property helps writing concise code for the following incr:
kadonotakashi 0:8fdf9a60065b 304 *
kadonotakashi 0:8fdf9a60065b 305 * function incr(p : pointer to int, a : int) returns int {
kadonotakashi 0:8fdf9a60065b 306 * done = false
kadonotakashi 0:8fdf9a60065b 307 * value = *p // This fetch operation need not be atomic.
kadonotakashi 0:8fdf9a60065b 308 * while not done {
kadonotakashi 0:8fdf9a60065b 309 * done = atomic_cas(p, &value, value + a) // *value gets updated automatically until success
kadonotakashi 0:8fdf9a60065b 310 * }
kadonotakashi 0:8fdf9a60065b 311 * return value + a
kadonotakashi 0:8fdf9a60065b 312 * }
kadonotakashi 0:8fdf9a60065b 313 *
kadonotakashi 0:8fdf9a60065b 314 * @note: This corresponds to the C11 "atomic_compare_exchange_strong" - it
kadonotakashi 0:8fdf9a60065b 315 * always succeeds if the current value is expected, as per the pseudocode
kadonotakashi 0:8fdf9a60065b 316 * above; it will not spuriously fail as "atomic_compare_exchange_weak" may.
kadonotakashi 0:8fdf9a60065b 317 */
kadonotakashi 0:8fdf9a60065b 318 bool core_util_atomic_cas_ptr(void *volatile *ptr, void **expectedCurrentValue, void *desiredValue);
kadonotakashi 0:8fdf9a60065b 319
kadonotakashi 0:8fdf9a60065b 320 /**
kadonotakashi 0:8fdf9a60065b 321 * Atomic increment.
kadonotakashi 0:8fdf9a60065b 322 * @param valuePtr Target memory location being incremented.
kadonotakashi 0:8fdf9a60065b 323 * @param delta The amount being incremented.
kadonotakashi 0:8fdf9a60065b 324 * @return The new incremented value.
kadonotakashi 0:8fdf9a60065b 325 */
kadonotakashi 0:8fdf9a60065b 326 uint8_t core_util_atomic_incr_u8(volatile uint8_t *valuePtr, uint8_t delta);
kadonotakashi 0:8fdf9a60065b 327
kadonotakashi 0:8fdf9a60065b 328 /**
kadonotakashi 0:8fdf9a60065b 329 * Atomic increment.
kadonotakashi 0:8fdf9a60065b 330 * @param valuePtr Target memory location being incremented.
kadonotakashi 0:8fdf9a60065b 331 * @param delta The amount being incremented.
kadonotakashi 0:8fdf9a60065b 332 * @return The new incremented value.
kadonotakashi 0:8fdf9a60065b 333 */
kadonotakashi 0:8fdf9a60065b 334 uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta);
kadonotakashi 0:8fdf9a60065b 335
kadonotakashi 0:8fdf9a60065b 336 /**
kadonotakashi 0:8fdf9a60065b 337 * Atomic increment.
kadonotakashi 0:8fdf9a60065b 338 * @param valuePtr Target memory location being incremented.
kadonotakashi 0:8fdf9a60065b 339 * @param delta The amount being incremented.
kadonotakashi 0:8fdf9a60065b 340 * @return The new incremented value.
kadonotakashi 0:8fdf9a60065b 341 */
kadonotakashi 0:8fdf9a60065b 342 uint32_t core_util_atomic_incr_u32(volatile uint32_t *valuePtr, uint32_t delta);
kadonotakashi 0:8fdf9a60065b 343
kadonotakashi 0:8fdf9a60065b 344 /**
kadonotakashi 0:8fdf9a60065b 345 * Atomic increment.
kadonotakashi 0:8fdf9a60065b 346 * @param valuePtr Target memory location being incremented.
kadonotakashi 0:8fdf9a60065b 347 * @param delta The amount being incremented in bytes.
kadonotakashi 0:8fdf9a60065b 348 * @return The new incremented value.
kadonotakashi 0:8fdf9a60065b 349 *
kadonotakashi 0:8fdf9a60065b 350 * @note The type of the pointer argument is not taken into account
kadonotakashi 0:8fdf9a60065b 351 * and the pointer is incremented by bytes.
kadonotakashi 0:8fdf9a60065b 352 */
kadonotakashi 0:8fdf9a60065b 353 void *core_util_atomic_incr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
kadonotakashi 0:8fdf9a60065b 354
kadonotakashi 0:8fdf9a60065b 355 /**
kadonotakashi 0:8fdf9a60065b 356 * Atomic decrement.
kadonotakashi 0:8fdf9a60065b 357 * @param valuePtr Target memory location being decremented.
kadonotakashi 0:8fdf9a60065b 358 * @param delta The amount being decremented.
kadonotakashi 0:8fdf9a60065b 359 * @return The new decremented value.
kadonotakashi 0:8fdf9a60065b 360 */
kadonotakashi 0:8fdf9a60065b 361 uint8_t core_util_atomic_decr_u8(volatile uint8_t *valuePtr, uint8_t delta);
kadonotakashi 0:8fdf9a60065b 362
kadonotakashi 0:8fdf9a60065b 363 /**
kadonotakashi 0:8fdf9a60065b 364 * Atomic decrement.
kadonotakashi 0:8fdf9a60065b 365 * @param valuePtr Target memory location being decremented.
kadonotakashi 0:8fdf9a60065b 366 * @param delta The amount being decremented.
kadonotakashi 0:8fdf9a60065b 367 * @return The new decremented value.
kadonotakashi 0:8fdf9a60065b 368 */
kadonotakashi 0:8fdf9a60065b 369 uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta);
kadonotakashi 0:8fdf9a60065b 370
kadonotakashi 0:8fdf9a60065b 371 /**
kadonotakashi 0:8fdf9a60065b 372 * Atomic decrement.
kadonotakashi 0:8fdf9a60065b 373 * @param valuePtr Target memory location being decremented.
kadonotakashi 0:8fdf9a60065b 374 * @param delta The amount being decremented.
kadonotakashi 0:8fdf9a60065b 375 * @return The new decremented value.
kadonotakashi 0:8fdf9a60065b 376 */
kadonotakashi 0:8fdf9a60065b 377 uint32_t core_util_atomic_decr_u32(volatile uint32_t *valuePtr, uint32_t delta);
kadonotakashi 0:8fdf9a60065b 378
kadonotakashi 0:8fdf9a60065b 379 /**
kadonotakashi 0:8fdf9a60065b 380 * Atomic decrement.
kadonotakashi 0:8fdf9a60065b 381 * @param valuePtr Target memory location being decremented.
kadonotakashi 0:8fdf9a60065b 382 * @param delta The amount being decremented in bytes.
kadonotakashi 0:8fdf9a60065b 383 * @return The new decremented value.
kadonotakashi 0:8fdf9a60065b 384 *
kadonotakashi 0:8fdf9a60065b 385 * @note The type of the pointer argument is not taken into account
kadonotakashi 0:8fdf9a60065b 386 * and the pointer is decremented by bytes
kadonotakashi 0:8fdf9a60065b 387 */
kadonotakashi 0:8fdf9a60065b 388 void *core_util_atomic_decr_ptr(void *volatile *valuePtr, ptrdiff_t delta);
kadonotakashi 0:8fdf9a60065b 389
kadonotakashi 0:8fdf9a60065b 390 #ifdef __cplusplus
kadonotakashi 0:8fdf9a60065b 391 } // extern "C"
kadonotakashi 0:8fdf9a60065b 392 #endif
kadonotakashi 0:8fdf9a60065b 393 /**@}*/
kadonotakashi 0:8fdf9a60065b 394
kadonotakashi 0:8fdf9a60065b 395 /**@}*/
kadonotakashi 0:8fdf9a60065b 396
kadonotakashi 0:8fdf9a60065b 397 #endif // __MBED_UTIL_CRITICAL_H__
kadonotakashi 0:8fdf9a60065b 398
kadonotakashi 0:8fdf9a60065b 399
kadonotakashi 0:8fdf9a60065b 400