Merge pull request #5148 from mprse/fix_enable_deepsleep_for_lp_timer

Enable deepsleep for LowPowerXXX objects
pull/5217/head
Jimmy Brisson 2017-09-28 14:11:40 -05:00 committed by GitHub
commit bb61b42fba
4 changed files with 31 additions and 15 deletions

View File

@ -25,10 +25,11 @@ namespace mbed {
void Ticker::detach() { void Ticker::detach() {
core_util_critical_section_enter(); core_util_critical_section_enter();
remove(); remove();
// unlocked only if we were attached (we locked it) // unlocked only if we were attached (we locked it) and this is not low power ticker
if (_function) { if(_function && _lock_deepsleep) {
sleep_manager_unlock_deep_sleep(); sleep_manager_unlock_deep_sleep();
} }
_function = 0; _function = 0;
core_util_critical_section_exit(); core_util_critical_section_exit();
} }

View File

@ -21,13 +21,14 @@
#include "platform/mbed_toolchain.h" #include "platform/mbed_toolchain.h"
#include "platform/NonCopyable.h" #include "platform/NonCopyable.h"
#include "platform/mbed_sleep.h" #include "platform/mbed_sleep.h"
#include "hal/lp_ticker_api.h"
namespace mbed { namespace mbed {
/** \addtogroup drivers */ /** \addtogroup drivers */
/** A Ticker is used to call a function at a recurring interval /** A Ticker is used to call a function at a recurring interval
* *
* You can use as many seperate Ticker objects as you require. * You can use as many separate Ticker objects as you require.
* *
* @note Synchronization level: Interrupt safe * @note Synchronization level: Interrupt safe
* *
@ -64,14 +65,18 @@ namespace mbed {
class Ticker : public TimerEvent, private NonCopyable<Ticker> { class Ticker : public TimerEvent, private NonCopyable<Ticker> {
public: public:
Ticker() : TimerEvent(), _function(0) { Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) {
} }
Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0) { // When low power ticker is in use, then do not disable deep-sleep.
Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) {
data->interface->init(); data->interface->init();
#if DEVICE_LOWPOWERTIMER
_lock_deepsleep = (data != get_lp_ticker_data());
#endif
} }
/** Attach a function to be called by the Ticker, specifiying the interval in seconds /** Attach a function to be called by the Ticker, specifying the interval in seconds
* *
* @param func pointer to the function to be called * @param func pointer to the function to be called
* @param t the time between calls in seconds * @param t the time between calls in seconds
@ -80,7 +85,7 @@ public:
attach_us(func, t * 1000000.0f); attach_us(func, t * 1000000.0f);
} }
/** Attach a member function to be called by the Ticker, specifiying the interval in seconds /** Attach a member function to be called by the Ticker, specifying the interval in seconds
* *
* @param obj pointer to the object to call the member function on * @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called * @param method pointer to the member function to be called
@ -97,7 +102,7 @@ public:
attach(callback(obj, method), t); attach(callback(obj, method), t);
} }
/** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
* *
* @param func pointer to the function to be called * @param func pointer to the function to be called
* @param t the time between calls in micro-seconds * @param t the time between calls in micro-seconds
@ -108,15 +113,15 @@ public:
* *
*/ */
void attach_us(Callback<void()> func, us_timestamp_t t) { void attach_us(Callback<void()> func, us_timestamp_t t) {
// lock only for the initial callback setup // lock only for the initial callback setup and this is not low power ticker
if (!_function) { if(!_function && _lock_deepsleep) {
sleep_manager_lock_deep_sleep(); sleep_manager_lock_deep_sleep();
} }
_function = func; _function = func;
setup(t); setup(t);
} }
/** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
* *
* @param obj pointer to the object to call the member function on * @param obj pointer to the object to call the member function on
* @param method pointer to the member function to be called * @param method pointer to the member function to be called
@ -148,6 +153,7 @@ protected:
protected: protected:
us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
Callback<void()> _function; /**< Callback. */ Callback<void()> _function; /**< Callback. */
bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */
}; };
} // namespace mbed } // namespace mbed

View File

@ -17,15 +17,19 @@
#include "hal/ticker_api.h" #include "hal/ticker_api.h"
#include "hal/us_ticker_api.h" #include "hal/us_ticker_api.h"
#include "platform/mbed_critical.h" #include "platform/mbed_critical.h"
#include "hal/lp_ticker_api.h"
namespace mbed { namespace mbed {
Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()) { Timer::Timer() : _running(), _start(), _time(), _ticker_data(get_us_ticker_data()), _lock_deepsleep(true) {
reset(); reset();
} }
Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data) { Timer::Timer(const ticker_data_t *data) : _running(), _start(), _time(), _ticker_data(data), _lock_deepsleep(true) {
reset(); reset();
#if DEVICE_LOWPOWERTIMER
_lock_deepsleep = (data != get_lp_ticker_data());
#endif
} }
Timer::~Timer() { Timer::~Timer() {
@ -40,7 +44,9 @@ Timer::~Timer() {
void Timer::start() { void Timer::start() {
core_util_critical_section_enter(); core_util_critical_section_enter();
if (!_running) { if (!_running) {
if(_lock_deepsleep) {
sleep_manager_lock_deep_sleep(); sleep_manager_lock_deep_sleep();
}
_start = ticker_read_us(_ticker_data); _start = ticker_read_us(_ticker_data);
_running = 1; _running = 1;
} }
@ -51,8 +57,10 @@ void Timer::stop() {
core_util_critical_section_enter(); core_util_critical_section_enter();
_time += slicetime(); _time += slicetime();
if (_running) { if (_running) {
if(_lock_deepsleep) {
sleep_manager_unlock_deep_sleep(); sleep_manager_unlock_deep_sleep();
} }
}
_running = 0; _running = 0;
core_util_critical_section_exit(); core_util_critical_section_exit();
} }

View File

@ -101,6 +101,7 @@ protected:
us_timestamp_t _start; // the start time of the latest slice us_timestamp_t _start; // the start time of the latest slice
us_timestamp_t _time; // any accumulated time from previous slices us_timestamp_t _time; // any accumulated time from previous slices
const ticker_data_t *_ticker_data; const ticker_data_t *_ticker_data;
bool _lock_deepsleep; // flag which indicates if deep-sleep should be disabled
}; };
} // namespace mbed } // namespace mbed