Simulated product dispenser
Fork of mbed-cloud-workshop-connect-HTS221 by
critical-nordic.c
00001 // ---------------------------------------------------------------------------- 00002 // Copyright 2015-2017 ARM Ltd. 00003 // 00004 // SPDX-License-Identifier: Apache-2.0 00005 // 00006 // Licensed under the Apache License, Version 2.0 (the "License"); 00007 // you may not use this file except in compliance with the License. 00008 // You may obtain a copy of the License at 00009 // 00010 // http://www.apache.org/licenses/LICENSE-2.0 00011 // 00012 // Unless required by applicable law or agreed to in writing, software 00013 // distributed under the License is distributed on an "AS IS" BASIS, 00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 // See the License for the specific language governing permissions and 00016 // limitations under the License. 00017 // ---------------------------------------------------------------------------- 00018 00019 #ifdef TARGET_MCU_NRF51 00020 00021 #include <stdint.h> // uint32_t, UINT32_MAX 00022 #include <stddef.h> // NULL 00023 #include <cmsis-core/core_generic.h> //__disable_irq, __enable_irq 00024 #include <assert.h> 00025 #include <stdbool.h> 00026 #include <nrf_soc.h> 00027 #include <nrf_sdm.h> 00028 00029 // Module include 00030 #include "aq_critical.h" 00031 00032 static volatile union { 00033 uint32_t _PRIMASK_state; 00034 uint8_t _sd_state; 00035 } _state = { 0 } ; 00036 static volatile uint32_t _entry_count = 0; 00037 static volatile bool _use_softdevice_routine = false; 00038 00039 void aq_critical_section_enter() 00040 { 00041 // if a critical section has already been entered, just update the counter 00042 if (_entry_count) { 00043 ++_entry_count; 00044 return; 00045 } 00046 00047 // in this path, a critical section has never been entered 00048 uint32_t primask = __get_PRIMASK(); 00049 00050 // if interrupts are enabled, try to use the soft device 00051 uint8_t sd_enabled; 00052 if ((primask == 0) && (sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) && sd_enabled == 1) { 00053 // if the soft device can be use, use it 00054 sd_nvic_critical_region_enter(&_state._sd_state); 00055 _use_softdevice_routine = true; 00056 } else { 00057 // if interrupts where enabled, disable them 00058 if(primask == 0) { 00059 __disable_irq(); 00060 } 00061 00062 // store the PRIMASK state, it will be restored at the end of the critical section 00063 _state._PRIMASK_state = primask; 00064 _use_softdevice_routine = false; 00065 } 00066 00067 assert(_entry_count == 0); // entry count should always be equal to 0 at this point 00068 ++_entry_count; 00069 } 00070 00071 void aq_critical_section_exit(void) 00072 { 00073 assert(_entry_count > 0); 00074 --_entry_count; 00075 00076 // If their is other segments which have entered the critical section, just leave 00077 if (_entry_count) { 00078 return; 00079 } 00080 00081 // This is the last segment of the critical section, state should be restored as before entering 00082 // the critical section 00083 if (_use_softdevice_routine) { 00084 sd_nvic_critical_region_exit(_state._sd_state); 00085 } else { 00086 __set_PRIMASK(_state._PRIMASK_state); 00087 } 00088 } 00089 00090 #endif //TARGET_MCU_NRF51
Generated on Tue Jul 12 2022 19:12:12 by 1.7.2