From 5a6aee43eb58c9ba3b1d2de7d178a3d68867a0da Mon Sep 17 00:00:00 2001 From: Martin Kojtal <0xc0170@gmail.com> Date: Fri, 22 Sep 2017 14:59:07 +0100 Subject: [PATCH] PWMOut: lock deesleep addition As PWMOut in most cases depend on high speed freq clock, it should lock deepsleep while active. --- drivers/PwmOut.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/drivers/PwmOut.h b/drivers/PwmOut.h index 1a90c3576a..c94b8584fd 100644 --- a/drivers/PwmOut.h +++ b/drivers/PwmOut.h @@ -21,6 +21,7 @@ #if defined (DEVICE_PWMOUT) || defined(DOXYGEN_ONLY) #include "hal/pwmout_api.h" #include "platform/mbed_critical.h" +#include "platform/mbed_sleep.h" namespace mbed { /** \addtogroup drivers */ @@ -56,12 +57,18 @@ public: * * @param pin PwmOut pin to connect to */ - PwmOut(PinName pin) { + PwmOut(PinName pin) : _deep_sleep_locked(false) { core_util_critical_section_enter(); pwmout_init(&_pwm, pin); core_util_critical_section_exit(); } + ~PwmOut() { + core_util_critical_section_enter(); + unlock_deep_sleep(); + core_util_critical_section_exit(); + } + /** Set the ouput duty-cycle, specified as a percentage (float) * * @param value A floating-point value representing the output duty-cycle, @@ -71,6 +78,7 @@ public: */ void write(float value) { core_util_critical_section_enter(); + lock_deep_sleep(); pwmout_write(&_pwm, value); core_util_critical_section_exit(); } @@ -177,7 +185,24 @@ public: } protected: + /** Lock deep sleep only if it is not yet locked */ + void lock_deep_sleep() { + if (_deep_sleep_locked == false) { + sleep_manager_lock_deep_sleep(); + _deep_sleep_locked = true; + } + } + + /** Unlock deep sleep in case it is locked */ + void unlock_deep_sleep() { + if (_deep_sleep_locked == true) { + sleep_manager_unlock_deep_sleep(); + _deep_sleep_locked = false; + } + } + pwmout_t _pwm; + bool _deep_sleep_locked; }; } // namespace mbed