Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_hal_interrupt.c Source File

arm_hal_interrupt.c

00001 /*
00002  * Copyright (c) 2016-2018, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "arm_hal_interrupt.h"
00019 #include "arm_hal_interrupt_private.h"
00020 
00021 #if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
00022 #include "platform/mbed_critical.h"
00023 #else
00024 #include "cmsis_os2.h"
00025 #include "mbed_rtos_storage.h"
00026 #endif
00027 
00028 #include <mbed_assert.h>
00029 
00030 
00031 // The critical section has two alternative implementations, the default which uses mutex
00032 // as locking primitive and the optional, which will bluntly disable interrupts
00033 // for the critical section. The mutex version will not cause issues by delaying
00034 // interrupts, but it has a down side that it can not be used from the interrupt
00035 // service routines. The IRQ-safe version will do the mutual exclusion by temporarily
00036 // disabling the interrupts, so it will have effect on interrupt latency.
00037 #if !MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
00038 
00039 static uint8_t sys_irq_disable_counter;
00040 
00041 static mbed_rtos_storage_mutex_t critical_mutex;
00042 static const osMutexAttr_t critical_mutex_attr = {
00043     .name = "nanostack_critical_mutex",
00044     .attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
00045     .cb_mem = &critical_mutex,
00046     .cb_size = sizeof critical_mutex,
00047 };
00048 static osMutexId_t critical_mutex_id;
00049 
00050 #endif
00051 
00052 void platform_critical_init(void)
00053 {
00054 #if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
00055     // nothing to do here
00056 #else
00057     critical_mutex_id = osMutexNew(&critical_mutex_attr);
00058     MBED_ASSERT(critical_mutex_id);
00059 #endif
00060 }
00061 
00062 void platform_enter_critical(void)
00063 {
00064 #if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
00065     core_util_critical_section_enter();
00066 #else
00067     osMutexAcquire(critical_mutex_id, osWaitForever);
00068     sys_irq_disable_counter++;
00069 #endif
00070 }
00071 
00072 void platform_exit_critical(void)
00073 {
00074 #if MBED_CONF_NANOSTACK_HAL_CRITICAL_SECTION_USABLE_FROM_INTERRUPT
00075     core_util_critical_section_exit();
00076 #else
00077     --sys_irq_disable_counter;
00078     osMutexRelease(critical_mutex_id);
00079 #endif
00080 }