Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 08:06:37 2018 +0000
Revision:
2:bf2124b482f9
Parent:
0:276e7a263c35
Update library

Who changed what in which revision?

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