|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2006-2019 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_HIGHRESCLOCK_H |
| 18 | +#define MBED_HIGHRESCLOCK_H |
| 19 | + |
| 20 | +#include <chrono> |
| 21 | +#include "hal/us_ticker_api.h" |
| 22 | +#include "platform/mbed_power_mgmt.h" |
| 23 | + |
| 24 | +namespace mbed { |
| 25 | +/** |
| 26 | + * \defgroup drivers_HighResClock HighResClock class |
| 27 | + * \ingroup drivers-public-api-ticker |
| 28 | + * @{ |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * A C++11 Clock representing the HAL us_ticker. |
| 33 | + * |
| 34 | + * The high resolution clock will pause whenever deep sleep is entered. |
| 35 | + * |
| 36 | + * Lock and unlock methods are provided to control deep sleep to keep the |
| 37 | + * clock running. LowPowerClock provides the same methods as dummies to support |
| 38 | + * generic code that works with either clock. |
| 39 | + * |
| 40 | + * Locks and unlocks are passed to the deep sleep manager, so are reference counted. |
| 41 | + * |
| 42 | + * The lock and unlock methods mean this is a C++11 BasicLockable class, |
| 43 | + * so scoped_lock<HighResClock> or unique_lock<HighResClock> could be used to |
| 44 | + * manage ownership and guarantee balanced unlocking. |
| 45 | + * |
| 46 | + * @note To avoid the need to worry about locking, consider using the Timer, Ticker or |
| 47 | + * Timeout classes, which lock automatically and only whenever they are active |
| 48 | + */ |
| 49 | +class HighResClock { |
| 50 | +public: |
| 51 | + /* duration is C++11 standard microseconds, so representation will be 64-bit signed integer */ |
| 52 | + using duration = std::chrono::microseconds; |
| 53 | + using rep = duration::rep; |
| 54 | + using period = duration::period; |
| 55 | + using time_point = std::chrono::time_point<HighResClock>; |
| 56 | + static const bool is_steady = false; |
| 57 | + |
| 58 | + /** Read the current time */ |
| 59 | + static time_point now() |
| 60 | + { |
| 61 | + return time_point{duration{ticker_read_us(get_us_ticker_data())}}; |
| 62 | + } |
| 63 | + |
| 64 | + /** Lock the clock to ensure it stays running */ |
| 65 | + static void lock() |
| 66 | + { |
| 67 | + sleep_manager_lock_deep_sleep(); |
| 68 | + } |
| 69 | + |
| 70 | + /** Unlock the clock, allowing it to stop during power saving */ |
| 71 | + static void unlock() |
| 72 | + { |
| 73 | + sleep_manager_unlock_deep_sleep(); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +/** @}*/ |
| 78 | + |
| 79 | +} |
| 80 | +#endif /* MBED_HIGHRESCLOCK_H */ |
0 commit comments