Skip to content

Fix for PWM resume issue, SWINTEGRATION-57 #15139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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