Skip to content

Commit 10ee2fa

Browse files
committed
Enable deepsleep for low power Ticker and low power Timer.
Fix for issue #5076.
1 parent 7b42891 commit 10ee2fa

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

drivers/Ticker.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ namespace mbed {
2525
void Ticker::detach() {
2626
core_util_critical_section_enter();
2727
remove();
28-
// unlocked only if we were attached (we locked it)
29-
if (_function) {
28+
// unlocked only if we were attached (we locked it) and this is not low power ticker
29+
if(_function && _lock_deepsleep) {
3030
sleep_manager_unlock_deep_sleep();
3131
}
32+
3233
_function = 0;
3334
core_util_critical_section_exit();
3435
}

drivers/Ticker.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
#include "platform/mbed_toolchain.h"
2222
#include "platform/NonCopyable.h"
2323
#include "platform/mbed_sleep.h"
24+
#include "hal/lp_ticker_api.h"
2425

2526
namespace mbed {
2627
/** \addtogroup drivers */
2728

2829
/** A Ticker is used to call a function at a recurring interval
2930
*
30-
* You can use as many seperate Ticker objects as you require.
31+
* You can use as many separate Ticker objects as you require.
3132
*
3233
* @note Synchronization level: Interrupt safe
3334
*
@@ -64,14 +65,18 @@ namespace mbed {
6465
class Ticker : public TimerEvent, private NonCopyable<Ticker> {
6566

6667
public:
67-
Ticker() : TimerEvent(), _function(0) {
68+
Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) {
6869
}
6970

70-
Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0) {
71+
// When low power ticker is in use, then do not disable deep-sleep.
72+
Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) {
7173
data->interface->init();
74+
#if DEVICE_LOWPOWERTIMER
75+
_lock_deepsleep = (data != get_lp_ticker_data());
76+
#endif
7277
}
7378

74-
/** Attach a function to be called by the Ticker, specifiying the interval in seconds
79+
/** Attach a function to be called by the Ticker, specifying the interval in seconds
7580
*
7681
* @param func pointer to the function to be called
7782
* @param t the time between calls in seconds
@@ -80,7 +85,7 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
8085
attach_us(func, t * 1000000.0f);
8186
}
8287

83-
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds
88+
/** Attach a member function to be called by the Ticker, specifying the interval in seconds
8489
*
8590
* @param obj pointer to the object to call the member function on
8691
* @param method pointer to the member function to be called
@@ -97,21 +102,21 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
97102
attach(callback(obj, method), t);
98103
}
99104

100-
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
105+
/** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
101106
*
102107
* @param func pointer to the function to be called
103108
* @param t the time between calls in micro-seconds
104109
*/
105110
void attach_us(Callback<void()> func, us_timestamp_t t) {
106-
// lock only for the initial callback setup
107-
if (!_function) {
111+
// lock only for the initial callback setup and this is not low power ticker
112+
if(!_function && _lock_deepsleep) {
108113
sleep_manager_lock_deep_sleep();
109114
}
110115
_function = func;
111116
setup(t);
112117
}
113118

114-
/** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
119+
/** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
115120
*
116121
* @param obj pointer to the object to call the member function on
117122
* @param method pointer to the member function to be called
@@ -143,6 +148,7 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
143148
protected:
144149
us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
145150
Callback<void()> _function; /**< Callback. */
151+
bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */
146152
};
147153

148154
} // namespace mbed

drivers/Timer.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@
1717
#include "hal/ticker_api.h"
1818
#include "hal/us_ticker_api.h"
1919
#include "platform/mbed_critical.h"
20+
#include "hal/lp_ticker_api.h"
2021

2122
namespace mbed {
2223

23-
Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()) {
24+
Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) {
2425
reset();
2526
}
2627

27-
Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data) {
28+
Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true) {
2829
reset();
30+
#if DEVICE_LOWPOWERTIMER
31+
_lock_deepsleep = (data != get_lp_ticker_data());
32+
#endif
2933
}
3034

3135
void Timer::start() {
3236
core_util_critical_section_enter();
3337
if (!_running) {
34-
sleep_manager_lock_deep_sleep();
38+
if(_lock_deepsleep) {
39+
sleep_manager_lock_deep_sleep();
40+
}
3541
_start = ticker_read_us(_ticker_data);
3642
_running = 1;
3743
}
@@ -42,7 +48,9 @@ void Timer::stop() {
4248
core_util_critical_section_enter();
4349
_time += slicetime();
4450
if (_running) {
45-
sleep_manager_unlock_deep_sleep();
51+
if(_lock_deepsleep) {
52+
sleep_manager_unlock_deep_sleep();
53+
}
4654
}
4755
_running = 0;
4856
core_util_critical_section_exit();

drivers/Timer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Timer : private NonCopyable<Timer> {
100100
us_timestamp_t _start; // the start time of the latest slice
101101
us_timestamp_t _time; // any accumulated time from previous slices
102102
const ticker_data_t *_ticker_data;
103+
bool _lock_deepsleep; // flag which indicates if deep-sleep should be disabled
103104
};
104105

105106
} // namespace mbed

0 commit comments

Comments
 (0)