Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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