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