From 67b3cae5022f5c6e857e6e2068002d83fa5e13ec Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Wed, 7 Feb 2024 11:09:47 +0530 Subject: [PATCH] ports/psoc6: Fixed issue setting u16 raw values. Signed-off-by: NikhitaR-IFX --- ports/psoc6/machine_pwm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ports/psoc6/machine_pwm.c b/ports/psoc6/machine_pwm.c index 41857a1418d0f..2fc9b5a5fa4fc 100644 --- a/ports/psoc6/machine_pwm.c +++ b/ports/psoc6/machine_pwm.c @@ -70,7 +70,7 @@ STATIC cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty } STATIC inline cy_rslt_t pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width) { - return cyhal_pwm_set_period(pwm_obj, 1000000 / fz, pulse_width * 1000); + return cyhal_pwm_set_period(pwm_obj, 1000000 / fz, pulse_width / 1000); // !# * --> / } STATIC inline cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) { @@ -99,12 +99,13 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, // self->active = 1; if ((args[ARG_freq].u_int != VALUE_NOT_SET)) { - pwm_freq_duty_set(&self->pwm_obj, args[ARG_freq].u_int, self->duty); + // pwm_freq_duty_set(&self->pwm_obj, args[ARG_freq].u_int, self->duty); self->fz = args[ARG_freq].u_int; } if ((args[ARG_duty_u16].u_int != VALUE_NOT_SET)) { - pwm_freq_duty_set(&self->pwm_obj, self->fz, args[ARG_duty_u16].u_int / 65535); + float val = (float)(args[ARG_duty_u16].u_int) / (float)65535; + pwm_freq_duty_set(&self->pwm_obj, self->fz, val); self->duty = args[ARG_duty_u16].u_int; self->duty_type = DUTY_U16; } @@ -178,7 +179,7 @@ STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_int_t duty_u // Check the value is more than the max value self->duty = duty_u16 > 65535 ? 65535 : duty_u16; self->duty_type = DUTY_U16; - pwm_freq_duty_set(&self->pwm_obj, self->fz, (self->duty) / 65535); // conversion of duty_u16 into dutyu16/65535 + pwm_freq_duty_set(&self->pwm_obj, self->fz, (float)(self->duty) / (float)65535); // s conversion of duty_u16 into dutyu16/65535 } STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) {