Fix for PWM resume issue, SWINTEGRATION-57

pull/15139/head
Bill Waters 2021-10-12 10:23:07 -07:00
parent 54a4879801
commit a2e46525ac
1 changed files with 8 additions and 0 deletions

View File

@ -28,6 +28,8 @@ extern "C" {
static const int CY_US_PER_SECOND = 1000000;
static const int CY_US_PER_MS = 1000;
static float _percent = 0.0f;
void pwmout_init(pwmout_t *obj, PinName pin)
{
if (CY_RSLT_SUCCESS != cyhal_pwm_init(&(obj->hal_pwm), pin, NULL)) {
@ -35,6 +37,7 @@ void pwmout_init(pwmout_t *obj, PinName pin)
}
obj->period_us = 100;
obj->width_us = 0;
_percent = 0.0f;
}
void pwmout_free(pwmout_t *obj)
@ -46,6 +49,7 @@ void pwmout_write(pwmout_t *obj, float percent)
{
MBED_ASSERT(percent >= 0.0f && percent <= 1.0f);
pwmout_pulsewidth_us(obj, (int)(percent * obj->period_us));
_percent = percent;
}
float pwmout_read(pwmout_t *obj)
@ -66,6 +70,9 @@ void pwmout_period_ms(pwmout_t *obj, int ms)
void pwmout_period_us(pwmout_t *obj, int us)
{
obj->period_us = (uint32_t)us;
if (_percent != 0.0f) {
obj->width_us = (int)(_percent * obj->period_us);
}
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");
}
@ -91,6 +98,7 @@ void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
{
_percent = 0.0f;
obj->width_us = (uint32_t)us;
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");