fixed resets after suspend

pull/13492/head
talorion 2020-08-25 23:48:28 +02:00 committed by Gregor Mayramhof
parent 191ec42dd6
commit 6625bdb9f3
2 changed files with 33 additions and 1 deletions

View File

@ -110,6 +110,12 @@ public:
*/ */
void period_us(int us); void period_us(int us);
/** Read the PWM period
* @returns
* The PWM period, specified in microseconds (int)
*/
int read_period_us();
/** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
* @param seconds Change the pulse width of a PWM signal specified in seconds (float) * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
*/ */
@ -125,6 +131,12 @@ public:
*/ */
void pulsewidth_us(int us); void pulsewidth_us(int us);
/** Read the PWM pulsewidth
* @returns
* The PWM pulsewith, specified in microseconds (int)
*/
int read_pulsewitdth_us();
/** Suspend PWM operation /** Suspend PWM operation
* *
* Control the PWM state. This is primarily intended * Control the PWM state. This is primarily intended
@ -191,6 +203,7 @@ protected:
bool _deep_sleep_locked; bool _deep_sleep_locked;
bool _initialized; bool _initialized;
float _duty_cycle; float _duty_cycle;
int _period_us;
#endif #endif
}; };

View File

@ -30,7 +30,8 @@ PwmOut::PwmOut(PinName pin) :
_pin(pin), _pin(pin),
_deep_sleep_locked(false), _deep_sleep_locked(false),
_initialized(false), _initialized(false),
_duty_cycle(0) _duty_cycle(0),
_period_us(0)
{ {
PwmOut::init(); PwmOut::init();
} }
@ -83,6 +84,14 @@ void PwmOut::period_us(int us)
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
int PwmOut::read_period_us()
{
core_util_critical_section_enter();
auto val = pwmout_read_period_us(&_pwm);
core_util_critical_section_exit();
return val;
}
void PwmOut::pulsewidth(float seconds) void PwmOut::pulsewidth(float seconds)
{ {
core_util_critical_section_enter(); core_util_critical_section_enter();
@ -104,11 +113,20 @@ void PwmOut::pulsewidth_us(int us)
core_util_critical_section_exit(); core_util_critical_section_exit();
} }
int PwmOut::read_pulsewitdth_us()
{
core_util_critical_section_enter();
auto val = pwmout_read_pulsewidth_us(&_pwm);
core_util_critical_section_exit();
return val;
}
void PwmOut::suspend() void PwmOut::suspend()
{ {
core_util_critical_section_enter(); core_util_critical_section_enter();
if (_initialized) { if (_initialized) {
_duty_cycle = PwmOut::read(); _duty_cycle = PwmOut::read();
_period_us = PwmOut::read_period_us();
PwmOut::deinit(); PwmOut::deinit();
} }
core_util_critical_section_exit(); core_util_critical_section_exit();
@ -120,6 +138,7 @@ void PwmOut::resume()
if (!_initialized) { if (!_initialized) {
PwmOut::init(); PwmOut::init();
PwmOut::write(_duty_cycle); PwmOut::write(_duty_cycle);
PwmOut::period_us(_period_us);
} }
core_util_critical_section_exit(); core_util_critical_section_exit();
} }