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