From 8b9c468f395de611bd9d2d935b1db020d8bfccbd Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Thu, 7 Sep 2023 10:52:19 +0200 Subject: [PATCH] ports/psoc6: Removed psoc6_pwm driver. Signed-off-by: enriquezgarc --- ports/psoc6/Makefile | 1 - ports/psoc6/drivers/machine/psoc6_pwm.c | 29 -------------------- ports/psoc6/drivers/machine/psoc6_pwm.h | 28 ------------------- ports/psoc6/modules/machine/machine_pwm.c | 33 +++++++++++++++++++---- 4 files changed, 28 insertions(+), 63 deletions(-) delete mode 100644 ports/psoc6/drivers/machine/psoc6_pwm.c delete mode 100644 ports/psoc6/drivers/machine/psoc6_pwm.h diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index d185ce246884a..01f5a5651fa37 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -130,7 +130,6 @@ DRIVERS_SRC_C += $(addprefix drivers/,\ DRIVERS_SRC_C += $(addprefix drivers/,\ machine/psoc6_gpio.c \ machine/psoc6_i2c.c \ - machine/psoc6_pwm.c \ machine/psoc6_system.c \ ) diff --git a/ports/psoc6/drivers/machine/psoc6_pwm.c b/ports/psoc6/drivers/machine/psoc6_pwm.c deleted file mode 100644 index e6289e87b50ff..0000000000000 --- a/ports/psoc6/drivers/machine/psoc6_pwm.c +++ /dev/null @@ -1,29 +0,0 @@ -// MTB includes -#include "cyhal.h" - -// port-specific includes -#include "psoc6_pwm.h" - -cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle) { - return cyhal_pwm_set_duty_cycle(pwm_obj, duty_cycle * 100, fz); // duty_cycle in percentage -} - -cy_rslt_t pwm_start(cyhal_pwm_t *pwm_obj) { - return cyhal_pwm_start(pwm_obj); -} - -cy_rslt_t pwm_init(machine_pwm_obj_t *machine_pwm_obj) { - return cyhal_pwm_init(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin, NULL); -} - -void pwm_deinit(cyhal_pwm_t *pwm_obj) { - cyhal_pwm_free(pwm_obj); -} - -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); -} - -cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) { - return cyhal_pwm_init_adv(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin, NC, CYHAL_PWM_LEFT_ALIGN, true, 0, true, NULL); // complimentary pin set as not connected -} diff --git a/ports/psoc6/drivers/machine/psoc6_pwm.h b/ports/psoc6/drivers/machine/psoc6_pwm.h deleted file mode 100644 index c61e92484b1f8..0000000000000 --- a/ports/psoc6/drivers/machine/psoc6_pwm.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef MICROPY_INCLUDED_PSOC6_PWM_H -#define MICROPY_INCLUDED_PSOC6_PWM_H - -// mpy includes -#include "py/runtime.h" - -// MTB includes -#include "cyhal.h" - -typedef struct _machine_pwm_obj_t { - mp_obj_base_t base; - cyhal_pwm_t pwm_obj; - bool active; - uint8_t pin; - uint32_t fz; - uint8_t duty_type; - mp_float_t duty; - bool invert; -} machine_pwm_obj_t; - -cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle); -cy_rslt_t pwm_start(cyhal_pwm_t *pwm_obj); -cy_rslt_t pwm_init(machine_pwm_obj_t *machine_pwm_obj); -cy_rslt_t pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width); -cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj); -void pwm_deinit(cyhal_pwm_t *pwm_obj); - -#endif // MICROPY_INCLUDED_PSOC6_PWM_H diff --git a/ports/psoc6/modules/machine/machine_pwm.c b/ports/psoc6/modules/machine/machine_pwm.c index dc464ebacbbd6..f38200eaf2a35 100644 --- a/ports/psoc6/modules/machine/machine_pwm.c +++ b/ports/psoc6/modules/machine/machine_pwm.c @@ -4,12 +4,22 @@ // port-specific includes #include "drivers/machine/psoc6_gpio.h" -#include "drivers/machine/psoc6_pwm.h" #include "mplogger.h" #include "pins.h" extern mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj); +typedef struct _machine_pwm_obj_t { + mp_obj_base_t base; + cyhal_pwm_t pwm_obj; + bool active; + uint8_t pin; + uint32_t fz; + uint8_t duty_type; + mp_float_t duty; + bool invert; +} machine_pwm_obj_t; + enum { VALUE_NOT_SET = -1, DUTY_NOT_SET = 0, @@ -21,6 +31,19 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq); STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_float_t duty_u16); STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_float_t duty_ns); +STATIC cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle) { + return cyhal_pwm_set_duty_cycle(pwm_obj, duty_cycle * 100, fz); // duty_cycle in percentage +} + +STATIC 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); +} + +STATIC cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) { + return cyhal_pwm_init_adv(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin, NC, CYHAL_PWM_LEFT_ALIGN, true, 0, true, NULL); // complimentary pin set as not connected +} + + // To check whether the PWM is active STATIC void pwm_is_active(machine_pwm_obj_t *self) { if (self->active == 0) { @@ -71,7 +94,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, if (args[ARG_invert].u_int != VALUE_NOT_SET) { self->invert = args[ARG_invert].u_int; if (self->invert == 1) { - pwm_deinit(&self->pwm_obj); + cyhal_pwm_free(&self->pwm_obj); cy_rslt_t result = pwm_advanced_init(self); if (result != CY_RSLT_SUCCESS) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("PWM initialisation failed with return code %lx ! and invert output is not available"), result); @@ -80,7 +103,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, self->duty = ((1) - ((self->duty) / 65535)) * 65535; } } - pwm_start(&self->pwm_obj); + cyhal_pwm_start(&self->pwm_obj); } STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { @@ -100,7 +123,7 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args self->invert = -1; // Initialize PWM - cy_rslt_t result = pwm_init(self); + cy_rslt_t result = cyhal_pwm_init(&self->pwm_obj, self->pin, NULL); // To check whether PWM init is successful if (result != CY_RSLT_SUCCESS) { @@ -116,7 +139,7 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args } STATIC void mp_machine_pwm_deinit(machine_pwm_obj_t *self) { - pwm_deinit(&self->pwm_obj); + cyhal_pwm_free(&self->pwm_obj); self->active = 0; }