leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2015-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 // This critical section implementation is generic for mbed OS targets,
leothedragon 0:8f0bb79ddd48 20 // except Nordic ones
leothedragon 0:8f0bb79ddd48 21 #if defined(TARGET_LIKE_MBED) && !defined(TARGET_NORDIC)
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 #include <stdint.h> // uint32_t, UINT32_MAX
leothedragon 0:8f0bb79ddd48 24 #include <stddef.h> // NULL
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 #if defined(YOTTA_CFG_MBED_OS)
leothedragon 0:8f0bb79ddd48 27 #include "cmsis-core/core_generic.h" //__disable_irq, __enable_irq
leothedragon 0:8f0bb79ddd48 28 #else
leothedragon 0:8f0bb79ddd48 29 #include "cmsis.h"
leothedragon 0:8f0bb79ddd48 30 #endif
leothedragon 0:8f0bb79ddd48 31
leothedragon 0:8f0bb79ddd48 32 #include <assert.h>
leothedragon 0:8f0bb79ddd48 33
leothedragon 0:8f0bb79ddd48 34 // Module include
leothedragon 0:8f0bb79ddd48 35 #include "aq_critical.h"
leothedragon 0:8f0bb79ddd48 36
leothedragon 0:8f0bb79ddd48 37 static volatile uint32_t interruptEnableCounter = 0;
leothedragon 0:8f0bb79ddd48 38 static volatile uint32_t critical_primask = 0;
leothedragon 0:8f0bb79ddd48 39
leothedragon 0:8f0bb79ddd48 40 void aq_critical_section_enter()
leothedragon 0:8f0bb79ddd48 41 {
leothedragon 0:8f0bb79ddd48 42 uint32_t primask;
leothedragon 0:8f0bb79ddd48 43 #if defined(__CORTEX_A9)
leothedragon 0:8f0bb79ddd48 44 primask = __get_CPSR(); // get the current interrupt enabled state
leothedragon 0:8f0bb79ddd48 45 #else
leothedragon 0:8f0bb79ddd48 46 primask = __get_PRIMASK(); // get the current interrupt enabled state
leothedragon 0:8f0bb79ddd48 47 #endif
leothedragon 0:8f0bb79ddd48 48 __disable_irq();
leothedragon 0:8f0bb79ddd48 49
leothedragon 0:8f0bb79ddd48 50 // Save the interrupt enabled state as it was prior to any nested critical section lock use
leothedragon 0:8f0bb79ddd48 51 if (!interruptEnableCounter) {
leothedragon 0:8f0bb79ddd48 52 #if defined(__CORTEX_A9)
leothedragon 0:8f0bb79ddd48 53 critical_primask = primask & 0x80;
leothedragon 0:8f0bb79ddd48 54 #else
leothedragon 0:8f0bb79ddd48 55 critical_primask = primask & 0x1;
leothedragon 0:8f0bb79ddd48 56 #endif
leothedragon 0:8f0bb79ddd48 57 }
leothedragon 0:8f0bb79ddd48 58 #if 0
leothedragon 0:8f0bb79ddd48 59 /* If the interruptEnableCounter overflows or we are in a nested critical section and interrupts
leothedragon 0:8f0bb79ddd48 60 are enabled, then something has gone badly wrong thus assert an error.
leothedragon 0:8f0bb79ddd48 61 */
leothedragon 0:8f0bb79ddd48 62
leothedragon 0:8f0bb79ddd48 63 /* FIXME: This assertion needs to be commented out for the moment, as it
leothedragon 0:8f0bb79ddd48 64 * triggers a fault when uVisor is enabled. For more information on
leothedragon 0:8f0bb79ddd48 65 * the fault please checkout ARMmbed/mbed-drivers#176. */
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 assert(interruptEnableCounter < UINT32_MAX);
leothedragon 0:8f0bb79ddd48 68 if (interruptEnableCounter > 0) {
leothedragon 0:8f0bb79ddd48 69 assert(primask & 0x1);
leothedragon 0:8f0bb79ddd48 70 }
leothedragon 0:8f0bb79ddd48 71 #endif
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 if (interruptEnableCounter < UINT32_MAX) {
leothedragon 0:8f0bb79ddd48 74 interruptEnableCounter++;
leothedragon 0:8f0bb79ddd48 75 }
leothedragon 0:8f0bb79ddd48 76 }
leothedragon 0:8f0bb79ddd48 77
leothedragon 0:8f0bb79ddd48 78 void aq_critical_section_exit(void)
leothedragon 0:8f0bb79ddd48 79 {
leothedragon 0:8f0bb79ddd48 80 // If critical_section_enter has not previously been called, do nothing
leothedragon 0:8f0bb79ddd48 81 if (interruptEnableCounter) {
leothedragon 0:8f0bb79ddd48 82 #if 0
leothedragon 0:8f0bb79ddd48 83 /* FIXME: This assertion needs to be commented out for the moment, as it
leothedragon 0:8f0bb79ddd48 84 * triggers a fault when uVisor is enabled. For more information
leothedragon 0:8f0bb79ddd48 85 * on the fault please checkout ARMmbed/mbed-drivers#176. */
leothedragon 0:8f0bb79ddd48 86
leothedragon 0:8f0bb79ddd48 87 uint32_t primask;
leothedragon 0:8f0bb79ddd48 88 #if defined(__CORTEX_A9)
leothedragon 0:8f0bb79ddd48 89 primask = __get_CPSR(); // get the current interrupt enabled state
leothedragon 0:8f0bb79ddd48 90 #else
leothedragon 0:8f0bb79ddd48 91 primask = __get_PRIMASK(); // get the current interrupt enabled state
leothedragon 0:8f0bb79ddd48 92 #endif
leothedragon 0:8f0bb79ddd48 93 assert(primask & 0x1); // Interrupts must be disabled on invoking an exit from a critical section
leothedragon 0:8f0bb79ddd48 94 #endif
leothedragon 0:8f0bb79ddd48 95
leothedragon 0:8f0bb79ddd48 96 interruptEnableCounter--;
leothedragon 0:8f0bb79ddd48 97
leothedragon 0:8f0bb79ddd48 98
leothedragon 0:8f0bb79ddd48 99 /* Only re-enable interrupts if we are exiting the last of the nested critical sections and
leothedragon 0:8f0bb79ddd48 100 interrupts were enabled on entry to the first critical section.
leothedragon 0:8f0bb79ddd48 101 */
leothedragon 0:8f0bb79ddd48 102 if (!interruptEnableCounter && !critical_primask) {
leothedragon 0:8f0bb79ddd48 103 __enable_irq();
leothedragon 0:8f0bb79ddd48 104 }
leothedragon 0:8f0bb79ddd48 105 }
leothedragon 0:8f0bb79ddd48 106 }
leothedragon 0:8f0bb79ddd48 107
leothedragon 0:8f0bb79ddd48 108 #endif // defined(TARGET_LIKE_MBED) && !defined(TARGET_NORDIC)