diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 1e2b1021ac9b..324e6a04af7d 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -128,7 +128,6 @@ DRIVERS_SRC_C += $(addprefix drivers/,\ ) DRIVERS_SRC_C += $(addprefix drivers/,\ - 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 e6289e87b50f..000000000000 --- 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 c61e92484b1f..000000000000 --- 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 434fdfd888c9..ea69049c10ec 100644 --- a/ports/psoc6/modules/machine/machine_pwm.c +++ b/ports/psoc6/modules/machine/machine_pwm.c @@ -3,12 +3,22 @@ #include "modmachine.h" // port-specific includes -#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, @@ -20,6 +30,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) { @@ -70,7 +93,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); @@ -79,7 +102,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) { @@ -99,7 +122,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) { @@ -115,7 +138,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; }