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