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>
This commit is contained in:
robert-hh 2023-05-25 14:05:19 +02:00 committed by Damien George
parent 73cc6b750e
commit 8f6315a279

View File

@ -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"));
}
}