Skip to content

Commit

Permalink
Merge pull request #15139 from billwatersiii/pr/pwm_resume_fix
Browse files Browse the repository at this point in the history
Fix for PWM resume issue, SWINTEGRATION-57
  • Loading branch information
0xc0170 authored Oct 25, 2021
2 parents d9b2b7d + a2e4652 commit 84e0d5d
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions targets/TARGET_Cypress/TARGET_PSOC6/cy_pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ 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)) {
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_init");
}
obj->period_us = 100;
obj->width_us = 0;
_percent = 0.0f;
}

void pwmout_free(pwmout_t *obj)
Expand All @@ -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)
Expand All @@ -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");
}
Expand All @@ -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");
Expand Down

0 comments on commit 84e0d5d

Please sign in to comment.