diff options
| author | robert-hh <robert@hammelrath.com> | 2023-05-25 14:05:19 +0200 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2023-06-01 12:25:34 +1000 |
| commit | 8f6315a279a7a5b2373902d6d9a868a1f8a4dbb2 (patch) | |
| tree | 021af7f8e871a3a35153f3bf1d6ad144d7ff496c | |
| parent | 73cc6b750e0709170183ce7da89af618eae40329 (diff) | |
mimxrt/machine_pwm: Fix freq change, PWM print, and error checks.
Three bugs have been fixed in this commit:
1. When the duty was set with duty_u16(), changing the freq with pwm.freq()
would not keep relative duty rate, but the absolute pulse duration.
2. Fix another inconsistency when displaying the PWM pin's properties of a
QTMR channel.
3. Improve the error checks for the second channel being a PWM pin and pin
pairs to be a FLEXPWM A/B pair.
Signed-off-by: robert-hh <robert@hammelrath.com>
| -rw-r--r-- | ports/mimxrt/machine_pwm.c | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/ports/mimxrt/machine_pwm.c b/ports/mimxrt/machine_pwm.c index b8b526ae0..a50b67cc8 100644 --- a/ports/mimxrt/machine_pwm.c +++ b/ports/mimxrt/machine_pwm.c @@ -91,7 +91,7 @@ STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_p } else { mp_printf(print, "<QTMR_PWM module=%u channel=%u freq=%u ", self->module, self->channel1, self->freq); - if (self->duty_ns == VALUE_NOT_SET) { + if (self->duty_ns != VALUE_NOT_SET) { mp_printf(print, "duty_ns=%d>", self->duty_ns); } else { mp_printf(print, "duty_u16=%d>", self->duty_u16); @@ -110,10 +110,6 @@ STATIC uint32_t duty_ns_to_duty_u16(uint32_t freq, uint32_t duty_ns) { return (uint32_t)duty; } -STATIC uint32_t duty_u16_to_duty_ns(machine_pwm_obj_t *self) { - return 1000000000ULL * (uint64_t)self->duty_u16 / self->freq / PWM_FULL_SCALE; -} - STATIC uint8_t module_decode(char channel) { switch (channel) { case '0': @@ -349,8 +345,6 @@ STATIC void configure_pwm(machine_pwm_obj_t *self) { if (self->freq != VALUE_NOT_SET && (self->duty_u16 != VALUE_NOT_SET || self->duty_ns != VALUE_NOT_SET)) { if (self->duty_ns != VALUE_NOT_SET) { self->duty_u16 = duty_ns_to_duty_u16(self->freq, self->duty_ns); - } else { - self->duty_ns = duty_u16_to_duty_ns(self); } if (self->is_flexpwm) { configure_flexpwm(self); @@ -489,6 +483,9 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args break; } } + if (af_obj2 == NULL) { + mp_raise_ValueError(MP_ERROR_TEXT("the second Pin doesn't support PWM")); + } } if (af_obj1 == NULL) { submodule1 = 0; @@ -504,12 +501,12 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args } #endif if (af_obj1 == NULL) { - mp_raise_ValueError(MP_ERROR_TEXT("the requested Pin(s) does not support PWM")); + mp_raise_ValueError(MP_ERROR_TEXT("the first Pin doesn't support PWM")); } } else { // is flexpwm, check for instance match is_flexpwm = true; - if (pin2 != NULL && af_obj1->instance != af_obj2->instance && submodule1 != submodule2) { + if (pin2 != NULL && (af_obj1->instance != af_obj2->instance || submodule1 != submodule2)) { mp_raise_ValueError(MP_ERROR_TEXT("the pins must be a A/B pair of a submodule")); } } |
