Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DeepSleepLock.cpp Source File

DeepSleepLock.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017-2019 ARM Limited
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 "platform/DeepSleepLock.h"
00019 
00020 #include "platform/mbed_atomic.h"
00021 #include "platform/mbed_critical.h"
00022 #include "platform/mbed_error.h"
00023 #include "platform/mbed_power_mgmt.h"
00024 
00025 namespace mbed {
00026 
00027 DeepSleepLock::DeepSleepLock(): _lock_count(1)
00028 {
00029     sleep_manager_lock_deep_sleep();
00030 }
00031 
00032 DeepSleepLock::~DeepSleepLock()
00033 {
00034     if (_lock_count) {
00035         sleep_manager_unlock_deep_sleep();
00036     }
00037 }
00038 
00039 void DeepSleepLock::lock()
00040 {
00041     uint16_t count = core_util_atomic_incr_u16 (&_lock_count, 1);
00042     if (1 == count) {
00043         sleep_manager_lock_deep_sleep();
00044     }
00045     if (0 == count) {
00046         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", count);
00047     }
00048 }
00049 
00050 void DeepSleepLock::unlock()
00051 {
00052     uint16_t count = core_util_atomic_decr_u16 (&_lock_count, 1);
00053     if (count == 0) {
00054         sleep_manager_unlock_deep_sleep();
00055     }
00056     if (count == USHRT_MAX) {
00057         core_util_critical_section_exit();
00058         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", count);
00059     }
00060 }
00061 
00062 } // namespace mbed