inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 * Copyright (c) 2017 ARM Limited
NYX 0:85b3fd62ea1a 3 *
NYX 0:85b3fd62ea1a 4 * Licensed under the Apache License, Version 2.0 (the "License");
NYX 0:85b3fd62ea1a 5 * you may not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 6 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 7 *
NYX 0:85b3fd62ea1a 8 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 9 *
NYX 0:85b3fd62ea1a 10 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 11 * distributed under the License is distributed on an "AS IS" BASIS,
NYX 0:85b3fd62ea1a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 13 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 14 * limitations under the License.
NYX 0:85b3fd62ea1a 15 */
NYX 0:85b3fd62ea1a 16 #ifndef MBED_DEEPSLEEPLOCK_H
NYX 0:85b3fd62ea1a 17 #define MBED_DEEPSLEEPLOCK_H
NYX 0:85b3fd62ea1a 18
NYX 0:85b3fd62ea1a 19 #include <limits.h>
NYX 0:85b3fd62ea1a 20 #include "platform/mbed_sleep.h"
NYX 0:85b3fd62ea1a 21 #include "platform/mbed_critical.h"
NYX 0:85b3fd62ea1a 22
NYX 0:85b3fd62ea1a 23 namespace mbed {
NYX 0:85b3fd62ea1a 24
NYX 0:85b3fd62ea1a 25
NYX 0:85b3fd62ea1a 26 /** RAII object for disabling, then restoring the deep sleep mode
NYX 0:85b3fd62ea1a 27 * Usage:
NYX 0:85b3fd62ea1a 28 * @code
NYX 0:85b3fd62ea1a 29 *
NYX 0:85b3fd62ea1a 30 * void f() {
NYX 0:85b3fd62ea1a 31 * // some code here
NYX 0:85b3fd62ea1a 32 * {
NYX 0:85b3fd62ea1a 33 * DeepSleepLock lock;
NYX 0:85b3fd62ea1a 34 * // Code in this block will run with the deep sleep mode locked
NYX 0:85b3fd62ea1a 35 * }
NYX 0:85b3fd62ea1a 36 * // deep sleep mode will be restored to their previous state
NYX 0:85b3fd62ea1a 37 * }
NYX 0:85b3fd62ea1a 38 * @endcode
NYX 0:85b3fd62ea1a 39 */
NYX 0:85b3fd62ea1a 40 class DeepSleepLock {
NYX 0:85b3fd62ea1a 41 private:
NYX 0:85b3fd62ea1a 42 uint16_t _lock_count;
NYX 0:85b3fd62ea1a 43
NYX 0:85b3fd62ea1a 44 public:
NYX 0:85b3fd62ea1a 45 DeepSleepLock(): _lock_count(1)
NYX 0:85b3fd62ea1a 46 {
NYX 0:85b3fd62ea1a 47 sleep_manager_lock_deep_sleep();
NYX 0:85b3fd62ea1a 48 }
NYX 0:85b3fd62ea1a 49
NYX 0:85b3fd62ea1a 50 ~DeepSleepLock()
NYX 0:85b3fd62ea1a 51 {
NYX 0:85b3fd62ea1a 52 if (_lock_count) {
NYX 0:85b3fd62ea1a 53 sleep_manager_unlock_deep_sleep();
NYX 0:85b3fd62ea1a 54 }
NYX 0:85b3fd62ea1a 55 }
NYX 0:85b3fd62ea1a 56
NYX 0:85b3fd62ea1a 57 /** Mark the start of a locked deep sleep section
NYX 0:85b3fd62ea1a 58 */
NYX 0:85b3fd62ea1a 59 void lock()
NYX 0:85b3fd62ea1a 60 {
NYX 0:85b3fd62ea1a 61 uint16_t count = core_util_atomic_incr_u16(&_lock_count, 1);
NYX 0:85b3fd62ea1a 62 if (1 == count) {
NYX 0:85b3fd62ea1a 63 sleep_manager_lock_deep_sleep();
NYX 0:85b3fd62ea1a 64 }
NYX 0:85b3fd62ea1a 65 if (0 == count) {
NYX 0:85b3fd62ea1a 66 error("DeepSleepLock overflow (> USHRT_MAX)");
NYX 0:85b3fd62ea1a 67 }
NYX 0:85b3fd62ea1a 68 }
NYX 0:85b3fd62ea1a 69
NYX 0:85b3fd62ea1a 70 /** Mark the end of a locked deep sleep section
NYX 0:85b3fd62ea1a 71 */
NYX 0:85b3fd62ea1a 72 void unlock()
NYX 0:85b3fd62ea1a 73 {
NYX 0:85b3fd62ea1a 74 uint16_t count = core_util_atomic_decr_u16(&_lock_count, 1);
NYX 0:85b3fd62ea1a 75 if (count == 0) {
NYX 0:85b3fd62ea1a 76 sleep_manager_unlock_deep_sleep();
NYX 0:85b3fd62ea1a 77 }
NYX 0:85b3fd62ea1a 78 if (count == USHRT_MAX) {
NYX 0:85b3fd62ea1a 79 core_util_critical_section_exit();
NYX 0:85b3fd62ea1a 80 error("DeepSleepLock underflow (< 0)");
NYX 0:85b3fd62ea1a 81 }
NYX 0:85b3fd62ea1a 82 }
NYX 0:85b3fd62ea1a 83 };
NYX 0:85b3fd62ea1a 84
NYX 0:85b3fd62ea1a 85 }
NYX 0:85b3fd62ea1a 86
NYX 0:85b3fd62ea1a 87 #endif