Skip to content

Commit 84e0d5d

Browse files
authored
Merge pull request #15139 from billwatersiii/pr/pwm_resume_fix
Fix for PWM resume issue, SWINTEGRATION-57
2 parents d9b2b7d + a2e4652 commit 84e0d5d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

targets/TARGET_Cypress/TARGET_PSOC6/cy_pwmout_api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ extern "C" {
2828
static const int CY_US_PER_SECOND = 1000000;
2929
static const int CY_US_PER_MS = 1000;
3030

31+
static float _percent = 0.0f;
32+
3133
void pwmout_init(pwmout_t *obj, PinName pin)
3234
{
3335
if (CY_RSLT_SUCCESS != cyhal_pwm_init(&(obj->hal_pwm), pin, NULL)) {
3436
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_init");
3537
}
3638
obj->period_us = 100;
3739
obj->width_us = 0;
40+
_percent = 0.0f;
3841
}
3942

4043
void pwmout_free(pwmout_t *obj)
@@ -46,6 +49,7 @@ void pwmout_write(pwmout_t *obj, float percent)
4649
{
4750
MBED_ASSERT(percent >= 0.0f && percent <= 1.0f);
4851
pwmout_pulsewidth_us(obj, (int)(percent * obj->period_us));
52+
_percent = percent;
4953
}
5054

5155
float pwmout_read(pwmout_t *obj)
@@ -66,6 +70,9 @@ void pwmout_period_ms(pwmout_t *obj, int ms)
6670
void pwmout_period_us(pwmout_t *obj, int us)
6771
{
6872
obj->period_us = (uint32_t)us;
73+
if (_percent != 0.0f) {
74+
obj->width_us = (int)(_percent * obj->period_us);
75+
}
6976
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
7077
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");
7178
}
@@ -91,6 +98,7 @@ void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
9198

9299
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
93100
{
101+
_percent = 0.0f;
94102
obj->width_us = (uint32_t)us;
95103
if (CY_RSLT_SUCCESS != cyhal_pwm_set_period(&(obj->hal_pwm), obj->period_us, obj->width_us)) {
96104
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_PWM, MBED_ERROR_CODE_FAILED_OPERATION), "cyhal_pwm_set_period");

0 commit comments

Comments
 (0)