Mistake on this page?
Report an issue in GitHub or email us
DeepSleepLock.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2017 ARM Limited
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef MBED_DEEPSLEEPLOCK_H
18 #define MBED_DEEPSLEEPLOCK_H
19 
20 #include <limits.h>
21 #include "platform/mbed_power_mgmt.h"
22 #include "platform/mbed_atomic.h"
23 
24 namespace mbed {
25 
26 /** \addtogroup platform */
27 /** @{*/
28 /**
29  * \defgroup platform_DeepSleepLock DeepSleepLock functions
30  * @{
31  */
32 
33 /** RAII object for disabling, then restoring the deep sleep mode
34  * Usage:
35  * @code
36  *
37  * void f() {
38  * // some code here
39  * {
40  * DeepSleepLock lock;
41  * // Code in this block will run with the deep sleep mode locked
42  * }
43  * // deep sleep mode will be restored to their previous state
44  * }
45  * @endcode
46  */
48 private:
49  uint16_t _lock_count;
50 
51 public:
52  DeepSleepLock(): _lock_count(1)
53  {
54  sleep_manager_lock_deep_sleep();
55  }
56 
57  ~DeepSleepLock()
58  {
59  if (_lock_count) {
60  sleep_manager_unlock_deep_sleep();
61  }
62  }
63 
64  /** Mark the start of a locked deep sleep section
65  */
66  void lock()
67  {
68  uint16_t count = core_util_atomic_incr_u16(&_lock_count, 1);
69  if (1 == count) {
70  sleep_manager_lock_deep_sleep();
71  }
72  if (0 == count) {
73  MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OVERFLOW), "DeepSleepLock overflow (> USHRT_MAX)", count);
74  }
75  }
76 
77  /** Mark the end of a locked deep sleep section
78  */
79  void unlock()
80  {
81  uint16_t count = core_util_atomic_decr_u16(&_lock_count, 1);
82  if (count == 0) {
83  sleep_manager_unlock_deep_sleep();
84  }
85  if (count == USHRT_MAX) {
87  MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_UNDERFLOW), "DeepSleepLock underflow (< 0)", count);
88  }
89  }
90 };
91 
92 /**@}*/
93 
94 /**@}*/
95 
96 
97 }
98 
99 #endif
void lock()
Mark the start of a locked deep sleep section.
Definition: DeepSleepLock.h:66
void core_util_critical_section_exit(void)
Mark the end of a critical section.
#define MBED_MAKE_ERROR(module, error_code)
Call this Macro to generate a mbed_error_status_t value for a System error.
Definition: mbed_error.h:939
uint16_t core_util_atomic_decr_u16(volatile uint16_t *valuePtr, uint16_t delta)
Atomic decrement.
uint16_t core_util_atomic_incr_u16(volatile uint16_t *valuePtr, uint16_t delta)
Atomic increment.
void unlock()
Mark the end of a locked deep sleep section.
Definition: DeepSleepLock.h:79
#define MBED_ERROR1(error_status, error_msg, error_value)
Macros for setting a fatal system error.
Definition: mbed_error.h:197
RAII object for disabling, then restoring the deep sleep mode Usage:
Definition: DeepSleepLock.h:47
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.