Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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