diff options
| author | robert-hh <robert@hammelrath.com> | 2023-03-04 11:36:54 +0100 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2023-03-10 10:30:16 +1100 |
| commit | cf43df4caa5a9cff4be149517504b1bb0e091436 (patch) | |
| tree | b3d7d155de78b051d02a8a0dd3dc55e71ac1aec4 | |
| parent | b336b6bb743159356f26dc79585186ca3c89cab6 (diff) | |
nrf/modules/machine/pwm: Add paramter checks and error messages.
| -rw-r--r-- | ports/nrf/modules/machine/pwm.c | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/ports/nrf/modules/machine/pwm.c b/ports/nrf/modules/machine/pwm.c index 7c84a8c88..9ec36d1e1 100644 --- a/ports/nrf/modules/machine/pwm.c +++ b/ports/nrf/modules/machine/pwm.c @@ -97,7 +97,6 @@ STATIC int hard_pwm_find(mp_obj_t id) { if (pwm_id >= 0 && pwm_id < MP_ARRAY_SIZE(machine_hard_pwm_obj)) { return pwm_id; } - mp_raise_ValueError(MP_ERROR_TEXT("PWM doesn't exist")); } return -1; } @@ -231,19 +230,22 @@ STATIC mp_obj_t machine_hard_pwm_make_new(mp_arg_val_t *args) { enum { ARG_id, ARG_pin, ARG_freq, ARG_period, ARG_duty, ARG_pulse_width, ARG_mode }; // get static peripheral object int pwm_id = hard_pwm_find(args[ARG_id].u_obj); + if (pwm_id < 0) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid or missing PWM id")); + } const machine_hard_pwm_obj_t *self = &machine_hard_pwm_obj[pwm_id]; // check if PWM pin is set if (args[ARG_pin].u_obj != MP_OBJ_NULL) { self->p_config->pwm_pin = mp_hal_get_pin_obj(args[ARG_pin].u_obj)->pin; } else { - // TODO: raise exception. + mp_raise_ValueError(MP_ERROR_TEXT("Pin missing")); } if (args[ARG_freq].u_obj != MP_OBJ_NULL) { self->p_config->freq = mp_obj_get_int(args[ARG_freq].u_obj); } else { - self->p_config->freq = 50; // 50 Hz by default. + self->p_config->freq = 2; // 4 MHz by default. } if (args[ARG_period].u_obj != MP_OBJ_NULL) { @@ -294,7 +296,7 @@ STATIC void machine_hard_pwm_init(mp_obj_t self_in, mp_arg_val_t *args) { uint16_t pulse_width = ((self->p_config->period * self->p_config->duty) / 100); - // If manual period has been set, override duty-cycle. + // If manual pulse width has been set, override duty-cycle. if (self->p_config->pulse_width > 0) { pulse_width = self->p_config->pulse_width; } |
