Skip to content

Commit b9aa69a

Browse files
committed
Add HighResClock and LowPowerClock
1 parent 4575ad3 commit b9aa69a

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

drivers/HighResClock.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 */

drivers/LowPowerClock.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_LOWPOWERCLOCK_H
18+
#define MBED_LOWPOWERCLOCK_H
19+
20+
#include <chrono>
21+
#include "hal/lp_ticker_api.h"
22+
23+
namespace mbed {
24+
/**
25+
* \defgroup drivers_LowPowerClock LowPowerClock class
26+
* \ingroup drivers-public-api-ticker
27+
* @{
28+
*/
29+
30+
/**
31+
* A C++11 Clock representing the HAL lp_ticker.
32+
*
33+
* Dummy lock/unlock methods are provided to have the same generic API
34+
* as UnlockedHighPowerClock. No action is required as the lp_ticker
35+
* runs continuously.
36+
*/
37+
class LowPowerClock {
38+
public:
39+
/* duration is C++11 standard microseconds, so representation will be 64-bit signed integer */
40+
using duration = std::chrono::microseconds;
41+
using rep = duration::rep;
42+
using period = duration::period;
43+
using time_point = std::chrono::time_point<LowPowerClock>;
44+
static const bool is_steady = true;
45+
46+
/** Read the current time */
47+
static time_point now()
48+
{
49+
return time_point{duration{ticker_read_us(get_lp_ticker_data())}};
50+
}
51+
52+
/** Lock the clock to ensure it stays running; dummy for API compatibility with HighResClock */
53+
static void lock()
54+
{
55+
}
56+
57+
/** Unlock the clock, allowing it to stop during power saving; dummy for API compatibility with HighResClock */
58+
static void unlock()
59+
{
60+
}
61+
};
62+
63+
/** @}*/
64+
65+
}
66+
#endif /* MBED_LOWPOWERCLOCK_H */

mbed.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@
7676

7777
// mbed Internal components
7878
#include "drivers/ResetReason.h"
79+
#include "drivers/HighResClock.h"
7980
#include "drivers/Timer.h"
8081
#include "drivers/Ticker.h"
8182
#include "drivers/Timeout.h"
83+
#include "drivers/LowPowerClock.h"
8284
#include "drivers/LowPowerTimeout.h"
8385
#include "drivers/LowPowerTicker.h"
8486
#include "drivers/LowPowerTimer.h"

0 commit comments

Comments
 (0)