mbed library sources. Supersedes mbed-src.

Dependents:   SPIne CH_Communicatuin_Test CH_Communicatuin_Test2 MCP_SPIne ... more

Fork of mbed-dev-f303 by Ben Katz

Committer:
benkatz
Date:
Wed May 02 18:08:16 2018 +0000
Revision:
179:97f825502e2a
Parent:
175:af195413fb11

        

Who changed what in which revision?

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